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

discord.py中reportar命令报错Object has no attribute send求助

discord.py reportar命令报错解决方案

报错根源

报错AttributeError: 'NoneType' object has no attribute 'send'指向await owner.send(embed=x)行,说明bot.get_user(773913180271280129)返回了空值,没有拿到目标用户对象,常见诱因如下:

  • 未开启成员网关意图:discord.py v2.0+ 默认关闭敏感权限,没有开启Server Members Intent的情况下,机器人无法缓存全局用户信息,仅能拿到当前服务器的在线成员数据
  • 所有者ID填写错误:检查填写的ID是否为你本人的Discord账号ID,有没有多输/漏输数字
  • 本地缓存未加载:get_user仅从机器人本地缓存拉取数据,刚启动时缓存未同步完成也会返回空值
  • 补充:原代码还存在兼容问题,私信调用命令时ctx.guild为None、discord.py v2.0+ 废弃了icon_url/avatar_url属性,后续也会触发报错

修复步骤

  1. 开启成员意图
    • 进入Discord开发者后台,找到对应机器人应用,进入「Bot」页面,在「Privileged Gateway Intents」板块开启Server Members Intent开关
    • 代码初始化bot时添加意图配置:
    import discord
    from discord.ext import commands
    
    intents = discord.Intents.default()
    intents.members = True # 开启成员意图
    bot = commands.Bot(command_prefix="你的命令前缀", intents=intents)
    
  2. 替换用户获取方式:用异步方法fetch_user代替get_user,直接请求Discord API拉取用户,不受本地缓存限制
  3. 增加边界判断:添加空值校验、异常捕获,避免异常直接导致命令崩溃
  4. 兼容属性变更:适配discord.py v2.0+ 的头像、图标属性写法

修复后完整代码

async def reportar(ctx, *, text=None):
    # 禁止私信调用命令,避免ctx.guild为空报错
    if not ctx.guild:
        await ctx.send("该命令仅可在服务器内使用", delete_after=5)
        return
    # 校验上报内容不为空
    if not text:
        await ctx.send("请输入需要上报的问题内容", delete_after=5)
        return
    # 拉取所有者用户对象,加异常捕获
    try:
        owner = await bot.fetch_user(773913180271280129)
    except discord.NotFound:
        await ctx.send("上报失败:所有者ID配置错误", delete_after=5)
        return
    except discord.Forbidden:
        await ctx.send("上报失败:机器人无拉取用户信息权限", delete_after=5)
        return

    # 构建上报Embed
    report_embed = discord.Embed(title='**Report Error!**')
    report_embed.add_field(name='Server:', value=ctx.guild.name, inline=False)
    report_embed.add_field(name='ID of the Server:', value=ctx.guild.id, inline=False)
    report_embed.add_field(name='Report:', value=text, inline=False)
    # 兼容无服务器图标、无用户头像的场景
    report_embed.set_thumbnail(url=ctx.guild.icon.url if ctx.guild.icon else None)
    report_embed.set_author(name=ctx.author, icon_url=ctx.author.avatar.url if ctx.author.avatar else None)
    report_embed.timestamp = datetime.datetime.utcnow()

    await owner.send(embed=report_embed)
    await ctx.message.add_reaction('✅')

    # 给上报用户发反馈
    reply_embed = discord.Embed(title='#Team B0T')
    reply_embed.add_field(name=f'**Hello {ctx.author.display_name}**', value='*YOUR MESSAGE*')
    reply_embed.timestamp = datetime.datetime.utcnow()
    await ctx.author.send(embed=reply_embed)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 12:45:00