#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)
either 1) make the response ephemeral or 2) check interaction.user.id in the callback
How would I check the interaction.user.id in the callback?
Wdym how
Callbacks have an interaction argument
Oh I see
Hmmm let's see
Are you subclassing?
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!")
Do I need to?
For your purpose I'd recommend it
How would I do that
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
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?
Yep
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))
Looks good to me
So how would I make the part where it only lets the author of the command use the button?
All you have to do in the callback is compare interaction.user to self.user
Oh okay
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
I am getting this error
What's your view code now?
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!")
You don't add user to the callback
okay
Inside the init function below super, you define self.user = user
Allgood