简体   繁体   English

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

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

So im making a discord Bot that will notify me when my favorite food is avalable in the cafiteria at my school.所以我制作了一个 discord 机器人,当我学校的食堂有我最喜欢的食物时,它会通知我。 Im trying to make it so multiple people can use the bot so im storing the users favorite food in a dictionary with their user id as the key.我试图做到这一点,以便多人可以使用该机器人,因此我将用户最喜欢的食物存储在字典中,并以他们的用户 ID 作为键。 I've never used slash commands in discord before, normally I just use a command.prefix but decided to try it this way instead.我以前从未在 discord 中使用过斜杠命令,通常我只使用 command.prefix 但决定改为尝试这种方式。

So far this is what I have:到目前为止,这就是我所拥有的:

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]

The problem im having is that the str: content doesnt have the author.id attrubite.我遇到的问题是 str: content 没有 author.id 属性。 Is there any way to aquire the id of who posted the command so I can add the content to their favorite food list or potentially add a new key to my dictionary有什么方法可以获取发布命令的人的 ID,以便我可以将内容添加到他们最喜欢的食物列表中,或者可能向我的字典中添加一个新键

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

That's because the content argument is a string since you're telling the library that you wanted it to be.那是因为content参数是一个字符串,因为您是在告诉库您想要它是什么。 If you want to get the ID of the user who triggered the command, you have to get the user from interaction.user then get the ID.如果要获取触发命令的用户的 ID,则必须从interaction.user获取用户,然后获取 ID。

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

discord.py isn't beginner-friendly; discord.py对初学者不友好; you should know the basics before trying it.在尝试之前,您应该了解基础知识。 You can look at some examples here .您可以在此处查看一些示例。

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

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