Home > Enterprise >  How do I get the user ID of the person who issued the command in Discord
How do I get the user ID of the person who issued the command in Discord

Time:12-18

So im making a discord Bot that will notify me when my favorite food is avalable in the cafiteria at my school. 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. I've never used slash commands in discord before, normally I just use a command.prefix but decided to try it this way instead.

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. 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

CodePudding user response:

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. 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.

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

discord.py isn't beginner-friendly; you should know the basics before trying it. You can look at some examples here.

  • Related