我查看了其他类似的问题,这些修复程序无法正常工作,这是我的代码。
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("Command On Cooldown. Sorry fo rugly text. I will fix it - Kwuartz")
m, s = divmod(error.retry_after, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 60)
if int(h) == 0 and int(m) == 0:
embed = discord.Embed(colour=0x1ABC9C)
embed.title = "**Command Cooldown:**"
embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(s)} seconds** on a per user basis."
await ctx.send(embed = embed)
elif int(d) == 0 and int(h) == 0 and int(m) != 0:
embed = discord.Embed(colour=0x1ABC9C)
embed.title = "**Command Cooldown:**"
embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(m)} minutes** and **{int(s)} seconds** on a per user basis."
await ctx.send(embed = embed)
elif int(d) == 0 and int(h) != 0:
embed = discord.Embed(colour=0x1ABC9C)
embed.title = "**Command Cooldown:**"
embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(h)} hours**, **{int(m)} minutes** and **{int(s)} seconds** on a per user basis."
await ctx.send(embed = embed)
else:
embed = discord.Embed(colour=0x1ABC9C)
embed.title = "**Command Cooldown:**"
embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(d)} days**, **{int(h)} hours**, **{int(m)} minutes** and **{int(s)} seconds** on a per user basis."
await ctx.send(embed = embed)
elif isinstance(error, commands.CommandNotFound):
embed = discord.Embed(colour=0x1ABC9C)
embed.title = "**Command Error:**"
embed.description = f":x: **Why this happened:**\nThe command you specified does not exist."
await ctx.send(embed = embed)
else:
raise error
PS:我是新来的,所以可能有愚蠢的错误。抱歉!如有必要,我将附加更多代码
控制台输出:
忽略on_message中的异常 追溯(最近一次通话): _run_event中的文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ client.py”,行333 等待科罗(* args,** kwargs) 文件“ C:\ Users \ OnlyMe \ PythonProjects \ Atom \ ImpulseBot \ main_setup.py”,行158,位于on_message中 等待bot.process_commands(消息) 文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ bot.py”,行940,在process_commands中 等待self.invoke(ctx) 调用中的文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ bot.py”,行907 等待ctx.command.dispatch_error(ctx,exc) 文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ core.py”,第422行,位于dispatch_error中 等待注入(cog,ctx,错误) 文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ core.py”,第71行,已包装 ret =等待coro(* args,** kwargs) 文件“ C:\ Users \ OnlyMe \ PythonProjects \ Atom \ ImpulseBot \ cogs \ moderation_commands.py”,第225行,在clear_error中 引发错误 调用中的文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ bot.py”,行903 等待ctx.command.invoke(ctx) 调用中的文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ core.py”,行851 等待self.prepare(ctx) 准备文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ core.py”,行785 self._prepare_cooldowns(ctx) 文件“ C:\ Users \ OnlyMe \ AppData \ Roaming \ Python \ Python38 \ site-packages \ discord \ ext \ commands \ core.py”,行773,在_prepare_cooldowns中 提高CommandOnCooldown(bucket,retry_after) discord.ext.commands.errors.CommandOnCooldown:您正在冷却。在9.53秒后重试
答案 0 :(得分:0)
如果您想使用hours, minutes, seconds,
格式的时间,则可以使用模运算符来发挥自己的优势:
d = math.floor(error.retry_after / 60 / 60 / 24)
h = math.floor(error.retry_after / 60 / 60)
m = math.floor(error.retry_after / 60)
s = error.retry_after % 60
如果on_command_error
不能捕获所有内容,那是因为它仅适用于 command 错误。
PS。请指定您的问题,以便更轻松地解决。
这是我的代码工作时的样子:
if isinstance(error, commands.CommandOnCooldown):
retryAfter = [math.floor(error.retry_after / 360), math.floor(error.retry_after / 60), error.retry_after % 60]
await ctx.send('You cannot use command `%s%s` for %s hours, %s minutes, and %.2f seconds.' % (
str(bot.command_prefix), str(ctx.command), retryAfter[0], retryAfter[1], retryAfter[2]))
print('Command "%s" is on a %.3f second cooldown' % (ctx.command, error.retry_after))