Class: Lita::Adapters::Discord::API

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/adapters/discord/api.rb

Constant Summary collapse

APIBASE =
'https://discordapp.com/api'.freeze

Instance Method Summary collapse

Instance Method Details

#avatar_url(user_id, avatar_id) ⇒ Object

Make an avatar URL from the user and avatar IDs



40
41
42
# File 'lib/lita/adapters/discord/api.rb', line 40

def avatar_url(user_id, avatar_id)
	"#{APIBASE}/users/#{user_id}/avatars/#{avatar_id}.jpg"
end

#login(email, password) ⇒ Object

Login to the server



45
46
47
# File 'lib/lita/adapters/discord/api.rb', line 45

def (email, password)
	request( :post, "#{APIBASE}/auth/login", email: email, password: password )
end

#logout(token) ⇒ Object

Logout from the server



50
51
52
# File 'lib/lita/adapters/discord/api.rb', line 50

def logout(token)
	request( :post, "#{APIBASE}/auth/logout", nil, Authorization: token )
end

#raw_request(type, attributes) ⇒ Object



19
20
21
22
23
# File 'lib/lita/adapters/discord/api.rb', line 19

def raw_request(type, attributes)
    RestClient.send(type, *attributes)
	rescue RestClient::Forbidden
	raise Lita.logger.debug("The bot doesn't have the required permission to do this!")
end

#request(type, *attributes) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lita/adapters/discord/api.rb', line 25

def request(type, *attributes)
	# Add a custom user agent
    attributes.last[:user_agent] = user_agent if attributes.last.is_a? Hash
    response = raw_request(type, attributes)

    while response.code == 429
    	wait_seconds = response[:retry_after].to_i / 1000.0
    	Lita.logger.debug("WARNING: Discord rate limiting will cause a delay of #{wait_seconds} seconds for the request: #{type} #{attributes}")
    	sleep wait_seconds / 1000.0
    	response = raw_request(type, attributes)
	end
	response				
end

#user(token, user_id) ⇒ Object



58
59
60
# File 'lib/lita/adapters/discord/api.rb', line 58

def user(token, user_id)
	request( :get, "#{APIBASE}/users/#{user_id}", Authorization: token )
end

#user_agentObject



10
11
12
13
14
15
16
# File 'lib/lita/adapters/discord/api.rb', line 10

def user_agent
    # This particular string is required by the Discord devs.
    required = "lita-discord (https://github.com/kyleboe/lita-discord, v0.1.0)"
	@bot_name ||= ''

	"rest-client/#{RestClient::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} lita-discord/0.1.0 #{required} #{@bot_name}"
end

#validate_token(token) ⇒ Object



54
55
56
# File 'lib/lita/adapters/discord/api.rb', line 54

def validate_token(token)
	request( :post, "#{APIBASE}/auth/login",{}.to_json, Authorization: token, content_type: :json )
end