Search code examples
python-3.xdiscorddiscord.pypython-asyncio

How to use change_presence at on_ready event? (discord.py)


I've read online that using change_presence at the on_ready event could cause your Discord bot to be banned due to rate limiting. I presume this is because a lot of API calls are made during this event.

My current solution is to use asyncio.sleep() at the start of the event in the hope that the initialisation API calls are made first and then, after a period of time, my code runs.

I'm now wondering whether this actually does anything to help, or if it's just as bad as having no sleep whatsoever?

Code below:

@commands.Cog.listener()
async def on_ready(self):
    await asyncio.sleep(5)
    await self.bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="❌"))


Solution

  • The easiest way is to just pass it in when constructing your bot. Example:

    from discord.ext import commands
    
    bot = commands.Bot(
        command_prefix="!",
        activity=discord.Activity(type=discord.ActivityType.watching, name="❌"),
        intents=discord.Intents.default(),
    )