#🔒 Discord.py Buttons not working

15 messages · Page 1 of 1 (latest)

worldly pumice
#

Here’s the code:

from discord.ext import commands
from discord.ui import Button, View
import logging

intents = discord.Intents.all()
intents.messages = True

# Configure logging
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print('Bot is ready!')

@bot.command()
async def verify(ctx):
    # Creating a button component
    button = Button(style=discord.ButtonStyle.green, label="✅ Verify")

    # Creating the embed
    embed = discord.Embed(
        title='Verification',
        description='Please click the button below to verify',
        color=0x00ff00  # green color
    )

    # Creating the view and adding the button
    view = View()
    view.add_item(button)

    # Sending the message with the button
    message = await ctx.send(embed=embed, view=view)

@bot.event
async def on_button_click(interaction):
    if isinstance(interaction, discord.ButtonClick):
        if interaction.component.label == "✅ Verify":
            await interaction.response.send_message("Please complete the captcha to verify.")
            await handle_verification(interaction.guild, interaction.user)

async def handle_verification(guild, user):
    try:
        member = await guild.fetch_member(user.id)
        role = discord.utils.get(guild.roles, name='Verified')
        if role:
            await member.add_roles(role)
            await user.send('You have been verified successfully!')
        else:
            await user.send('Verification failed: Role not found.')
    except Exception as e:
        logger.error(f'Error handling verification: {e}')  # Log error to console

bot.run('token') ```

I keep attempting and it keeps saying interaction failure and there’s no errors in console
wooden torrentBOT
#

@worldly pumice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @worldly pumice!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
worldly pumice
fringe valve
#

@bot.event
async def on_button_click(interaction):

im pretty sure thats not a real event

#

but ill double check the docs just to make sure

#

it looks like it is a thing but used in a different way if im looking at it right, i think you want something else if youll give me a second

#
class VoteNoButton(discord.ui.Button):
  async def callback(self):
    button_press_code

#and in your command creating the view
view.add_item(VoteNoButton)

thats at least how ive done it with my view

worldly pumice
#

Do I copy it from that doc?

fringe valve
#

i would recommend you borrow from my example bc it appears to be using different styles than youve been learning

#

my example works with what you have where the doc doesnt

wooden torrentBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.