python 如何检查谁与www.example.com 2.0组件进行了交互discord.py

5tmbdcev  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(98)

我有一个discord.py2.0按钮组件,我需要确保单击按钮的人是发起提示的人。但我不知道如何获得单击按钮的人,以便我可以比较它太ctx.author。以下是我到目前为止所做的。

@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)
q3qa4bjr

q3qa4bjr1#

经过更多的互联网搜索,我发现应该使用interaction.user而不是ctx.author,就像这样,

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)

相关问题