gpt4 book ai didi

python - 捕获 CommandOnCooldown 错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:41 24 4
gpt4 key购买 nike

我正在制作一个具有冷却时间的 discord 机器人,我正在尝试制作一个事件,当发生 CommandOnCooldown 错误时,机器人将向他们发送 DM,告诉他们他们必须等待多长时间。这是我的代码,看起来一切正常,但它不知道 retry_after 是什么意思:

@bot.event
async def on_CommandOnCooldown():
await bot.send_message(ctx.message.channel, 'You are on cooldown. Try again in {:.2f}s'.format(retry_after))

@bot.command(pass_context = True)
@commands.cooldown(1, 30, commands.BucketType.user)
async def getalt(ctx):
msg = ["a list of stuff"]
await bot.send_message(ctx.message.author, random.choice(msg))
await bot.send_message(ctx.message.channel, "Alt Has Been Seen To Your DMs")
await bot.purge_from(ctx.message.channel, limit=2)
await bot.send_message(ctx.message.author, "Please Wait 30 Seconds Before Using This Command Again. If you do not wait the full time then you won't be sent an alt.")

我正在使用来自 https://git.radiobrony.fr/MKody/discord.py/commit/cd0de57d13b15f709aaacf78ce611dd87e0784ce 的引用

最佳答案

这是在使用 discord.py 时捕获异常的通用格式:

from discord.ext import commands

bot = commands.Bot('$')

@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send('This command is on a %.2fs cooldown' % error.retry_after)
raise error # re-raise the error so all the errors will still show up in console


@commands.cooldown(1, 30)
@bot.command()
async def getalt(ctx):
await ctx.send('in getalt')

bot.run('token')

getalt是命令,有30秒的冷却时间,被on_command_error事件捕获,轮流向 channel 发送消息。还有什么不明白的可以引用详细文档here .

关于python - 捕获 CommandOnCooldown 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com