Top.gg Documentation

/

Python

Official Top.gg Python library


Python Library

This is our official Python Library for top.gg, if you have any issues please submit an issue on our github.

Installation

pip install topggpy

Install from source

git clone https://github.com/top-gg/python-sdk/
cd python-sdk
pip install -R requirements.txt

Examples

Manual server count post every 30 minutes:

from discord.ext import tasks

import topgg

# This example uses tasks provided by discord.ext to create a task that posts guild count to Top.gg every 30 minutes.

dbl_token = "Top.gg token"  # set this to your bot's Top.gg token
bot.topggpy = topgg.DBLClient(bot, dbl_token)


@tasks.loop(minutes=30)
async def update_stats():
    """This function runs every 30 minutes to automatically update your server count."""
    try:
        await bot.topggpy.post_guild_count()
        print(f"Posted server count ({bot.topggpy.guild_count})")
    except Exception as e:
        print(f"Failed to post server count\n{e.__class__.__name__}: {e}")


update_stats.start()

Webhook:

import topgg

# This example uses topggpy's webhook system.
bot.topgg_webhook = topgg.WebhookManager(bot).dbl_webhook("/dblwebhook", "password")

# The port must be a number between 1024 and 49151.
bot.topgg_webhook.run(5000)  # this method can be awaited as well


@bot.event
async def on_dbl_vote(data):
    """An event that is called whenever someone votes for the bot on Top.gg."""
    if data["type"] == "test":
        # this is roughly equivalent to
        # `return await on_dbl_test(data)` in this case
        return bot.dispatch("dbl_test", data)

    print(f"Received a vote:\n{data}")


@bot.event
async def on_dbl_test(data):
    """An event that is called whenever someone tests the webhook system for your bot on Top.gg."""
    print(f"Received a test vote:\n{data}")

Automatically post server count every 30 minutes:

import topgg

# This example uses topggpy's autopost feature to post guild count to Top.gg every 30 minutes
# as well as the shard count if applicable.

dbl_token = "Top.gg token"  # set this to your bot's Top.gg token
bot.topggpy = topgg.DBLClient(bot, dbl_token, autopost=True, post_shard_count=True)


@bot.event
async def on_autopost_success():
    print(
        f"Posted server count ({bot.topggpy.guild_count}), shard count ({bot.shard_count})"
    )
Edit on GitHub