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.

Parameters:

  • token (String)

    The token that should be used to authenticate to Discord. Can be an OAuth token or a regular user account token.



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

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.

Returns:

  • (Array<Connection>)

    this account's connections.



53
54
55
56
# File 'lib/discordrb/light/light_bot.rb', line 53

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

#join(code) ⇒ Object

Joins a server using an instant invite.

Parameters:



47
48
49
# File 'lib/discordrb/light/light_bot.rb', line 47

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

#profileLightProfile

Returns the details of the user this bot is connected to.

Returns:

  • (LightProfile)

    the details of the user this bot is connected to.



33
34
35
36
# File 'lib/discordrb/light/light_bot.rb', line 33

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

#serversArray<LightServer>

Returns the servers this bot is connected to.

Returns:

  • (Array<LightServer>)

    the servers this bot is connected to.



39
40
41
42
# File 'lib/discordrb/light/light_bot.rb', line 39

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