Catching CommandOnCooldown Error(捕获 CommandOnCooldown 错误)
问题描述
我正在制作一个有冷却时间的不和谐机器人,并且我正在尝试制作一个事件,当 CommandOnCooldown
错误发生时,该机器人会告诉他们他们需要等待多长时间.这是我的代码,看起来一切正常,但它不知道 retry_after 是什么意思:
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.")
我正在使用来自 https://git.radiobrony.fr/的参考资料MKody/discord.py/commit/cd0de57d13b15f709aaacf78ce611dd87e0784ce
推荐答案
这是使用 discord.py 时捕获异常的通用格式:
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(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
事件捕获,依次向频道发送消息.如果您还有其他不清楚的地方,请参阅详细文档 这里.
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.
这篇关于捕获 CommandOnCooldown 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:捕获 CommandOnCooldown 错误
基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01