class Topgg
Class Topgg
The class instantiates all the methods for api requests and posts.
Public Class Methods
initializes the class attributes. @param token [String] The authorization token from top.gg @param id [String] The client id of the bot.
# File lib/lib.rb, line 16 def initialize(token, id) @id = id @token = token @conn = Faraday.new( url: 'https://top.gg/api', headers: { 'authorization' => @token } ) end
Public Instance Methods
Auto-posts stats on to the top.gg website @param client [Discordrb::Bot] instanceof discordrb client.
# File lib/lib.rb, line 94 def auto_post_stats(client) semaphore = Mutex.new Thread.new do semaphore.synchronize do interval 1800 do server_len = client.servers.length post_stats(server_len) print("[TOPGG] : \033[31;1;4m Bot statistics has been successfully posted!\033[0m") end end end end
The method fetches bot statistics from top.gg @param id [String] The id of the bot you want to fetch statistics from. @return [Spreader::Bot] The Bot Object
# File lib/lib.rb, line 28 def get_bot(id) resp = @conn.get("bots/#{id}") Spreader::Bot.new(JSON.parse(resp.body)) end
Get Bot statistics. @param id [String] Id of the bot you want to get statistics of @return [Spreader::Stats]
# File lib/lib.rb, line 55 def get_stats(id) resp = @conn.get("bots/#{id}/stats") Spreader::Stats.new(JSON.parse(resp.body)) end
# File lib/lib.rb, line 113 def interval(seconds) loop do yield sleep seconds end end
Post Bot Statistics to the website @param server_count [Integer] The amount of servers the bot is in. @param shards [Integer] The amount of servers the bot is in per shard @param shard_count [Integer] The total number of shards.
# File lib/lib.rb, line 83 def post_stats(server_count, shards: nil, shard_count: nil) json_post = { server_count: server_count, shards: shards, shard_count: shard_count }.to_json @conn.post("bots/#{@id}/stats", json_post, { 'Content-Type' => 'application/json' }) end
The method searches bots from top.gg using a keyword query. @param [Object] params The parameters that can be used to query a search To know what the parameters are check it out here @return [Spreader::BotSearch] The BotSearch Object
# File lib/lib.rb, line 38 def search_bot(params) resp = @conn.get('bots', params) Spreader::BotSearch.new(JSON.parse(resp.body)) end
Mini-method to get statistics on self @return [get_bot]
# File lib/lib.rb, line 109 def self get_bot(@id) end
Search for a user on top.gg with id @param id [String] The id of the user to search for. @return [Spreader::User]
# File lib/lib.rb, line 46 def user(id) resp = @conn.get("users/#{id}") Spreader::User.new(JSON.parse(resp.body)) end
Mini-method to query if the bot(self) was voted by the user. @param userid [String] The user id. @return [Boolean]
# File lib/lib.rb, line 64 def voted?(userid) resp = @conn.get("bots/#{@id}/check", { userId: userid }) ret = JSON.parse(resp.body) ret['voted'] == 1 end
Get the Last 1000 votes of the bot(self) @return [Spreader::Votes]
# File lib/lib.rb, line 73 def votes resp = @conn.get("bots/#{@id}/votes") Spreader::Votes.new(JSON.parse(resp.body)) end