discord.py-与多个按钮的互动

发布于 2025-02-11 06:02:47 字数 475 浏览 2 评论 0原文

我为我的Discord Bot创建了一个带有按钮的命令。 在下面的当前代码中,与蓝色按钮(Button1)的相互作用正常。绿色按钮的交互(Button3)不起作用。

如何为同一命令中的不同按钮创建不同的交互?

我正在使用discord.py和discord_components

@bot.command()
async def test(ctx):
    await ctx.send(
        "This is a button test.",
        components=[
            [
                Button( 
                    style=ButtonStyle.blue,
                    custom_id="button1",
                    label="Blue button",
                    emoji="
              

I've created a command with buttons for my Discord bot.
With the current code below, the interaction with the blue button (button1) works fine. Interaction for the green button (button3) does not work.

How do I create different interactions for different buttons within the same command?

I am using discord.py and discord_components

@bot.command()
async def test(ctx):
    await ctx.send(
        "This is a button test.",
        components=[
            [
                Button( 
                    style=ButtonStyle.blue,
                    custom_id="button1",
                    label="Blue button",
                    emoji="????",
                ),
                Button(
                    style=ButtonStyle.red,
                    custom_id="button2",
                    label="Red button",
                    disabled=True,
                    emoji="????",
                ),
                Button(
                    style=ButtonStyle.green,
                    custom_id="button3",
                    label="Green button",
                    emoji="????",
                ),
            ]
        ]
    )
    blue = await bot.wait_for(
        "button_click", check = lambda i: i.custom_id == "button1"
    )
    await blue.send(content="Button clicked!", ephemeral=False)
    green = await bot.wait_for(
        "button_click", check = lambda k: k.custom_id == "button3"
    )
    await green.send(content="Button smashed!", ephemeral=False)

The bot is currently waiting for the blue button to be pressed. Then the green button works. Obviously I want them to work at the same time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-02-18 06:02:47

您可以简单地检查按钮自定义ID是否为button1(蓝色)或button3(绿色)

    button = await bot.wait_for(
        "button_click", check = lambda i: i.custom_id in ["button1", "button3"]
    )

    if button.custom_id == "button1":
        await button.send(content="Button clicked!", ephemeral=False)
    else:
        await button.send(content="Button smashed!", ephemeral=False)

如果您希望按钮不断工作,请将代码放在一段时间内环形

You can simply check if the buttons custom id is either button1 (blue) or button3 (green)

    button = await bot.wait_for(
        "button_click", check = lambda i: i.custom_id in ["button1", "button3"]
    )

    if button.custom_id == "button1":
        await button.send(content="Button clicked!", ephemeral=False)
    else:
        await button.send(content="Button smashed!", ephemeral=False)

If you want the buttons to constantly work, put the code above into a while loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文