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

使用ctx.author.mention无法获取作者,报错'Interaction'无author属性

问题描述

使用Discord.py的app_commands创建/getinfo命令时,尝试通过ctx.author.mention获取命令调用者的提及信息,触发AttributeError,报错提示'Interaction'对象没有'author'属性。尝试将ctx改为message后问题仍未解决。

原代码

@tree.command(name="getinfo", description="Gets a users info", guild=discord.Object(id=1045161049491505202))
async def getinfo(ctx, user:discord.Member):
    author = ctx.author.mention
    myEmbed = discord.Embed(title="Account Info", description="Shows your **Server Join Date, Account Created Date, and Name**.", color=0x00ff00)
    myEmbed.add_field(name="**Name:**", value=user.display_name, inline=False)
    myEmbed.add_field(name="**Join Date:**", value=user.joined_at, inline=False)
    myEmbed.add_field(name="**Account Created Date:**", value=user.created_at, inline=False)
    myEmbed.add_field(name="**Top Role:**", value=user.top_role.mention, inline=False)
    myEmbed.set_author(name="General Frog")
    await ctx.response.send_message(embed=myEmbed)

报错信息

Traceback (most recent call last):
  File "C:\Users\madri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\app_commands\tree.py", line 1242, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "C:\Users\madri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\app_commands\commands.py", line 887, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "C:\Users\madri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\app_commands\commands.py", line 880, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'getinfo' raised an exception: AttributeError: 'Interaction' object has no attribute 'author'
解决方案

Discord.py的app_commands(斜杠命令)中,命令函数接收的第一个参数是Interaction对象,而非传统前缀命令里的Context或Message对象。Interaction对象没有author属性,获取命令发起者需要使用interaction.user属性。

修正后的代码

@tree.command(name="getinfo", description="Gets a users info", guild=discord.Object(id=1045161049491505202))
async def getinfo(interaction, user:discord.Member):
    # 获取命令调用者的提及信息
    author = interaction.user.mention
    myEmbed = discord.Embed(title="Account Info", description="Shows your **Server Join Date, Account Created Date, and Name**.", color=0x00ff00)
    myEmbed.add_field(name="**Name:**", value=user.display_name, inline=False)
    myEmbed.add_field(name="**Join Date:**", value=user.joined_at, inline=False)
    myEmbed.add_field(name="**Account Created Date:**", value=user.created_at, inline=False)
    myEmbed.add_field(name="**Top Role:**", value=user.top_role.mention, inline=False)
    myEmbed.set_author(name="General Frog")
    await interaction.response.send_message(embed=myEmbed)

说明

  • 将参数名ctx改为interaction(可选,但更符合语义),调用interaction.user获取命令发起者,再通过.mention获取提及格式。
  • 保持interaction.response.send_message()的调用逻辑不变,因为斜杠命令必须通过Interaction对象的响应方法回复。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 20:35:27