这是我的机器人中的测试命令。
@tree.command(name="button", description="testing")
async def button(interaction):
view = discord.ui.View() # make a view
style = discord.ButtonStyle.primary # makes the button blue
item = discord.ui.Button(style=style, label="Click to continue!", custom_id="continue_button") # make the button
view.add_item(item=item) # add button to view
await interaction.response.send_message("Waiting until you press the button!", view=view) # send message with button
def check(i):
return i.user.id == interaction.user.id and i.custom_id == "continue_button"
try:
interaction, button = await client.wait_for("button_click", check=check, timeout=10)
# when button is pressed
await interaction.channel.send("You pressed the button!")
except asyncio.TimeoutError:
# button wasn't pressed before timeout
await interaction.channel.send("You didn't press the button within the specified time.")
但是,机器人永远不会获取按钮单击事件!即使检查没有任何内容,没有自定义 ID 等,机器人也会超时并单击按钮导致交互失败。另外,我想保留按钮并在命令中监听,以便我可以根据用户按下的按钮执行更多代码。
我希望机器人发送一条消息,表明我已单击了按钮,但即使在按下按钮后,我总是会超时。我尝试删除检查、超时等,但我无法让事件发生。
我也在学习,但我不认为你是在调用检查函数/输入参数
我最近也遇到这个问题。问题是,仅当您使用
"button_click"
库时,client.wait_for
的 discord-components
事件才存在。
使用
callback
来完成您正在做的事情会好得多。
此外,您还希望在超时时收到响应消息。所以最好使用一个类。
class MyButtonView(discord.ui.View):
@discord.ui.button(label="Click to continue!", style=discord.ButtonStyle.primary)
async def mybutton_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message("You pressed the button!")
@tree.command(name="button", description="testing")
async def button(interaction):
await interaction.response.send_message("Waiting until you press the button!", view=MyButtonView()) # send message with button
这是按钮的基线。
您想要检查单击该按钮的人是否也是使用该命令的人。 我们可以通过在类中创建我们自己的
interaction_check
方法来实现这一点。
Python解释:
MyButtonView
类继承discord.ui.View
类,该类有方法interaction_check
。通过编写我们自己的,我们可以覆盖它。
在该方法中,只需检查作者是否是交互用户。我们通过解析参数来获取作者,需要
__init__
。
class MyButtonView(discord.ui.View):
def __init__(self, author):
self.author = author
@discord.ui.button(label="Click to continue!", style=discord.ButtonStyle.primary)
async def mybutton_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message("You pressed the button!")
async def interaction_check(self, interaction: discord.Interaction):
return self.author.id == interaction.user.id
由于
MyButtonView
现在需要 author
参数,我们必须在发送按钮消息时解析该参数。
await interaction.response.send_message("...", view=MyButtonView(interaction.user))
现在我们希望它在超时时发送消息。再次,我们制定了自己的方法
on_timeout
。然而,这没有任何参数,所以我们必须自己解析interaction
。
class MyButtonView(discord.ui.View):
def __init__(self, interaction):
self.interaction = interaction
@discord.ui.button(label="Click to continue!", style=discord.ButtonStyle.primary)
async def mybutton_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message("You pressed the button!")
async def interaction_check(self, interaction: discord.Interaction):
return self.interaction.user.id == interaction.user.id
async def on_timeout(self): # No interaction argument
await self.interaction.channel.send("You didn't press the button within the specified time.")
这也消除了对
author
参数的需要,因为我们可以从 interaction
参数中得到相同的结果。
请注意
__init__
、self.interaction
和按钮 interaction
中解析为参数的交互之间的区别。
有关按钮的更多信息,discord.ui.Button、discord.Interaction、discord.ui.View、Python 类
最后一点:抱歉回答这么长,我想深入了解一下,以便您充分了解自己在做什么。
该问题是由于没有button_click事件引起的。相反,寻找由按钮引起的交互:
interaction = await client.wait_for('interaction',
check=lambda i:
i.type == discord.InteractionType.component and
i.user == interaction.user and
i.channel == interaction.channel, timeout=30)