Class: Discord::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/discord/client.rb

Constant Summary collapse

BASE_URL =
"https://discord.com/api/v10"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_access_token: nil, bot_access_token: nil, adapter: Faraday.default_adapter) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/discord/client.rb', line 7

def initialize(user_access_token: nil, bot_access_token: nil, adapter: Faraday.default_adapter)
  @user_access_token = user_access_token
  @bot_access_token = bot_access_token
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/discord/client.rb', line 5

def adapter
  @adapter
end

#bot_access_tokenObject (readonly)

Returns the value of attribute bot_access_token.



5
6
7
# File 'lib/discord/client.rb', line 5

def bot_access_token
  @bot_access_token
end

#user_access_tokenObject (readonly)

Returns the value of attribute user_access_token.



5
6
7
# File 'lib/discord/client.rb', line 5

def user_access_token
  @user_access_token
end

Instance Method Details

#channelsObject



21
22
23
# File 'lib/discord/client.rb', line 21

def channels
  ChannelsResource.new(self)
end

#connectionObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/discord/client.rb', line 29

def connection
  @connection ||= Faraday.new(BASE_URL) do |conn|
    if user_access_token
      conn.request :authorization, :Bearer, user_access_token
    elsif bot_access_token
      conn.request :authorization, :Bot, bot_access_token
    else
      raise Error, "You must provide a user or bot access token."
    end

    conn.headers = {
      "User-Agent" => "discord_api/v#{VERSION} (github.com/deanpcmad/discord_api)"
    }

    conn.request :json

    conn.response :json, content_type: "application/json"

    conn.adapter adapter, @stubs
  end
end

#guildsObject



17
18
19
# File 'lib/discord/client.rb', line 17

def guilds
  GuildsResource.new(self)
end

#messagesObject



25
26
27
# File 'lib/discord/client.rb', line 25

def messages
  MessagesResource.new(self)
end

#usersObject



13
14
15
# File 'lib/discord/client.rb', line 13

def users
  UsersResource.new(self)
end