You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py报错AttributeError:'Command'对象无'CommandOnCooldown'属性

解决Discord.py中AttributeError: 'Command' object has no attribute 'CommandOnCooldown'错误

你碰到的这个错误,根源是代码里把CommandOnCooldown异常类当成了命令对象的属性来用,但实际上这个异常类是discord.ext.commands模块下的东西。

修复方法

检查你的错误处理函数,确保判断冷却异常时用的是commands.CommandOnCooldown,而不是命令对象(比如daily.CommandOnCooldown)。正确的写法如下:

@daily.error
async def daily_error(ctx, error):
    if isinstance(error, commands.CommandOnCooldown):
        remaining_time = error.retry_after
        await ctx.send(f"You need to wait {remaining_time:.0f} seconds before claiming your daily bonus again.")

额外检查点

  • 确认已经正确导入commands模块:from discord.ext import commands
  • 若使用的Discord.py版本过旧,建议升级到v1.x或v2.x版本,该异常类在这两个版本中都稳定存在于commands模块下。

(可选)优化文件操作逻辑

你的daily命令里为每个用户单独创建文件,没必要循环读取每行,直接读写文件内容即可,简化后的代码示例:

try:
    with open(f'{ctx.author.id}.txt', 'r') as f:
        content = f.read().strip()
        balance = int(content.split(':')[1]) if content else 0
    # 计算新余额
    bonus = random.randint(1, 1000) * 15
    new_balance = balance + bonus
    # 写入新余额
    with open(f'{ctx.author.id}.txt', 'w') as f:
        f.write(f"{ctx.author.id}:{new_balance}")
    # 发送提示消息
    embed = discord.Embed(title=f"{ctx.author.name} claimed their daily bonus!", description=f"You earned `{bonus}`:dollar: as your daily bonus.\nYour new balance is `{new_balance}` :dollar:.\n\n*This command has a cooldown of 24 hours.*", color=0x000000)
    await ctx.send(f"{ctx.author.mention}", embed=embed)
except FileNotFoundError:
    embed = discord.Embed(title=f"{ctx.author.name}", description=f"You do not have a balance. Please use the `++reset` command to create one.\n\n*This command has a cooldown of 24 hours.*", color=0x000000)
    await ctx.send(f"{ctx.author.mention}", embed=embed)

内容的提问来源于stack exchange,提问作者Saka Discord

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 11:55:16