|
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) |
2 | 25 |
|
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 |
5 | 27 |
|
6 | | -It provides you with numerous methods to interact with the API. |
7 | | -## Dependencies |
| 28 | +```sh |
| 29 | +$ gem install topgg |
| 30 | +``` |
8 | 31 |
|
9 | | -* Ruby |
| 32 | +## Setting up |
10 | 33 |
|
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" |
12 | 48 |
|
13 | | -``` bash |
| 49 | +client = Topgg.new(ENV["TOPGG_TOKEN"]) |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +### API v1 |
14 | 55 |
|
15 | | -gem install topgg |
| 56 | +#### Getting your project's vote information of a user |
16 | 57 |
|
| 58 | +```rb |
| 59 | +vote = client.vote("661200758510977084") |
17 | 60 | ``` |
18 | | -## Note |
19 | 61 |
|
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 |
22 | 63 |
|
23 | | -## Example |
| 64 | +##### Discordrb |
24 | 65 |
|
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 |
26 | 68 |
|
27 | | -```ruby |
28 | | -require 'topgg' |
| 69 | +client.post_commands(commands) |
| 70 | +``` |
29 | 71 |
|
30 | | -client = Topgg.new("AUTH_TOKEN", "BOTID") |
| 72 | +##### Raw |
31 | 73 |
|
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}]" |
35 | 76 |
|
| 77 | +client.post_commands(commands) |
36 | 78 | ``` |
37 | | -### Auto Posting |
38 | 79 |
|
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 |
41 | 81 |
|
42 | | -```ruby |
43 | | -require 'topgg' |
44 | | -require 'discordrb' |
| 82 | +#### Getting a bot |
45 | 83 |
|
46 | | -bot = Discordrb::Bot.new token: "TOKEN" |
| 84 | +```rb |
| 85 | +bot = client.get_bot("264811613708746752") |
| 86 | +``` |
47 | 87 |
|
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 |
51 | 95 | 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 | + |
52 | 153 | bot.run |
53 | 154 | ``` |
54 | 155 |
|
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 |
56 | 191 |
|
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 | +``` |
0 commit comments