Module: DSU3::API

Defined in:
lib/dsu3/api.rb,
lib/dsu3/api/user.rb,
lib/dsu3/api/guild.rb,
lib/dsu3/api/invite.rb,
lib/dsu3/api/channel.rb,
lib/dsu3/api/message.rb

Overview

Namespace for all methods representing Discord API endpoints

Defined Under Namespace

Modules: Channel, Guild, Invite, Message, User

Constant Summary collapse

API_BASE =
'https://discord.com/api/v9/'

Class Method Summary collapse

Class Method Details

.raw_request(token, method, route, headers = {}, payload = nil) ⇒ String

Makes an API request without any error handling

Parameters:

  • token (String)

    Discord account token

  • method (Symbol, String)
  • route (String)

    Discord API route

  • headers (Hash) (defaults to: {})

    Additional request headers

  • payload (String) (defaults to: nil)

Returns:

  • (String)

    Response body



27
28
29
30
31
32
33
34
35
# File 'lib/dsu3/api.rb', line 27

def raw_request(token, method, route, headers = {}, payload = nil)
  RestClient::Request.execute(
    method: method,
    url: API_BASE + route,
    headers: headers.merge(DSU3::Props::HEADERS, { authorization: token }),
    payload: payload,
    proxy: DSU3.proxies.sample
  ).body
end

.requestString

Makes an API request, includes handling ratelimits and errors

Parameters:

  • token (String)

    Discord account token

  • method (Symbol, String)
  • route (String)

    Discord API route

  • headers (Hash)

    Additional request headers

  • payload (String)

Returns:

  • (String)

    Response body



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dsu3/api.rb', line 40

def request(...)
  raw_request(...)
rescue RestClient::ExceptionWithResponse => e
  begin
    data = JSON.parse(e.response.body)
    raise DSU3::RateLimitError.new('ratelimit exceeded', data['retry_after']) if e.is_a?(RestClient::TooManyRequests)
    raise DSU3::CodeError, "#{data['code']}: #{data['message']}"
  rescue JSON::ParserError
    raise e
  end
end