英文:
How can i make a verification button on discord
问题
I've been playing around with making a verification button on discord, whenever I press the "Verify" button it gives me the error "This Interaction Failed" -> 我一直在尝试在Discord上创建一个验证按钮,每当我按下"验证"按钮时,它会给我一个错误消息:"This Interaction Failed"
英文:
I've been playing around with making a verification button on discord, whenever i press the "Verify" button it gives me the error "This Interaction Failed"
bot = commands.Bot(command_prefix=".", intents=discord.Intents.all(), help_command=None)
@bot.event
async def on_ready():
print("Bot is online!")
@bot.command()
async def verify(ctx):
await ctx.send('Click the button below to verify yourself')
def check_author(interaction):
return interaction.user.id == ctx.author.id
verify_button = discord.ui.Button(style=discord.ButtonStyle.green, label='Verify')
view = discord.ui.View()
view.add_item(verify_button)
message = await ctx.send(view=view)
try:
interaction = await bot.wait_for('button_click', timeout=30.0, check=check_author)
except asyncio.TimeoutError:
await message.edit(content='Verification timed out', view=None)
else:
member = ctx.author
role = discord.utils.get(ctx.guild.roles, name='Verified')
await member.add_roles(role)
await interaction.response.edit_message(content='You have been verified!', view=None)
答案1
得分: 1
在 discord.py
中处理用户交互的最佳方法是创建继承自 discord.ui.View
的类来生成视图:
class VerificationView(discord.ui.View):
def __init__(self, ctx: commands.Context):
super().__init__(timeout=30)
self.ctx = ctx
self.message = None
async def interaction_check(self, interaction: discord.Interaction) -> bool:
return interaction.user == self.ctx.author
async def start(self):
self.message = await self.ctx.send(content='Click the button below to verify yourself', view=self)
@discord.ui.button(label="Verify", style=discord.ButtonStyle.green)
async def verify(self, interaction: discord.Interaction, button: discord.Button):
member = interaction.user
role = discord.utils.get(self.ctx.guild.roles, name='Verified')
await member.add_roles(role)
await interaction.response.edit_message(content='You have been verified!', view=None)
async def on_timeout(self):
await self.message.edit(content='Verification timed out', view=None)
准备好视图后,你只需要在命令中使用 start()
方法:
@bot.command()
async def verify(ctx: commands.Context):
view = VerificationView(ctx)
await view.start()
英文:
The best way to work with user interactions in discord.py
is to create views from classes that inherit discord.ui.View
. Here's how you could build your view:
class VerificationView(discord.ui.View):
def __init__(self, ctx: commands.Context):
super().__init__(timeout=30)
self.ctx = ctx
self.message = None
async def interaction_check(self, interaction: discord.Interaction) -> bool:
return interaction.user == self.ctx.author
async def start(self):
self.message = await self.ctx.send(content='Click the button below to verify yourself', view=self)
@discord.ui.button(label="Verify", style=discord.ButtonStyle.green)
async def verify(self, interaction: discord.Interaction, button: discord.Button):
member = interaction.user
role = discord.utils.get(self.ctx.guild.roles, name='Verified')
await member.add_roles(role)
await interaction.response.edit_message(content='You have been verified!', view=None)
async def on_timeout(self):
await self.message.edit(content='Verification timed out', view=None)
With the view prepared, all you need to do is use the start()
method in your command:
@bot.command()
async def verify(ctx: commands.Context):
view = VerificationView(ctx)
await view.start()
答案2
得分: 0
我以前没有使用Python创建过Discord机器人,而是使用C#库创建过,但我认为在涉及交互模块时解决问题的方法类似。
对我而言,解决了这个问题的方法是在按下按钮时调用回调函数内的defer函数。我假设您正在使用discord.py。我发现函数的名称是相同的。
Discord.py - defer
这个函数的作用是确认交互并允许您执行耗时的任务,也许是调用数据库或等待来自外部API的响应。
英文:
I haven't created a discord bot on python before but instead created using a C# library but I think the fix is similar when it comes to interaction module.
For me what solved this issue was calling defer function within the callback function that gets called when the button is pressed. I assume you are using discord.py. I found the name of the function is identical.
Discord.py - defer
What this function does is that it acknowledges the interaction and allows you to do time consuming tasks, perhaps, call database or wait for response from an external API.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论