Class: Topgg

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

Overview

The class instantiates all the methods for api requests and posts.

Instance Method Summary collapse

Constructor Details

#initialize(token, id) ⇒ Topgg

initializes the class attributes.

Parameters:

  • token (String)

    The authorization token from top.gg

  • id (String)

    The client id of the bot.



16
17
18
19
20
# File 'lib/lib.rb', line 16

def initialize(token, id)
  @id = id
  @token = token
  @request = Dbl::Utils::Request.new(token)
end

Instance Method Details

#auto_post_stats(client) ⇒ Object

Auto-posts stats on to the top.gg website

Parameters:

  • client (Discordrb::Bot)

    instanceof discordrb client.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lib.rb', line 91

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

#get_bot(id) ⇒ Dbl::Bot

The method fetches bot statistics from top.gg

Parameters:

  • id (String)

    The id of the bot you want to fetch statistics from.

Returns:



25
26
27
28
29
# File 'lib/lib.rb', line 25

def get_bot(id)
  resp = @request.get("bots/#{id}")

  Dbl::Bot.new(resp)
end

#get_stats(id) ⇒ Dbl::Stats

Get Bot statistics.

Parameters:

  • id (String)

    Id of the bot you want to get statistics of

Returns:



52
53
54
55
56
# File 'lib/lib.rb', line 52

def get_stats(id)
  resp = @request.get("bots/#{id}/stats")

  Dbl::Stats.new(resp)
end

#interval(seconds) ⇒ Object



110
111
112
113
114
115
# File 'lib/lib.rb', line 110

def interval(seconds)
  loop do
    yield
     sleep seconds
  end
end

#post_stats(server_count, shards: nil, shard_count: nil) ⇒ Object

Post Bot Statistics to the website

Parameters:

  • server_count (Integer)

    The amount of servers the bot is in.

  • shards (Integer) (defaults to: nil)

    The amount of servers the bot is in per shard

  • shard_count (Integer) (defaults to: nil)

    The total number of shards.



80
81
82
83
84
85
86
87
# File 'lib/lib.rb', line 80

def post_stats(server_count, shards: nil, shard_count: nil)
  json_post = {
    server_count: server_count,
    shards: shards,
    shard_count: shard_count
  }.to_json
  @request.post("bots/#{@id}/stats", json_post, { 'Content-Type' => 'application/json' })
end

#search_bot(params) ⇒ Dbl::BotSearch

The method searches bots from top.gg using a keyword query. To know what the parameters are check it out here

Parameters:

  • params (Object)

    The parameters that can be used to query a search

Returns:



35
36
37
38
# File 'lib/lib.rb', line 35

def search_bot(params)
  resp = @request.get('bots/search', params)
  Dbl::BotSearch.new(resp)
end

#selfget_bot

Mini-method to get statistics on self

Returns:



106
107
108
# File 'lib/lib.rb', line 106

def self
  get_bot(@id)
end

#user(id) ⇒ Dbl::User

Search for a user on top.gg with id

Parameters:

  • id (String)

    The id of the user to search for.

Returns:



43
44
45
46
47
# File 'lib/lib.rb', line 43

def user(id)
  resp = @request.get("users/#{id}")

  Dbl::User.new(resp)
end

#voted?(userid) ⇒ Boolean

Mini-method to query if the bot(self) was voted by the user.

Parameters:

  • userid (String)

    The user id.

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/lib.rb', line 61

def voted?(userid)
  resp = @request.get("bots/#{@id}/check", { userId: userid })

  ret = resp
  ret['voted'] == 1
end

#votesDbl::Votes

Get the Last 1000 votes of the bot(self)

Returns:



70
71
72
73
74
# File 'lib/lib.rb', line 70

def votes
  resp = @request.get("bots/#{@id}/votes")

  Dbl::Votes.new(resp)
end