#How can I make it so only the person who did the slash command can interact with a button?

1 messages · Page 1 of 1 (latest)

runic vale
#

Im making a command and I am trying to make it so only the person who did the slash command can click the button.

scarlet crypt
scarlet crypt
#

Wdym how

#

Callbacks have an interaction argument

#

Oh I see

#

Hmmm let's see

#

Are you subclassing?

runic vale
#

Look let me send my code/view

#
class StopDupe(discord.ui.View):  # Create a class called MyView that subclasses discord.ui.View

    @discord.ui.button(label="Stop Dupe", style=discord.ButtonStyle.danger)
    async def button_callback(self, button, interaction):
        await interaction.response.send_message("Stopped dupe!")
scarlet crypt
#

Isee

#

You didn't override the init?

runic vale
#

Do I need to?

scarlet crypt
#

For your purpose I'd recommend it

runic vale
#

How would I do that

scarlet crypt
#

Because then you could add a custom argument like user

#

Basically just add def __init__(self, user): at the top of a class, then the first line should super().__init__() to ensure everything works

#

And finally you want to define self.user = user; that way, when you call the view from a command you can do something like StopDupe(ctx.author) and it'll become an attribute

runic vale
#
class StopDupe(discord.ui.View):  # Create a class called MyView that subclasses discord.ui.View
    def __init__(self, user):
        super().__init__()

    @discord.ui.button(label="Stop Dupe", style=discord.ButtonStyle.danger)
    async def button_callback(self, button, interaction):
        await interaction.response.send_message("Stopped dupe!")
``` Liek this?
scarlet crypt
#

Yep

runic vale
#

So it should look like this

#
@bot.slash_command(description="dupe")
async def dupe(ctx):
    check = await cursor.find_one({"discordid": ctx.author.id})
    if check is None:
        await ctx.respond("You have not logged in yet!")
    else:
        idk = discord.Embed(title="Dupe Is Started!",description="Press stop dupe to stop it!")
        await ctx.respond(embed=idk, view=StopDupe(ctx.author))
scarlet crypt
#

Looks good to me

runic vale
#

So how would I make the part where it only lets the author of the command use the button?

scarlet crypt
#

All you have to do in the callback is compare interaction.user to self.user

runic vale
#

Oh okay

scarlet crypt
#

If it's false then make a generic response saying you can't use it

#

Though conversely, you can also set the command response to ephemeral=True so only the command user can see the button in the first place

runic vale
#

I am getting this error

scarlet crypt
#

What's your view code now?

runic vale
#
class StopDupe(discord.ui.View):  # Create a class called MyView that subclasses discord.ui.View
    def __init__(self, user):
        super().__init__()

    @discord.ui.button(label="Stop Dupe", style=discord.ButtonStyle.danger)
    async def button_callback(self, button, interaction, user):
        if interaction.user == self.user:
            await interaction.response.send_message("Stopped dupe!")
        else:
            await interaction.response.send_message("You cannot do this!")
scarlet crypt
#

You don't add user to the callback

runic vale
#

okay

scarlet crypt
#

Inside the init function below super, you define self.user = user

runic vale
#

I fixed it

#

Thank you

#

It works

scarlet crypt
#

Allgood