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