-1

Discord Bot List API POST stats example

Class Python 1 revisions 45 16 days ago 16 days ago
import aiohttp
import asyncio
from discord.ext import commands


class DiscordBotListAPI(commands.Cog):
    def __init__(self, bot):
        self.token = "your_token_here"
        self.updating = self.bot.loop.create_task(self.update_stats())

    async def update_stats(self):
        """This function runs every 30 minutes to automatically update your stats"""
        try:
            async with aiohttp.ClientSession() as session:
                async with session.post(
                    "https://discordbotlist.com/api/bots/606276772295868416/stats",
                    headers={"Authorization": self.dblcomtoken},
                    json={
                        "guilds": len(self.bot.guilds),
                        "users": len([*self.bot.get_all_members()]),
                    },
                ) as resp:
                    resp.raise_for_status()
                    logger.info(
                        f"Posted server count to dblcom: ({len(self.bot.guilds)})"
                    )
        except Exception as e:
            logger.exception(
                f"Failed to post server count to dblcom\n{type(e).__name__}: {e}"
            )
        await asyncio.sleep(1800)


def setup(bot):
    global logger
    logger = logging.getLogger("bot")
    bot.add_cog(DiscordBotListAPI(bot))

Simple discord.py extension for POSTing bot stats to the discordbotlist.org api. Replace your_token_here with your api token, load the extension and everything happens automagically!