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

Free lines Python 3 revisions 773 2 years ago 2 years ago
import logger
import asyncio
import requests

async def on_ready():
    global logger
    logger = logging.getLogger('client')

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

    def __init__(self, bot):
        self.bot = bot
        self.token = 'Your token'
        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...')
                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:
                    '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)]