捕获CommandOnCooldown错误

时间:2017-10-04 00:48:09

标签: python discord discord.py

我正在制作一个具有冷却时间的discord机器人,并且我正在尝试制作一个事件,当发生CommandOnCooldown错误时,机器人会告诉他们他们需要等待多长时间。这是我的代码,它看起来都没问题,但它不知道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

中的参考资料

1 个答案:

答案 0 :(得分:3)

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

from discord.ext import commands

bot = commands.Bot('$')

@bot.event
async def on_command_error(error, ctx):
    if isinstance(error, commands.CommandOnCooldown):
        await bot.send_message(ctx.message.channel, content='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


@bot.command(pass_context=True)
@commands.cooldown(1, 30)
async def getalt(ctx):
    await bot.send_message(ctx.message.channel, content='in getalt')

bot.run('token')

getalt是命令,其具有30秒的冷却时间,被on_command_error事件捕获,然后将向该频道发送消息。如果您还有其他任何您不清楚的内容,请参阅详细文档here