How to check who interacted with discord.py 2.0 component

Question:

I have a discord.py 2.0 button component, I need to make sure the person who clicks the button is the one who initiated the prompt. But I don’t know how to get the person who clicks the button so I can compare it too ctx.author. Here is what I have so far.

@bot.command()
async def prompt(ctx):
    positive = Button(label="Yes", style=discord.ButtonStyle.green)
    negative = Button(label="No", style=discord.ButtonStyle.red)

    async def positive_callback(interaction):
        if ctx.button == ctx.author:  # ctx.button is not a real argument, that is what needs to be the user who clicks the button.
            await interaction.response.edit_message(content=f"{ctx.author.mention} Here is some cake ❤", view=None)

    positive.callback = positive_callback

    view = View()
    view.add_item(positive)
    view.add_item(negative)
    await ctx.send(content=f"{ctx.author.mention} Would you like to have some cake?", view=view)
Asked By: Lloyd

||

Answers:

After some more internet searches I found out instead of using ctx.author you should use interaction.user
Like so,

async def positive_callback(interaction):
    if interaction.user == ctx.author:
        await interaction.response.edit_message(content=f"{ctx.author.mention} Here is some cake ❤", view=None)
Answered By: Lloyd
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.