#how would i make a button only work for the person who did the command?

1 messages · Page 1 of 1 (latest)

cloud fiber
#

Pass a user attribute to your class

deep panther
#

like user: discord.Member?

cloud fiber
#
view = MyView()
view.user = ctx.author
await ctx.respond(view=view)```
deep panther
#

alright let me try

small pike
#

use interaction check

deep panther
cloud fiber
#

Thought that was obvious. The command.

deep panther
#

thought as much just wanted to make sure

deep panther
cloud fiber
#

Code

deep panther
#
class loginView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)
    
    @discord.ui.button(
        label="Submit Code",
        style=discord.ButtonStyle.green,
        emoji="🔒",
        custom_id="submit"
    )
    async def create(self, button: discord.ui.Button, interaction: discord.Interaction, ctx: discord.ApplicationContext):
        modal = LoginModal(title="")
        await interaction.response.send_modal(modal)```
cloud fiber
#

Command code

deep panther
#
@bot.slash_command()
async def command(ctx): 
    await ctx.defer()
    view=loginView()
    if view.user == ctx.author:
      embed=discord.Embed(title="stuff",url='link',color=0x00aaff)
      embed.add_field(name="more stuff",
                            value="stuff ", inline=False)
      embed.set_image(url="stuff here")
      embed.add_field(name="stuff here",value=f'stuff here', inline=False)
      await ctx.respond(embed=embed, view=loginView())
    else:
      return```
small pike
#

if you want user in your view, you have to put user in your view

cloud fiber
#

You definitely didn't understand the code I gave you

deep panther
#

yup

#

i can confirm

#

i didn't

cloud fiber
#

And you literally did what you wanted

#

You dont check the view.user in the command.

#

You do that on interaction_check on your view class.

deep panther
#

oh

#

you said in the command a bit ago so i got a little confused

cloud fiber
#

This looks nothing like a check

#

What we're doing there is:

deep panther
#
    async def interaction_check(self, interaction: discord.Interaction):
      view=loginView()
      view.user = ctx.author```?
cloud fiber
#

Creating an instance of a View(), setting an attribute to it which is the author, which then you access in your view class with self.user

#

And then you're sending such instance

cloud fiber
#

interaction_check expects a return of True or False

#

So you work with your condition and return whichever value.

deep panther
#

huh?

cloud fiber
#

Do you have some python knowledge

deep panther
#

yeah kinda

#

still a newbie tho

cloud fiber
#

Do you know OOP

#

A Discord bot is hard for new people

deep panther
cloud fiber
#

?tag lp

low galeBOT
#
deep panther
#
    async def interaction_check(self, interaction: discord.Interaction):
        return interaction.user.id == view.user```
#

does this make sense?

cloud fiber
#

You're close

#

But that will always return False

deep panther
#

so

cloud fiber
#

Because an int will never be equal to a user object

deep panther
#
    async def interaction_check(self, interaction: discord.Interaction):
        return interaction.user != view.user```
cloud fiber
#

No

deep panther
#

oh

cloud fiber
cloud fiber
#

You access a class attribute with self

deep panther
#

i edited it

#

oh

deep panther
# cloud fiber Just self.user.id

so like this

    async def interaction_check(self, interaction: discord.Interaction):
      return interaction.user.id == self.user.id```
cloud fiber
#

Bravo

deep panther
#

i don't change anything in the command itself?

#
 line 168, in interaction_check
    return interaction.user.id == self.user.id
AttributeError: 'loginView' object has no attribute 'user'```
cloud fiber
#

Create the instance, set the attribute, send the button

#

In the command

deep panther
#

oh yeah

deep panther
cloud fiber
#

Command code

deep panther
cloud fiber
#

I know

#

I'm asking for your command code

deep panther
#
@bot.slash_command()
async def command(ctx): 
      await ctx.defer()
      view=loginView()
      embed=discord.Embed(title="stuff",url='link',color=0x00aaff)
      embed.add_field(name="more stuff",
                            value="stuff ", inline=False)
      embed.set_image(url="stuff here")
      embed.add_field(name="stuff here",value=f'stuff here', inline=False)
      await ctx.respond(embed=embed, view=loginView())```
#

code so bad bro got offline

cloud fiber
#

You didn't set the view attribute

#

And you're creating a NEW instance when sending the view

deep panther
#

oh

#

do you have an example

small pike
deep panther
#

i figured it out

#

dw about it