在 discord.py 中检测到失败的频道编辑

Detect failed edit of channel in discord.py

我正在制作一个让用户编辑语音频道名称的 discord 机器人。我的代码是这样的:

channel = client.get_channel(id)
try:
    await channel.edit(name="new name")
except:
    raise

如果名称无效但如果名称有效并且由于 Discord 速率限制更改不成功我猜每 10 分钟编辑 2 次,它会引发罚款。

我认为问题是我这边没有错误,Discord 只是在等待响应。有没有办法让我的请求超时大约 5 秒,如果超时时间太长则提高?

我会同时检查错误和 None 响应。更好的是,为什么不检查更改后的名称是否正确?

我认为您不能将超时直接添加到函数调用中,但这是您可能想要循环并添加 asyncio.sleep 的类型。忽略第一部分,你可能会得到这样的结果:

NEW_NAME = "new name"
while True:
    e = None
    try:
        await channel.edit(name=NEW_NAME)
    except Exception as e: pass
    # "channel.name" is pseudo-code. 
    # Use whatever method to check the name.
    if <channel.name> != NEW_NAME: 
        print ("Exception: %s ...(whatever else you want to print)" % e)
        await asyncio.sleep(60) # or whatever you think reasonable
    else:
        break