【问题标题】:Discord.py how to get slash command author idDiscord.py 如何获取斜杠命令作者 ID
【发布时间】:2022-12-23 02:49:07
【问题描述】:

我想获取使用斜杠命令的用户 ID。我尝试使用:

author = ctx.message.author.id

但我得到这个错误:

AttributeError: 'NoneType' object has no attribute 'author'

我的完整代码:

slash = SlashCommand(Bot,sync_commands=True)

@slash.slash(
    name="getid",
    description="description",
    guild_ids=[guild id here]
)
async def _getid(ctx:SlashContext):
    author = ctx.message.author.id
    await ctx.send(author)

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    ctx.author.id 应该可以。我认为您无法通过交互访问消息。

    如果你使用discord.pyapp_commands,你可以使用interaction.user.id

    @bot.tree.command(name='userid', guild = discord.Object(id=...)) 
    async def userid(interaction: discord.Interaction):
        await interaction.response.send_message(f"The id of the user that invoked this command is {interaction.user.id}")
    

    【讨论】:

    • 我收到 AttributeError: module 'discord' has no attribute 'Interaction' 错误,我编辑了我的问题并添加了我的完整代码。
    • 哦,看来您正在使用另一个包来处理斜杠命令而不是discord.py==2.0。无论如何,我认为如果您执行ctx.author.id,它就会起作用。
    • 我如何安装 discord.py 2.0?
    • pip 安装 discord.py
    【解决方案2】:

    好吧,斜杠命令没有命令的实际消息。 这就是为什么当你尝试执行ctx.message时它返回None,因为当你执行/getid并发送它时,你实际上并没有发送消息,而是发送了命令。你会收到错误消息,因为None 没有属性 id

    所以你需要在这里做的是ctx.author.id,因为实际上有一个命令的作者,但没有消息。所以你可以直接通过ctx.author.id

    您的新代码应该是:

    slash = SlashCommand(Bot,sync_commands=True)
    
    @slash.slash(name="getid", description="description", guild_ids=[guild id here])
    async def _getid(ctx: SlashContext):
        author = ctx.author.id
        await ctx.send(author)
    

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 2022-11-25
      • 2023-02-26
      • 2022-11-30
      • 1970-01-01
      • 2021-10-02
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多