问题
I am making a discord bot that has a cooldown and I am attempting to make an event that when the CommandOnCooldown
Error occurs, the bot will DM them how much longer they have to wait. Here is my code and it all looks okay, but it doesn't know what retry_after means:
@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.")
I am using references from https://git.radiobrony.fr/MKody/discord.py/commit/cd0de57d13b15f709aaacf78ce611dd87e0784ce
回答1:
This is the general format for catching exceptions when using 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')
The getalt
is the command, which has a 30-second cooldown, is caught by the on_command_error
event, in turns will send a message to the channel. If you have anything else that you’re unclear about, please refer to the detailed documentation here.
来源:https://stackoverflow.com/questions/46555469/catching-commandoncooldown-error