【问题标题】:button press command works, but still says interaction failed; using discord-components按钮按下命令有效,但仍然说交互失败;使用不和谐组件
【发布时间】:2021-09-06 10:10:08
【问题描述】:

代码做了应该做的事情,但每次按下按钮后都会显示“此交互失败”。按下按钮编辑嵌入以将其更改为另一个。按下按钮后如何消除交互失败消息?

问题:https://i.stack.imgur.com/i4dTd.png

收到的代码来自:https://github.com/elixss/YouTube/blob/main/source/buttons.py

代码如下:

@bot.group(invoke_without_command=True)
async def help(ctx):

    # buttons
    one = Button(style=1, label="Commands", id="em1")
    two = Button(style=1, label="Depression", id="em2")
    three = Button(style=1, label="Moderator", id="em3")
    four = Button(style=1, label="Language", id="em4")

    # embeds
    em1 = Embed(title="Commands Plugin",color=0x5865F2)
    em2 = Embed(title="Depression Plugin", description="placeholder", color=0x5865F2)
    em3 = Embed(title="Moderator Plugin", description="placeholder", color=0x5865F2)
    em4 = Embed(title="Language Plugin", description="placeholder", color=0x5865F2)

    # main help embed
    help_embed = Embed(description="> **Help Module**\nPress on any button to view the commands for that selected plugin.",
                      color=discord.Color.random())

    # buttons to embeds
    buttons = {
        "em1": em1,
        "em2": em2,
        "em3": em3,
        "em4": em4
    }

    msg = await ctx.send(embed=help_embed,
                         components=[[one, two, three, four]])
    while True:
        event = await bot.wait_for("button_click", timeout=60.0)

        if event.channel is not ctx.channel:
            return

        if event.channel == ctx.channel:
            response = buttons.get(event.component.id)
            await msg.edit(embed=response)

            if response is None:
                await event.channel.send("error, try again.")

【问题讨论】:

  • 嵌入应该是discord.Embed() 而不仅仅是Embed()
  • @ChaoticNebula 不。他本可以使用from discord import Embed 而不是import discord。这没有什么问题。
  • 也许..他直接导入了? @ChaoticNebula
  • 是的,我用过from discord import Embed
  • 我今天学到的新东西????

标签: discord.py


【解决方案1】:

正如 Elias 所说,您必须对交互做出响应,否则它会显示“此交互失败”,但不是使用常规的 ctx.send(),而是使用(在您的情况下)

await event.respond(type=4, message="Responded!")

如果您不想发送消息作为对按钮单击或选择中的选择的响应,您可以只使用 type=6 而不发送消息:

await event.respond(type=6)

更多类型请查看documentation

【讨论】:

    【解决方案2】:

    最好的方法是在按下按钮后移除所有按钮,或者添加一个冷却时间,在一定时间后自动移除按钮。

    在你的情况下,它看起来像这样:

    msg = await ctx.send(embed=help_embed, components=[[one, two, three, four]])
    try: 
       res = await client.wait_for("button_click", timeout = 60)
    except asyncio.TimeoutError:
       await msg.edit(components = []) #removes buttons from message
    else:
       if res.author == message.author:
           #when button is clicked
           await msg.edit(components = [])
    

    这也可以防止速率受限,因此您不会有按钮不断等待响应。

    【讨论】:

    • 是的,这是一种方式,但这不是最好的方式。
    猜你喜欢
    • 2021-12-08
    • 2021-11-20
    • 2022-01-19
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    相关资源
    最近更新 更多