如何在on_ready事件中使用change_presence ?(discord.py)



我在网上看到,在on_ready活动中使用change_presence可能会导致您的Discord bot因速率限制而被禁止。我认为这是因为在此事件期间进行了大量API调用。

我目前的解决方案是在事件开始时使用asyncio.sleep(),希望首先进行初始化API调用,然后在一段时间后,我的代码运行。

我现在想知道这是否真的有什么帮助,或者它是否和没有睡眠一样糟糕?

下面的代码:

@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="❌"))

最简单的方法是在构建bot时传递它。例子:

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

最新更新