├── .bundle └── config ├── .gitignore ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib ├── lib.rb └── topgg │ ├── bot.rb │ ├── botSearch.rb │ ├── stats.rb │ ├── user.rb │ ├── utils │ └── request.rb │ └── votes.rb └── topgg.gemspec /.bundle/config: -------------------------------------------------------------------------------- 1 | --- 2 | BUNDLE_CACHE_ALL: "false" 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | # Specify your gem's dependencies in topgg.gemspec 6 | gemspec 7 | 8 | gem 'rake', '~> 13.0' 9 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | topgg (1.1.0) 5 | 6 | GEM 7 | remote: https://rubygems.org/ 8 | specs: 9 | rake (13.0.6) 10 | 11 | PLATFORMS 12 | x86_64-linux 13 | 14 | DEPENDENCIES 15 | rake (~> 13.0) 16 | topgg! 17 | 18 | BUNDLED WITH 19 | 2.2.22 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 rhydderchc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 TODO: Write your name 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Top.gg](https://top.gg) Ruby 2 | 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. 5 | 6 | It provides you with numerous methods to interact with the API. 7 | ## Dependencies 8 | 9 | * Ruby 10 | 11 | ## Installation 12 | 13 | ``` bash 14 | 15 | gem install topgg 16 | 17 | ``` 18 | ## Note 19 | 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` 22 | 23 | ## Example 24 | 25 | Here's a straightforward example of how to request data with the wrapper. 26 | 27 | ```ruby 28 | require 'topgg' 29 | 30 | client = Topgg.new("AUTH_TOKEN", "BOTID") 31 | 32 | client.get_bot("1234").defAvatar 33 | # returns 34 | # "6debd47ed13483642cf09e832ed0bc1b" 35 | 36 | ``` 37 | ### Auto Posting 38 | 39 | The library provides you with autoposting functionality, and autoposts at an interval of 30 minutes. 40 | Here's how you can use it 41 | 42 | ```ruby 43 | require 'topgg' 44 | require 'discordrb' 45 | 46 | bot = Discordrb::Bot.new token: "TOKEN" 47 | 48 | client = Topgg.new("AUTH_TOKEN", "BOTID") 49 | bot.ready do |event| 50 | client.auto_post_stats(bot) # The discordrb bot client. 51 | end 52 | bot.run 53 | ``` 54 | 55 | # Documentation 56 | 57 | Check out the api reference [here](https://rubydoc.info/gems/topgg/) 58 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "bundler/gem_tasks" 4 | task default: %i[] 5 | -------------------------------------------------------------------------------- /lib/lib.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative 'topgg/utils/request' 4 | require 'json' 5 | require_relative 'topgg/bot' 6 | require_relative 'topgg/botSearch' 7 | require_relative 'topgg/user' 8 | require_relative 'topgg/stats' 9 | require_relative 'topgg/votes' 10 | # Class Topgg 11 | # The class instantiates all the methods for api requests and posts. 12 | class Topgg 13 | # initializes the class attributes. 14 | # @param token [String] The authorization token from top.gg 15 | # @param id [String] The client id of the bot. 16 | def initialize(token, id) 17 | @id = id 18 | @token = token 19 | @request = Dbl::Utils::Request.new(token) 20 | end 21 | 22 | # The method fetches bot statistics from top.gg 23 | # @param id [String] The id of the bot you want to fetch statistics from. 24 | # @return [Dbl::Bot] The Bot Object 25 | def get_bot(id) 26 | resp = @request.get("bots/#{id}") 27 | 28 | Dbl::Bot.new(resp) 29 | end 30 | 31 | # The method searches bots from top.gg using a keyword query. 32 | # @param [Object] params The parameters that can be used to query a search 33 | # To know what the parameters are check it out here 34 | # @return [Dbl::BotSearch] The BotSearch Object 35 | def search_bot(params) 36 | resp = @request.get('bots/search', params) 37 | Dbl::BotSearch.new(resp) 38 | end 39 | 40 | # Search for a user on top.gg with id 41 | # @param id [String] The id of the user to search for. 42 | # @return [Dbl::User] 43 | def user(id) 44 | resp = @request.get("users/#{id}") 45 | 46 | Dbl::User.new(resp) 47 | end 48 | 49 | # Get Bot statistics. 50 | # @param id [String] Id of the bot you want to get statistics of 51 | # @return [Dbl::Stats] 52 | def get_stats(id) 53 | resp = @request.get("bots/#{id}/stats") 54 | 55 | Dbl::Stats.new(resp) 56 | end 57 | 58 | # Mini-method to query if the bot(self) was voted by the user. 59 | # @param userid [String] The user id. 60 | # @return [Boolean] 61 | def voted?(userid) 62 | resp = @request.get("bots/#{@id}/check", { userId: userid }) 63 | 64 | ret = resp 65 | ret['voted'] == 1 66 | end 67 | 68 | # Get the Last 1000 votes of the bot(self) 69 | # @return [Dbl::Votes] 70 | def votes 71 | resp = @request.get("bots/#{@id}/votes") 72 | 73 | Dbl::Votes.new(resp) 74 | end 75 | 76 | # Post Bot Statistics to the website 77 | # @param server_count [Integer] The amount of servers the bot is in. 78 | # @param shards [Integer] The amount of servers the bot is in per shard 79 | # @param shard_count [Integer] The total number of shards. 80 | def post_stats(server_count, shards: nil, shard_count: nil) 81 | json_post = { 82 | server_count: server_count, 83 | shards: shards, 84 | shard_count: shard_count 85 | }.to_json 86 | @request.post("bots/#{@id}/stats", json_post, { 'Content-Type' => 'application/json' }) 87 | end 88 | 89 | # Auto-posts stats on to the top.gg website 90 | # @param client [Discordrb::Bot] instanceof discordrb client. 91 | def auto_post_stats(client) 92 | semaphore = Mutex.new 93 | Thread.new do 94 | semaphore.synchronize do 95 | interval 1800 do 96 | server_len = client.servers.length 97 | post_stats(server_len) 98 | print("[TOPGG] : \033[31;1;4m Bot statistics has been successfully posted!\033[0m") 99 | end 100 | end 101 | end 102 | end 103 | 104 | # Mini-method to get statistics on self 105 | # @return [get_bot] 106 | def self 107 | get_bot(@id) 108 | end 109 | 110 | def interval(seconds) 111 | loop do 112 | yield 113 | sleep seconds 114 | end 115 | end 116 | end 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /lib/topgg/bot.rb: -------------------------------------------------------------------------------- 1 | module Dbl 2 | # The Bot class spreads the json parsed hash into different methods 3 | class Bot 4 | # Initializes the Bot class 5 | # @param obj [Object] 6 | def initialize(obj) 7 | @obj = obj 8 | end 9 | # Returns raw hash of the parsed json object 10 | # @return [Hash] 11 | attr_reader :obj 12 | 13 | alias :raw :obj 14 | 15 | alias data obj 16 | # Returns error message, if there's an error 17 | # @return [String] 18 | def error 19 | @obj['error'].to_str 20 | end 21 | 22 | # Returns the default Avatar of the client 23 | # @return [String] 24 | def defAvatar 25 | @obj['defAvatar'].to_str 26 | end 27 | 28 | # Returns the invite link of the bot 29 | # @return [String] 30 | def invite 31 | @obj['invite'].to_str 32 | end 33 | 34 | # Returns the bot website, if configured 35 | # @return [String] 36 | def website 37 | @obj['website'].to_str 38 | end 39 | 40 | # Returns support server link 41 | # @return [String] 42 | def support 43 | "https://discord.gg/#{@obj['support']}" 44 | end 45 | 46 | # Returns github repository link, if any 47 | # @return [String] 48 | def github 49 | @obj['github'].to_str 50 | end 51 | 52 | # Returns the long Description of the bot 53 | # @return [String] 54 | def longdesc 55 | @obj['longdesc'].to_str 56 | end 57 | 58 | # Returns the short description of the bot 59 | # @return [String] 60 | def shortdesc 61 | @obj['shortdesc'].to_str 62 | end 63 | 64 | # Returns the default prefix of the bot 65 | # @return [String] 66 | def prefix 67 | @obj['prefix'].to_str 68 | end 69 | 70 | # Returns the bot library 71 | # @return [String] 72 | def lib 73 | @obj['lib'].to_str 74 | end 75 | 76 | # Returns the bot client id 77 | # @return [String] 78 | def clientid 79 | @obj['clientid'].to_str 80 | end 81 | 82 | # Returns the avatar link of the bot 83 | # @return [String] 84 | def avatar 85 | "https://cdn.discordapp.com/avatars/#{@obj['id']}/#{@obj['avatar']}.webp?size=1024" 86 | end 87 | 88 | # Returns the bot id 89 | # @return [String] 90 | def id 91 | @obj['id'].to_str 92 | end 93 | 94 | # Returns the bot descriminator 95 | # @return [String] 96 | def descriminator 97 | @obj['descriminator'].to_str 98 | end 99 | 100 | # Returns the bot username 101 | # @return [String] 102 | def username 103 | @obj['username'].to_str 104 | end 105 | 106 | # Returns the date on which the bot was submitted 107 | # @return [Date] 108 | def date 109 | Date.parse(@obj['date']) 110 | end 111 | 112 | # Returns the server count of the bot 113 | # @return [Integer] 114 | def server_count 115 | @obj['server_count'].to_i 116 | end 117 | 118 | # Returns the amount of shards 119 | # @return [String] 120 | def shard_count 121 | @obj['shard_count'].to_str 122 | end 123 | 124 | # Returns configured guilds in which the bot is in 125 | # @return [String] 126 | def guilds 127 | @obj['guilds'].to_str 128 | end 129 | 130 | # Returns the amount of guilds per shard of the bot 131 | # @return [String] 132 | def shards 133 | @obj['shards'].to_str 134 | end 135 | 136 | # Returns the monthyPoints of the bot 137 | # @return [String] 138 | def monthlyPoints 139 | @obj['monthlyPoints'].to_str 140 | end 141 | 142 | # Returns the total points of the bot 143 | # @return [String] 144 | def points 145 | @obj['points'].to_str 146 | end 147 | 148 | # Returns true/false depending on if the bot is certified or not 149 | # @return [Boolean] 150 | def certifiedBot 151 | @obj['certifiedBot'].to_str 152 | end 153 | 154 | # Returns the owner ids 155 | # @return [Array] 156 | def owners 157 | @obj['owners'].to_str 158 | end 159 | 160 | # Return the bot tags 161 | # @return [Array] 162 | def tags 163 | @obj['tags'].to_str 164 | end 165 | 166 | # Returns the bot banner url 167 | # @return [String] 168 | def bannerUrl 169 | @obj['bannerUrl'].to_str 170 | end 171 | 172 | # Returns the donate bot guild ID 173 | # @return [String] 174 | def donatebotguildid 175 | @obj['donatebotguildid'].to_str 176 | end 177 | end 178 | end 179 | -------------------------------------------------------------------------------- /lib/topgg/botSearch.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Dbl 3 | # This class spreads the BotSearch Response body into different methods 4 | class BotSearch 5 | # Initalizes the BotSearch class 6 | # @param obj [Object] Response Hash 7 | def initialize(obj) 8 | @obj = obj 9 | end 10 | # Get raw hash response 11 | # @return [Hash] 12 | attr_reader :obj 13 | 14 | alias raw obj 15 | 16 | alias data obj 17 | 18 | # The Total number of results 19 | # @return [Integer] 20 | def total 21 | @obj['total'].to_i 22 | end 23 | 24 | # The first result 25 | # @return [Spreader::Bot] 26 | def first 27 | Spreader::Bot.new(@obj['results'][0]) 28 | end 29 | 30 | # The number of bots shown in the first page 31 | # @return [Integer] 32 | def count 33 | @obj['count'].to_i 34 | end 35 | 36 | # Iterates through the results 37 | # @return [Array] 38 | def results 39 | arr = [] 40 | flag = 0 # iteration flag 41 | @obj['results'].each do |data| 42 | arr[flag] = Spreader::Bot.new(data) 43 | flag += 1 44 | end 45 | arr 46 | end 47 | 48 | # Length of the results. 49 | # @return [Integer] 50 | def size 51 | @obj['results'].length 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/topgg/stats.rb: -------------------------------------------------------------------------------- 1 | module Dbl 2 | # The Stats class spreads the json response into different methods 3 | class Stats 4 | # Initializes the Stats class 5 | # @param obj [Object] Response Hash 6 | def initialize(obj) 7 | @obj = obj 8 | end 9 | 10 | # Returns raw Hash of the response 11 | attr_reader :obj 12 | 13 | alias raw obj 14 | 15 | alias data obj 16 | # Returns the server Count of the bot 17 | # @return [Integer] 18 | def server_count 19 | @obj['server_count'] 20 | end 21 | 22 | # The amount of servers per shard 23 | # @return [Integer] 24 | def shards 25 | @obj['shards'] 26 | end 27 | 28 | # Returns the total number of shards 29 | # @return [Integer] 30 | def shard_count 31 | @obj['shard_count'] 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/topgg/user.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Dbl 3 | # The User class, used for Spreading the response body into different methods 4 | class User 5 | # Instantiates the class variables 6 | # @param obj [Object] Response Data Object 7 | def initialize(obj) 8 | @obj = obj 9 | end 10 | 11 | attr_reader :obj 12 | 13 | alias raw obj 14 | 15 | alias data obj 16 | # Check for errors, if any 17 | def error 18 | @obj['error'] 19 | end 20 | 21 | # The Id of the user 22 | def id 23 | @obj['id'] 24 | end 25 | 26 | # The username of the user 27 | def username 28 | @obj['username'] 29 | end 30 | 31 | # The avatar of the user 32 | # @return [String] 33 | def avatar 34 | "https://cdn.discordapp.com/avatars/#{@obj['id']}/#{@obj['avatar']}.webp?size=1024" 35 | end 36 | 37 | # The default avatar of the user 38 | # @return [String] 39 | def defAvatar 40 | @obj['defAvatar'] 41 | end 42 | 43 | # Returns true/false depending upon if the user is a moderator or not. 44 | # @return [Boolean] 45 | def mod 46 | @obj['mod'] 47 | end 48 | 49 | # Returns true/false depending upon if the user is a supporter or not. 50 | # @return [Boolean] 51 | def supporter 52 | @obj['supporter'] 53 | end 54 | 55 | # Returns true/false depending upon if the user is a certified developer or not. 56 | # @return [Boolean] 57 | def certifiedDev 58 | @obj['certifiedDev'] 59 | end 60 | 61 | # Returns an object containing all user social integrations. 62 | # @return [Object] 63 | def social 64 | @obj['social'] 65 | end 66 | 67 | # Returns true/false depending on weather the user is an admin or not. 68 | # @return [Boolean] 69 | def admin 70 | @obj['admin'] 71 | end 72 | 73 | # Returns true/false depending on weather the user is a website Moderator or not. 74 | # @return [Boolean] 75 | def webMod 76 | @obj['webMod'] 77 | end 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /lib/topgg/utils/request.rb: -------------------------------------------------------------------------------- 1 | require 'net/http' 2 | require 'json' 3 | 4 | module Dbl 5 | module Utils 6 | class Request 7 | def initialize(token) 8 | @token = token 9 | @url = "https://top.gg/api" 10 | end 11 | 12 | 13 | def get(params) 14 | 15 | uri = URI.parse(@url+"/#{params}") 16 | http = Net::HTTP.new(uri.host, uri.port) 17 | http.use_ssl = true 18 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE 19 | 20 | request = Net::HTTP::Get.new(uri.request_uri) 21 | request.add_field 'Authorization', @token 22 | 23 | response = http.request(request) 24 | JSON.parse(response.body) 25 | end 26 | 27 | def post(params, data) 28 | 29 | 30 | uri = URI.parse(@url+"/#{params}") 31 | http = Net::HTTP.new(uri.host, uri.port) 32 | http.use_ssl = true 33 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE 34 | 35 | request = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json', 'Authorization' => @token}) 36 | request.body = data.to_json 37 | 38 | response = http.request(request) 39 | JSON.parse(response.body) 40 | end 41 | end 42 | end 43 | end -------------------------------------------------------------------------------- /lib/topgg/votes.rb: -------------------------------------------------------------------------------- 1 | module Dbl 2 | # This class Spreads the Vote response body into different methods. 3 | class Votes 4 | # Initializes the votes class 5 | # @param obj [Object] JSON parsed object 6 | def initialize(obj) 7 | @obj = obj 8 | end 9 | 10 | # Get raw hash return of the object 11 | # @return [Hash] 12 | attr_reader :obj 13 | 14 | alias raw obj 15 | 16 | alias data obj 17 | # Get the first vote amongst all the other votes. 18 | # @return [Spreader::User] 19 | def first 20 | Spreader::User.new(@obj[0]) 21 | end 22 | 23 | # Get the total number of votes 24 | # @return [Integer] 25 | def total 26 | @obj.length 27 | end 28 | end 29 | end -------------------------------------------------------------------------------- /topgg.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Gem::Specification.new do |spec| 4 | spec.name = 'topgg' 5 | spec.version = '1.1.0' 6 | spec.authors = ['Adonis Tremblay'] 7 | spec.email = ['rhydderchc@gmail.com'] 8 | 9 | spec.summary = 'A top.gg api wrapper for ruby.' 10 | spec.description = 'This is a ruby library of the top.gg API.' 11 | spec.homepage = 'https://github.com/rhydderchc/topgg-ruby' 12 | spec.license = 'MIT' 13 | spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0') 14 | 15 | spec.metadata['allowed_push_host'] = "https://rubygems.org" 16 | 17 | spec.metadata['homepage_uri'] = spec.homepage 18 | spec.metadata['source_code_uri'] = 'https://github.com/rhydderchc/topgg-ruby' 19 | spec.metadata['changelog_uri'] = 'https://github.com/rhydderchc/CHANGELOG.md' 20 | 21 | # Specify which files should be added to the gem when it is released. 22 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 23 | spec.files = Dir.chdir(File.expand_path(__dir__)) do 24 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) } 25 | end 26 | spec.bindir = 'exe' 27 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 28 | spec.require_paths = ['lib'] 29 | 30 | # Uncomment to register a new dependency of your gem 31 | # spec.add_dependency "example-gem", "~> 1.0" 32 | 33 | # For more information and examples about making a new gem, checkout our 34 | # guide at: https://bundler.io/guides/creating_gem.html 35 | end 36 | --------------------------------------------------------------------------------