Class: Discordrb::Light::LightBot

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/light/light_bot.rb

Overview

A bot that only uses the REST part of the API. Hierarchically unrelated to the regular Bot. Useful to make applications integrated to Discord over OAuth, for example.

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ LightBot

Create a new LightBot. This does no networking yet, all networking is done by the methods on this class.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/discordrb/light/light_bot.rb', line 16

def initialize(token)
  if token.respond_to? :token
    # Parse AccessTokens from the OAuth2 gem
    token = token.token
  end

  unless token.include? '.'
    # Discord user/bot tokens always contain two dots, so if there's none we can assume it's an OAuth token.
    token = "Bearer #{token}" # OAuth tokens have to be prefixed with 'Bearer' for Discord to be able to use them
  end

  @token = token
end

Instance Method Details

#connectionsArray<Connection>

Gets the connections associated with this account.



51
52
53
54
# File 'lib/discordrb/light/light_bot.rb', line 51

def connections
  response = Discordrb::API.connections(@token)
  JSON.parse(response).map { |e| Connection.new(e, self) }
end

#join(code) ⇒ Object

Joins a server using an instant invite.



45
46
47
# File 'lib/discordrb/light/light_bot.rb', line 45

def join(code)
  Discordrb::API.join_server(@token, code)
end

#profileLightProfile



31
32
33
34
# File 'lib/discordrb/light/light_bot.rb', line 31

def profile
  response = Discordrb::API.profile(@token)
  LightProfile.new(JSON.parse(response), self)
end

#serversArray<LightServer>



37
38
39
40
# File 'lib/discordrb/light/light_bot.rb', line 37

def servers
  response = Discordrb::API.servers(@token)
  JSON.parse(response).map { |e| LightServer.new(e, self) }
end