1

Posting to discordbotslist.com with aiohttp

Free lines Python 2 revisions 86 a month ago a month ago
import asyncio
import json
import aiohttp


class StatPoster:
    def __init__(self, bot):
        self.bot = bot
        self.token = 'yourtoken'
        self.dbl_header = {"Authorization": f"Bot {self.token}", "Content-Type": "application/json"}
        self.session = aiohttp.ClientSession(loop=bot.loop)
        self.bot.loop.create_task(self.poster())

    async def poster(self):
        while True:
                dbl_payload = {"guilds": len(self.bot.guilds), "users": len(self.bot.users)), "voice_connections": len(self.bot.voice_clients) }
                async with self.session.post(f'https://discordbotlist.com/api/bots/{self.bot.id}/stats', headers=self.dbl_header, data=json.dumps(dbl_payload)) as dbl:
                    response = await dbl.text()
                    print('Guild count posted to DBL: [{}]: {}'.format(dbl.status, response))
            await asyncio.sleep(300)


def setup(bot):
    bot.add_cog(StatPoster(bot))

aiohttp makes bot go fast zoooom