繁体   English   中英

如何获取在 Discord 中发出命令的人的用户 ID

[英]How do I get the user ID of the person who issued the command in Discord

所以我制作了一个 discord 机器人,当我学校的食堂有我最喜欢的食物时,它会通知我。 我试图做到这一点,以便多人可以使用该机器人,因此我将用户最喜欢的食物存储在字典中,并以他们的用户 ID 作为键。 我以前从未在 discord 中使用过斜杠命令,通常我只使用 command.prefix 但决定改为尝试这种方式。

到目前为止,这就是我所拥有的:

client = aclient()
tree = app_commands.CommandTree(client)

@tree.command(guild = discord.Object(id=guild_id), name = 'favorite_add', description='Add a food item to your favorite food list') #guild specific slash command
async def addFavorite(interaction: discord.Interaction, content : str = ""):
    await interaction.response.send_message(f"{content} has been added to your favorite food list", ephemeral = True) 
    userFavorites[content.author.id] += [content]

我遇到的问题是 str: content 没有 author.id 属性。 有什么方法可以获取发布命令的人的 ID,以便我可以将内容添加到他们最喜欢的食物列表中,或者可能向我的字典中添加一个新键

async def addFavorite(interaction: discord.Interaction, content : str = ""):
                                                        ^^^^^^^

那是因为content参数是一个字符串,因为您是在告诉库您想要它是什么。 如果要获取触发命令的用户的 ID,则必须从interaction.user获取用户,然后获取 ID。

userFavorites[interaction.user.id] += [content]

discord.py对初学者不友好; 在尝试之前,您应该了解基础知识。 您可以在此处查看一些示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM