#Discord python Buttons

62 messages · Page 1 of 1 (latest)

stable nymph
#

How do I detected when someone clicks a button? This is what I have to add a button:

view = discord.ui.View()
view.add_item(discord.ui.Button(label="Test Button", style=discord.ButtonStyle.green))

await client.get_channel(channelId).send(embed=embed, view=view)```
unborn mauve
#

Pycord?

stable nymph
#

Euhh

#

Does that also work when the bot restarts?

unborn mauve
#

Oh

#

await interaction_check(interaction)

stable nymph
unborn mauve
#

discord.ui.View.interaction_check()

#

And then is_component() to check that the interaction comes from the button

#

Oh yeesh

#

There's like 10 ways of doing that, lmao

stable nymph
unborn mauve
#

Hmm

#

Hold on

#

There's the await callback(interaction) that's for discord.ui.Button

#

Which is what you use in the add_item

#

Hmmm

stable nymph
unborn mauve
stable nymph
#

Waaaat

#

I don't get it how these docs work lol

unborn mauve
#

OH

#
view = discord.ui.View()

customButton = discord.ui.Button(label="Test Button", style=discord.ButtonStyle.green)

async def customButton_callback(interaction):
  await interaction.response.send_message("Hello!")

customButton.callback = customButton_callback

view.add_item(customButton)

await client.get_channel(channelId).send(embed=embed, view=view)

Try that

#

I guess you could also use customButton.callback = await interaction.response.send_message("Hello!")

stable nymph
unborn mauve
#

Hmmm

stable nymph
#
  • how do I access the user who pressed the button with that?
unborn mauve
#

And

#
view = discord.ui.View()

customButton = discord.ui.Button(label="Test Button", custom_id="testButton1" style=discord.ButtonStyle.green)

async def customButton_callback(interaction):
  await interaction.response.send_message("Hello!")

view.timeout = None
customButton.callback = customButton_callback

view.add_item(customButton)
bot.add_view(view)

await client.get_channel(channelId).send(embed=embed, view=view)

I think?

stable nymph
#

Lemeee try

unborn mauve
#

Or client.add_view() if you're using client instead of bot

stable nymph
#

Yeah ik

#

There's no difference between that and the previous piece of code you sent thonk

unborn mauve
#

In function?

#

Or in code?

stable nymph
#

In function

#

We somehow need to store the view and then use add_view in the on_read event I assume

unborn mauve
#

Yeah

stable nymph
#

Ok, I got restarting to work

#

Now all I need is access the user who clicked the button Thonking

unborn mauve
#

interaction.user ?

stable nymph
#

OH

#

YES

#

One more thing xdxdxd

#

How can I send the user an ephemeral message?

unborn mauve
#

ephemeral=True ?

#

In the interaction.send_message

stable nymph
#

Oh wait

#

My brain just died or smth lol

unborn mauve
#

What's the code?

stable nymph
#

Nah, I got it, my brain just wasn't functional lol

#

Thanks a lot wolflove

wintry oakBOT
unborn mauve
#

Hahah, I linked you the interaction docs a while ago

unborn mauve
#

You can see all the attributes it has got that you can use