这曾经用于每 5 分钟更改我的机器人的活动,但现在终端会收到此速率限制警告的垃圾邮件:
WARNING discord.gateway WebSocket in shard ID None is ratelimited, waiting 0.00 seconds
它给出的数字有时也可能在 57-59 秒左右。我不认为 5 分钟对于改变机器人的某些内容来说太短了,我见过其他机器人每隔几分钟左右就会改变他们的个人资料图片,我认为这对服务器的要求更高。有更好的方法吗?代码如下。
actaray = ["PcktWtchr's Videos", "Cams", "and Listening Always", "or Listening or Both"]
# Start-up
@bot.event
async def on_ready():
print('Logged in as {0.user}'.format(bot))
while True:
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=random.choice(actaray)))
asyncio.sleep(300)
就像我说的,据我记忆,这曾经有效。等待时间过去是 60 秒,但由于它给了我速率限制,我将其增加到 300 秒,因为我认为它太快了。问题依然存在。
在您当前的代码中,您有一个无限循环,每 5 分钟更改一次机器人的存在。虽然每 5 分钟更改一次状态似乎并不过分,但循环会连续运行,没有任何延迟,这可能会导致达到速率限制。使用
asyncio.sleep
修改代码以在状态更改之间引入延迟。
尝试修改后的代码,看看它是否有效:
actaray = ["PcktWtchr's Videos", "Cams", "and Listening Always", "or Listening or Both"]
# Start-up
@bot.event
async def on_ready():
print('Logged in as {0.user}'.format(bot))
while True:
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=random.choice(actaray)))
await asyncio.sleep(300) # delay between presence changes
@bot.event
async def on_error(event, *args, **kwargs):
if isinstance(args[0], discord.HTTPException) and args[0].status == 429:
# Rate limit encountered
retry_after = args[0].headers.get("Retry-After")
if retry_after:
await asyncio.sleep(int(retry_after) + 1) # Wait for the suggested time before retrying
else:
await asyncio.sleep(5) # Fallback to a reasonable delay if Retry-After is not provided
else:
# Handle errors if they occur
print(f"An error occurred: {args[0]}")