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

尝试解决Python中discord.py命令运行触发AttributeError的问题

错误根因

报错discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'author'的核心原因是:你在代码中对字符串类型的变量调用了仅discord上下文对象、消息对象才具备的author属性。

常见触发场景及修复方案

  • 命令函数参数配置错误
    discord.py的命令函数第一个参数必须是上下文对象ctx,如果把第一个参数设为接收字符串的参数,后续调用author就会报错。
    错误代码示例:
    @bot.command()
    async def send(msg: str):
        await ctx.send(f"触发用户:{msg.author}")
    
    修复后代码:
    @bot.command()
    async def send(ctx, msg: str):
        await ctx.send(f"触发用户:{ctx.author}")
    
  • 消息对象与消息内容混淆
    如果你把字符串类型的消息内容message.content赋值给了本该存储消息对象的变量,调用author时就会触发报错。
    错误代码示例:
    current_msg = message.content
    print(current_msg.author)
    
    修复后代码:
    current_msg = message
    print(current_msg.author)
    
  • 手动传参错误
    如果你在代码中手动调用命令函数时,错误传入字符串代替了上下文/消息对象,也会触发该报错。排查所有调用.author的代码行,确认对应变量的类型为discord.Message或discord.ext.commands.Context即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 16:54:03