python discord.py bot按钮事件未触发

u5rb5r59  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(165)

discord机器人发送一个带有2个按钮的交互式消息。通常情况下,触发的事件应该根据按下的按钮发布答案,但discord仅在聊天中回复:此交互失败。
(我使用的是discord.Client())
我尝试添加第二个(交互)事件-没有效果
我添加了await interaction.respond(type=6),它应该告诉服务器
一个答案将被发送的机器人-没有效果
同样没有效果我已经添加了intents = discord.Intents.default()
intents.messages = True
intents.reactions = True
client = discord.Client(intents=intents)(on_message事件工作正常)
为什么按钮事件没有被触发?(控制台中也没有出现print语句)
(The#print(f“Send by:{ctx.author.name}”)未评论时打印)
(Do我可能需要一个defer()来让服务器等待更长的时间?)
现在代码部分:

@slash.slash(
    name="testPopup",
    description="A funny little popup something",
    guild_ids=[GUILD_TOKEN_MAIN_TESTING,GUILD_TOKEN_COMMUNITY_SERVER]
)
async def modal(ctx: SlashContext):
    # Define the components of the modal message (two blue OK buttons)
    components = [
        manage_components.create_actionrow(
            manage_components.create_button(
                style=ButtonStyle.blue,
                label="OK",
                custom_id="button1"
            ),
            manage_components.create_button(
                style=ButtonStyle.blue,
                label="OK",
                custom_id="button2"
            )
        )
    ]

    # Send the message with the defined components
    msg = await ctx.send(content="This is a message with two OK buttons.", components=components)

    try:
        #print(f"Send by: {ctx.author.name}")
        # Define a function that will handle the button clicks
        def check(res):
            print("click recognized")
            return res.user == ctx.author and res.channel == ctx.channel

        # Wait for a button click from the user who triggered the command
        interaction = await client.wait_for("button_click", check=check, timeout=60)
        # Tell discord server that the bot wants to answer
        await interaction.respond(type=6)

        # Check which button was clicked and send a response in the chat
        if interaction.component.custom_id == "button1":
            print("ok")
            await ctx.channel.send(content="You clicked OK 1.")
        elif interaction.component.custom_id == "button2":
            await ctx.channel.send(content="You clicked OK 2.")

    except asyncio.TimeoutError:
        # If the user does not click a button within the timeout period, disable the buttons in the modal message
        components[0]["components"][0]["disabled"] = True
        components[0]["components"][1]["disabled"] = True
        await msg.edit(components=components)

#Other test event
@client.event
async def on_interaction(interaction):
    print('Event')
    await interaction.respond(content='Something happened!')
r7s23pms

r7s23pms1#

我想这应该对你有帮助https://guide.pycord.dev/interactions/ui-components/buttons这里有你需要的一切,它在某种程度上帮助了我

相关问题