'The verification system only works the first time. After the first button click, it says "Interaction failed."
Hey there,
I'm currently creating a verification system for my server that can detect if a user has already verified or not. To explain thoroughly, I have a command that sends an embed to a channel with a button. When you press the button, it should give you a role that makes you verified.
This interaction should also detect if you are already verified in the server, meaning you already have the role.
I made a code that acts a little weird. When I do the command to post the embed with the verify button, the button only works as intended on the first click. The second click says,
"This interaction failed,"
and I'm unsure what's going on.
Here's the code:
#Verification System
@client.command()
async def acceptterms(ctx):
channel = client.get_channel(961292090522869771)
verifyRole = discord.utils.get(ctx.guild.roles, name='Member')
verifyEmbed = discord.Embed(title='**Welcome to the Discord server.**', description='To get access to the rest of the server, you must agree to the our rules and Discord [community guidelines](https://discord.com/guidelines).\n\n**To agree and get access to the server, click on the button below this message.**', color=0xa7f0a7)
message = await channel.send(embed=verifyEmbed, components = [
Button(label= 'I agree to the server rules and Discord community guidelines.', custom_id='button1', style=ButtonStyle.blue)
])
interaction = await client.wait_for("button_click", check = lambda i: i.custom_id == "button1")
if verifyRole in ctx.author.roles:
await interaction.send(content = "You are already verified and have access to the entire server.")
else:
await interaction.send(content = "You are now verified! Welcome to the server!")
await ctx.author.add_roles(verifyRole)
At the bottom, you can see that the interaction waits for a button click. When the button is clicked, the interaction should respond according to with the "if" function. But it doesn't. Additionally, it only works one time after the embed is created.
Does anyone have a solution for this?
Thanks beforehand.
Solution 1:[1]
It is better to make a callback function and then add the code there.
@client.command()
async def acceptterms(ctx):
async def callback(interaction):
if interaction.user != ctx.author:
await interaction.response.send_message(content='You are not the author of this interaction.', ephemeral=True)
return
if verifyRole in interaction.user.roles:
await interaction.response.send_message(content='You already have the verification role.', ephemeral=True)
return
await ctx.author.add_roles(verifyRole)
await interaction.response.send_message(content='You have been verified!', ephemeral=True)
channel = discord.utils.get(ctx.guild.text_channels, id=961292090522869771)
verifyRole = discord.utils.get(ctx.guild.roles, name='Member')
verifyEmbed = discord.Embed(title='**Welcome to the Discord server.**', description='To get access to the rest of the server, you must agree to the our rules and Discord [community guidelines](https://discord.com/guidelines).\n\n**To agree and get access to the server, click on the button below this message.**', color=0xa7f0a7)
button = Button(label='I agree to the server rules and Discord community guidelines', style = discord.ButtonStyle.green()) # from discord.ui import Button
view = View() # from discord.ui import View
view.add_item(button)
button.callback = callback
await channel.send(embed=verifyEmbed, view=view)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sandy |
