Discord.py 不响应按钮事件

问题描述 投票:0回答:3

这是我的机器人中的测试命令。

@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 等,机器人也会超时并单击按钮导致交互失败。另外,我想保留按钮并在命令中监听,以便我可以根据用户按下的按钮执行更多代码。

我希望机器人发送一条消息,表明我已单击了按钮,但即使在按下按钮后,我总是会超时。我尝试删除检查、超时等,但我无法让事件发生。

python async-await discord discord.py discord-buttons
3个回答
0
投票

我也在学习,但我不认为你是在调用检查函数/输入参数


0
投票

我最近也遇到这个问题。问题是,仅当您使用

"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.Buttondiscord.Interactiondiscord.ui.ViewPython 类

最后一点:抱歉回答这么长,我想深入了解一下,以便您充分了解自己在做什么。


0
投票

该问题是由于没有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)
© www.soinside.com 2019 - 2024. All rights reserved.