Skip to content

Commit a3a80a2

Browse files
committed
[feat,doc]: add error handling and documentation tweaks
1 parent 8a2035b commit a3a80a2

File tree

6 files changed

+292
-73
lines changed

6 files changed

+292
-73
lines changed

README.md

Lines changed: 195 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,220 @@
1-
# [Top.gg](https://top.gg) Ruby
1+
# Top.gg Ruby SDK
2+
3+
The community-maintained Ruby library for Top.gg.
4+
5+
## Chapters
6+
7+
- [Installation](#installation)
8+
- [Setting up](#setting-up)
9+
- [Usage](#usage)
10+
- [API v1](#api-v1)
11+
- [Getting your project's vote information of a user](#getting-your-projects-vote-information-of-a-user)
12+
- [Posting your bot's application commands list](#posting-your-bots-application-commands-list)
13+
- [API v0](#api-v0)
14+
- [Getting a bot](#getting-a-bot)
15+
- [Getting several bots](#getting-several-bots)
16+
- [Getting your project's voters](#getting-your-projects-voters)
17+
- [Check if a user has voted for your project](#check-if-a-user-has-voted-for-your-project)
18+
- [Getting your bot's statistics](#getting-your-bots-statistics)
19+
- [Posting your bot's statistics](#posting-your-bots-statistics)
20+
- [Automatically posting your bot's statistics every few minutes](#automatically-posting-your-bots-statistics-every-few-minutes)
21+
- [Checking if the weekend vote multiplier is active](#checking-if-the-weekend-vote-multiplier-is-active)
22+
- [Generating widget URLs](#generating-widget-urls)
23+
- [Webhooks](#webhooks)
24+
- [Being notified whenever someone voted for your project](#being-notified-whenever-someone-voted-for-your-project)
225

3-
The Top.gg Ruby SDK is a lighweight package, that allows you
4-
to fetch data from the top.gg api and post data such as statistics to the website.
26+
## Installation
527

6-
It provides you with numerous methods to interact with the API.
7-
## Dependencies
28+
```sh
29+
$ gem install topgg
30+
```
831

9-
* Ruby
32+
## Setting up
1033

11-
## Installation
34+
### API v1
35+
36+
> **NOTE**: API v1 also includes API v0.
37+
38+
```rb
39+
require "topgg"
40+
41+
client = V1Topgg.new(ENV["TOPGG_TOKEN"])
42+
```
43+
44+
### API v0
45+
46+
```rb
47+
require "topgg"
1248

13-
``` bash
49+
client = Topgg.new(ENV["TOPGG_TOKEN"])
50+
```
51+
52+
## Usage
53+
54+
### API v1
1455

15-
gem install topgg
56+
#### Getting your project's vote information of a user
1657

58+
```rb
59+
vote = client.vote("661200758510977084")
1760
```
18-
## Note
1961

20-
You require a Token to interact with the Api.
21-
The token can be found at `https://top.gg/bot/[YOUR_BOT_ID]/webhooks`
62+
#### Posting your bot's application commands list
2263

23-
## Example
64+
##### Discordrb
2465

25-
Here's a straightforward example of how to request data with the wrapper.
66+
```rb
67+
commands = bot.rest.api.get_global_application_commands(bot.application_id).to_json
2668

27-
```ruby
28-
require 'topgg'
69+
client.post_commands(commands)
70+
```
2971

30-
client = Topgg.new("AUTH_TOKEN", "BOTID")
72+
##### Raw
3173

32-
client.get_bot("1234").defAvatar
33-
# returns
34-
# "6debd47ed13483642cf09e832ed0bc1b"
74+
```rb
75+
commands = "[{\"options\":[],\"name\":\"test\",\"name_localizations\":null,\"description\":\"command description\",\"description_localizations\":null,\"contexts\":[],\"default_permission\":null,\"default_member_permissions\":null,\"dm_permission\":false,\"integration_types\":[],\"nsfw\":false}]"
3576

77+
client.post_commands(commands)
3678
```
37-
### Auto Posting
3879

39-
The library provides you with autoposting functionality, and autoposts at an interval of 30 minutes.
40-
Here's how you can use it
80+
### API v0
4181

42-
```ruby
43-
require 'topgg'
44-
require 'discordrb'
82+
#### Getting a bot
4583

46-
bot = Discordrb::Bot.new token: "TOKEN"
84+
```rb
85+
bot = client.get_bot("264811613708746752")
86+
```
4787

48-
client = Topgg.new("AUTH_TOKEN", "BOTID")
49-
bot.ready do |event|
50-
client.auto_post_stats(bot) # The discordrb bot client.
88+
#### Getting several bots
89+
90+
```rb
91+
bots = client.search_bot({ sort: "id", limit: 50, offset: 0 })
92+
93+
for bot in bots.results do
94+
puts bot.username
5195
end
96+
```
97+
98+
#### Getting your project's voters
99+
100+
##### First page
101+
102+
```rb
103+
voters = client.votes
104+
105+
for voter in voters.results do
106+
puts voter.username
107+
end
108+
```
109+
110+
##### Subsequent pages
111+
112+
```rb
113+
voters = client.votes(2)
114+
115+
for voter in voters.results do
116+
puts voter.username
117+
end
118+
```
119+
120+
#### Check if a user has voted for your project
121+
122+
```rb
123+
has_voted = client.voted?("8226924471638491136")
124+
```
125+
126+
#### Getting your bot's statistics
127+
128+
```rb
129+
stats = client.get_stats
130+
```
131+
132+
#### Posting your bot's statistics
133+
134+
```rb
135+
client.post_stats(bot.server_count)
136+
```
137+
138+
#### Automatically posting your bot's statistics every few minutes
139+
140+
With Discordrb:
141+
142+
```rb
143+
require "discordrb"
144+
145+
bot = Discordrb::Bot.new(token: env["BOT_TOKEN"], intents: [:servers])
146+
147+
bot.ready do |event|
148+
client.auto_post_stats(bot)
149+
150+
puts("Bot is now ready!")
151+
end
152+
52153
bot.run
53154
```
54155

55-
# Documentation
156+
#### Checking if the weekend vote multiplier is active
157+
158+
```rb
159+
is_weekend = client.is_weekend?
160+
```
161+
162+
#### Generating widget URLs
163+
164+
##### Large
165+
166+
```rb
167+
widget_url = Dbl::Widget.large(:discord_bot, "574652751745777665")
168+
```
169+
170+
##### Votes
171+
172+
```rb
173+
widget_url = Dbl::Widget.votes(:discord_bot, "574652751745777665")
174+
```
175+
176+
##### Owner
177+
178+
```rb
179+
widget_url = Dbl::Widget.owner(:discord_bot, "574652751745777665")
180+
```
181+
182+
##### Social
183+
184+
```rb
185+
widget_url = Dbl::Widget.social(:discord_bot, "574652751745777665")
186+
```
187+
188+
### Webhooks
189+
190+
#### Being notified whenever someone voted for your project
56191

57-
Check out the api reference [here](https://rubydoc.info/gems/topgg/)
192+
##### Ruby on Rails
193+
194+
In your `config/application.rb`:
195+
196+
```rb
197+
module MyServer
198+
class Application < Rails::Application
199+
# ...
200+
201+
config.middleware.use Dbl::Webhook,
202+
type: Dbl::Webhook::VOTE,
203+
path: "/votes",
204+
auth: ENV["MY_TOPGG_WEBHOOK_SECRET"] do |vote|
205+
Rails.logger.info "A user with the ID of #{vote.voter_id} has voted us on Top.gg!"
206+
end
207+
end
208+
end
209+
```
210+
211+
##### Sinatra
212+
213+
```rb
214+
use Dbl::Webhook,
215+
type: Dbl::Webhook::VOTE,
216+
path: "/votes",
217+
auth: ENV["MY_TOPGG_WEBHOOK_SECRET"] do |vote|
218+
puts "A user with the ID of #{vote.voter_id} has voted us on Top.gg!"
219+
end
220+
```

lib/lib.rb

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
require_relative "topgg/utils/request"
1515

1616
# Class Topgg
17-
# The class instantiates all the methods for API requests and posts.
17+
# Top.gg API v0 client
1818
class Topgg
19-
# initializes the class attributes.
20-
# @param token [String] The authorization token from top.gg
19+
# Initializes the client
20+
# @param token [String] Your Top.gg API token
2121
def initialize(token)
2222
begin
2323
token_section = token.split('.')[1]
@@ -35,14 +35,14 @@ def initialize(token)
3535
@request = Dbl::Utils::Request.new(token)
3636
end
3737

38-
# Fetches bot statistics from top.gg
38+
# Get Discord bot statistics from top.gg
3939
# @param id [String] The bot's ID
4040
# @return [Dbl::Bot] The Bot Object
4141
def get_bot(id)
4242
Dbl::Bot.new(@request.get("bots/#{id}"))
4343
end
4444

45-
# Searches bots from Top.gg using a keyword query.
45+
# Searches Discord bots from Top.gg using a keyword query.
4646
# @param [Object] params The parameters that can be used to query a search
4747
# To know what the parameters are check it out here
4848
# @return [Dbl::BotSearch] The BotSearch Object
@@ -61,9 +61,7 @@ def user(id)
6161
# @param id [String] The bot's ID. Unused, no longer has an effect.
6262
# @return [Dbl::Stats]
6363
def get_stats(id = nil)
64-
resp = @request.get("bots/stats")
65-
66-
Dbl::Stats.new(resp)
64+
Dbl::Stats.new(@request.get("bots/stats"))
6765
end
6866

6967
# Mini-method to query if your project was voted by the user in the past 12 hours.
@@ -87,12 +85,11 @@ def is_weekend?
8785
def votes(page = 1)
8886
page = page.to_i
8987
page = 1 if page < 1
90-
resp = @request.get("bots/#{@id}/votes", { page: page })
9188

92-
Dbl::Votes.new(resp)
89+
Dbl::Votes.new(@request.get("bots/#{@id}/votes", { page: page }))
9390
end
9491

95-
# Posts your Discord bot's server count to the API. This will update the server count in your bot's Top.gg page.
92+
# Posts your Discord bot's statistics to the API. This will update the statistics in your Discord bot's Top.gg page.
9693
# @param server_count [Integer] The amount of servers the bot is in. Must not be less than 1.
9794
# @param shards [Integer] The amount of servers the bot is in per shard. Unused, no longer has an effect.
9895
# @param shard_count [Integer] The total number of shards. Unused, no longer has an effect.
@@ -106,7 +103,7 @@ def post_stats(server_count, shards = nil, shard_count = nil)
106103
)
107104
end
108105

109-
# Auto-posts stats to the Top.gg website
106+
# Auto-posts your Discord bot's stats to the Top.gg website
110107
# @param client [Discordrb::Bot] instanceof discordrb client.
111108
def auto_post_stats(client)
112109
semaphore = Mutex.new
@@ -136,3 +133,27 @@ def interval(seconds)
136133
end
137134
end
138135
end
136+
137+
# Top.gg API v1 client
138+
class V1Topgg < Topgg
139+
# Updates the application commands list in your Discord bot's Top.gg page.
140+
# @param commands [String] A list of application commands in raw Discord API JSON objects. This cannot be empty.
141+
def post_commands(commands)
142+
@request.post("v1/projects/@me/commands", commands)
143+
end
144+
145+
# Get the latest vote information of a Top.gg user on your project.
146+
# @param id [String] The user's ID.
147+
# @param source [String] The ID type to use. Defaults to "discord".
148+
# @return [Dbl::Vote]
149+
def vote(id, source = "discord")
150+
raise ArgumentError, "source must be either \"discord\" or \"topgg\"" unless source == "discord" or source == "topgg"
151+
152+
begin
153+
Dbl::Vote.new(@request.get("v1/projects/@me/votes/#{id}", { source: source }))
154+
rescue Net::HTTPError => err
155+
return nil if err.response.code == "404"
156+
raise
157+
end
158+
end
159+
end

lib/topgg/utils/request.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def get(params, query=nil)
2424
request.add_field "Authorization", "Bearer #{@token}"
2525

2626
response = http.request(request)
27+
raise Net::HTTPError.new("Top.gg HTTP error: #{response.code}", response) unless response.code.start_with?('2')
28+
2729
JSON.parse(response.body)
2830
end
2931

0 commit comments

Comments
 (0)