You need to enable JavaScript to run this app. discord.py实现@用户Nitro查询指令触发AttributeError的解决
优惠活动
大模型
产品
解决方案
定价
更多

discord.py实现@用户Nitro查询指令触发AttributeError的解决

报错原因定位
  • 第一处错误是API调用完全不符合discord.py的对象结构:ctx.message.author 返回的是服务器成员(discord.Member)实例,这个类根本没有挂载discord这个属性,直接顺着原来的路径访问ctx.message.author.discord.profile.nitro,自然会抛出AttributeError。
  • 第二处是功能认知偏差:discord.py 作为官方维护的开源库,公共API从来不会直接暴露用户的Nitro订阅状态字段,你原来写的.discord.profile.nitro是不存在的接口路径。
  • 第三处是功能逻辑缺失:当前代码没有设置接收@提及用户的参数,就算跑通了也只会查询指令发送者自己的状态,根本实现不了指定@用户查状态的需求。
可落地的正确实现方案

因为Discord官方不直接提供Nitro状态的返回值,我们可以通过Nitro用户专属的可获取特征(动态头像、自定义用户横幅、头像装饰)做判断,这些权益只有Nitro订阅用户才能使用,检测准确率足够覆盖绝大多数场景。
代码运行前需要先在客户端初始化时开启对应权限(服务器成员读取、消息内容读取的intent),否则无法获取到目标用户的完整数据:

# 初始化客户端时的intent配置参考
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
client = commands.Bot(command_prefix=".", intents=intents)

@client.command()
async def nitro(ctx, target_member: discord.Member = None):
    # 未指定目标用户时,默认查询指令发送者自身状态
    target = target_member or ctx.author
    # 拉取目标用户的完整公开资料(包含横幅、装饰等只有Nitro能配置的字段)
    full_user = await client.fetch_user(target.id)

    # 判断是否具备Nitro专属功能特征
    has_nitro = any([
        target.animated_avatar is not None,
        full_user.banner is not None,
        full_user.avatar_decoration is not None
    ])

    # 按照状态设置展示内容与样式
    if has_nitro:
        embed_color = discord.Colour.green()
        status_desc = "✅ 已识别到Nitro专属功能配置,确认持有Nitro"
    else:
        embed_color = discord.Colour.greyple()
        status_desc = "❌ 未检测到Nitro专属功能配置,暂不识别为Nitro用户"

    embed = discord.Embed(
        title="用户Nitro状态查询",
        description=f"查询目标:{target.mention}\n状态说明:{status_desc}",
        color=embed_color
    )
    embed.set_thumbnail(url=target.display_avatar.url)
    await ctx.send(embed=embed)

注意:Discord公开API层面不提供Nitro订阅状态的直接返回值,上述实现是基于功能权限特征的间接判断,存在极小概率漏判(比如刚开通Nitro但还没配置任何专属功能的用户),这是平台API的限制,并非代码逻辑问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.09 11:57:03