这曾经用于每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)

就像我说的,就我记忆所及,这招很管用.等待时间过go 是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]}")

Python相关问答推荐

Python会扔掉未使用的表情吗?

Odoo 14 hr. emergency.public内的二进制字段

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

将jit与numpy linSpace函数一起使用时出错

Pandas 有条件轮班操作

在线条上绘制表面

我们可以为Flask模型中的id字段主键设置默认uuid吗

Python解析整数格式说明符的规则?

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

实现神经网络代码时的TypeError

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

如何在BeautifulSoup/CSS Select 器中处理regex?

下三角形掩码与seaborn clustermap bug

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

如何在一组行中找到循环?

如何在Gekko中处理跨矢量优化

将像素信息写入文件并读取该文件

查找数据帧的给定列中是否存在特定值

FileNotFoundError:[WinError 2]系统找不到指定的文件:在os.listdir中查找扩展名

将索引表转换为Numy数组