📖 In this guide I will explain and display how to setup top.gg api for your bot on topgg in case you're stuck somewhere. After this guide, you will be able to;
- POST server count
- GET bot info, server count, upvote info
- GET data user data whenever your bot is upvoted etc.
-
First you need to setup your IP address in
webhooks
section of your bot. The IP needs to be in formathttp://ip:port/dblwebhook
, Example:http://50.50.00.12:5000/dblwebhook
. Make sure your code is running on the IP you provided. -
After your IP is ready. Create an authorization password which you will use in your code to identify requests coming from topgg. This may sound complicated but the library we are going to use will manage it for us, We simply need to mention the password.
-
Now you need to generate a top.gg token for your bot. You may do it by clicking the button here. Options could be different for you if you have not generated the token before.
I will be using topggpy for this tutorial.
- While I was migrating to topggpy from dblpy, I faced issues with it's pypi installation which were fixed when I installed from source. It has been a while so I'm unsure if it's fixed. Nevertheless, we will be using source installation for this tutorial.
pip3 install git+https://github.com/top-gg/python-sdk/
-
We need few extra files created in our code to get the library working. Official Example is available here.
-
From the example, copy the callbacks directory and its files into your code.
-
In
callbacks/webhook.py
replaceyoushallnotpass
from line 29 with your authorization password. In our case, we usedtesting
as our password, therefore our code will look like this;
import topgg
# this can be async too!
@topgg.endpoint("/dblwebhook", topgg.WebhookType.BOT, "testing")
def endpoint(
vote_data: topgg.BotVoteData,
# uncomment this if you want to get access to client
# client: discord.Client = topgg.data(discord.Client),
):
# this function will be called whenever someone votes for your bot.
print("Received a vote!", vote_data)
# do anything with client here
# client.dispatch("dbl_vote", vote_data)
This concludes the extra files you needed for the setup.
This code needs to be used in your main file.
import discord
import topgg
from .callbacks import autopost, webhook
client = discord.Client()
webhook_manager = topgg.WebhookManager().set_data(client).endpoint(webhook.endpoint)
dblclient = topgg.DBLClient("TOPGG_TOKEN").set_data(client)
autoposter: topgg.AutoPoster = (
dblclient.autopost()
.on_success(autopost.on_autopost_success)
.on_error(autopost.on_autopost_error)
.stats(autopost.stats)
)
@client.event
async def on_ready():
assert client.user is not None
dblclient.default_bot_id = client.user.id
# if it's ready, then the event loop's run,
# hence it's safe starting the autopost here
if not autoposter.is_running:
# don't await unless you want to wait for the autopost loop to get finished
autoposter.start()
# we can also start the webhook here
if not webhook_manager.is_running:
await webhook_manager.start(PORT)
- First, You need to replace
TOPGG_TOKEN
with your top.gg token. - Second, You need to replace
PORT
with your port which you used along with your IP in the webhook setup. In our case, it is 5000. Therefore line 63 will look like this;
await webhook_manager.start(5000)
This concludes the setup. You may now go ahead and make changes in callbacks/webhook.py
to fit your needs. Server Count posting should also start working when you run the code.
In this guide I explained how to setup top.gg api in your bot. I hope you found it helpful.
In case you still need help, You may contact me in my discord server.