0

POST to https://discordbotlist.com/ API using discord.py and Requests

Free lines Python 3 revisions 132 2 months ago 2 months ago
import logger
import asyncio
import requests

@client.event
async def on_ready():
    global logger
    logger = logging.getLogger('client')
    client.add_cog(DiscordBotsListComAPI(client))

class DiscordBotsListComAPI:
    """Handles interactions with the discordbotslist.com API"""

    def __init__(self, bot):
        self.bot = bot
        self.token = 'Your token'
        self.bot.loop.create_task(self.update_stats())
        self.requests = requests

    async def update_stats(self):
        """This function runs every 30 minutes to automatically update your server count"""

        while True:
            logger.info('Attempting to post server count...')
            try:
                payload = {
                'shard_id': '0',
                'guilds': len(list(self.bot.guilds)), 
                'users': len(self.bot.users),
                'voice_connections': 0 } # idk how to fill that in lol
                headers = {'Authorization': f'Bot {self.token}'}
                url = f'https://discordbotlist.com/api/bots/{self.bot.user.id}/stats'
                r = requests.post(url,  headers=headers, data=payload)
                print(f'Status code:{r.status_code}')
            except Exception as e:
                logger.exception(
                    'Failed to post server count\n{}: {}'.format(type(e).__name__, e))
            await asyncio.sleep(1800)

Prerequisites: 1[Discord.py rewrite] 2[requests (pip install requests)]