Discord.py - 按下按钮时它会发送消息
Discord.py - When a button is pressed it sends a message
我一直在尝试制作一个 python discord 机器人,它发送带有下方交互式按钮的嵌入式消息。到目前为止它看起来像这样:
虽然我不确定如何让按钮响应。当您单击该按钮时,它只会显示“此交互失败”
这是我的代码:
@bot.command()
async def question(ctx):
await ctx.message.delete()
e = discord.Embed(title="TRIVIA QUESTION", description="When did the dinosours become extinct?")
await ctx.send(embed=e, content="", components=[Button(style=ButtonStyle.red, label="60 million years ago", custom_id="button1", disabled = True), Button(style=ButtonStyle.red, label="65 million years ago", custom_id="button2")])
------------------------编辑-------------------- -----
查看 hYg-Cain (https://devkiki7000.gitbook.io/discord-components/guide/getting-started#send-selects) 评论的文档,我将这一行放在我的代码中:
res = await bot.wait_for("button_click", timeout=10)
这会等待用户单击按钮,然后:
if res.custom_id == "button1":
await ctx.send("You got it right. ")
表示如果按下的按钮的 ID 为 button1,它会发送一条消息。
我认为你必须编写一个检查函数来获取单击按钮的 ID,然后通过 if/else stuff
进行处理
def check_button(i: discord.Interaction, button):
return i.author == ctx.author and i.message == msg
@bot.command()
async def question(ctx):
await ctx.message.delete()
e = discord.Embed(title="TRIVIA QUESTION", description="When did the dinosours become extinct?")
msg = await ctx.send(embed=e, content="", components=[Button(style=ButtonStyle.red, label="60 million years ago", custom_id="button1", disabled = True), Button(style=ButtonStyle.red, label="65 million years ago", custom_id="button2")])
interaction, button = await client.wait_for('button_click', check=check_button)
if button.custom_id == "button1":
# do stuff
elif button.custom_id == "button2":
# do other stuff
else:
# how did we get here
然而,作为 select 选项可能更容易实现
https://devkiki7000.gitbook.io/discord-components/guide/getting-started#send-selects
你不会检查你的互动。
interaction = await bot.wait_for("button_click", check = lambda i: i.custom_id == "button1")
你可以像这样使用它
@bot.command()
async def button(ctx):
await ctx.send(
"When did the dinosours become extinct?",
components = [
Button(label="60 million years ago", custom_id = "button1", disabled = True),
Button(label="65 million years ago", custom_id="button2")
]
)
interaction = await bot.wait_for("button_click", check = lambda i: i.custom_id == "button1")
await interaction.send(content = "Button clicked!")
我一直在尝试制作一个 python discord 机器人,它发送带有下方交互式按钮的嵌入式消息。到目前为止它看起来像这样:
虽然我不确定如何让按钮响应。当您单击该按钮时,它只会显示“此交互失败”
这是我的代码:
@bot.command()
async def question(ctx):
await ctx.message.delete()
e = discord.Embed(title="TRIVIA QUESTION", description="When did the dinosours become extinct?")
await ctx.send(embed=e, content="", components=[Button(style=ButtonStyle.red, label="60 million years ago", custom_id="button1", disabled = True), Button(style=ButtonStyle.red, label="65 million years ago", custom_id="button2")])
------------------------编辑-------------------- -----
查看 hYg-Cain (https://devkiki7000.gitbook.io/discord-components/guide/getting-started#send-selects) 评论的文档,我将这一行放在我的代码中:
res = await bot.wait_for("button_click", timeout=10)
这会等待用户单击按钮,然后:
if res.custom_id == "button1":
await ctx.send("You got it right. ")
表示如果按下的按钮的 ID 为 button1,它会发送一条消息。
我认为你必须编写一个检查函数来获取单击按钮的 ID,然后通过 if/else stuff
进行处理def check_button(i: discord.Interaction, button):
return i.author == ctx.author and i.message == msg
@bot.command()
async def question(ctx):
await ctx.message.delete()
e = discord.Embed(title="TRIVIA QUESTION", description="When did the dinosours become extinct?")
msg = await ctx.send(embed=e, content="", components=[Button(style=ButtonStyle.red, label="60 million years ago", custom_id="button1", disabled = True), Button(style=ButtonStyle.red, label="65 million years ago", custom_id="button2")])
interaction, button = await client.wait_for('button_click', check=check_button)
if button.custom_id == "button1":
# do stuff
elif button.custom_id == "button2":
# do other stuff
else:
# how did we get here
然而,作为 select 选项可能更容易实现 https://devkiki7000.gitbook.io/discord-components/guide/getting-started#send-selects
你不会检查你的互动。
interaction = await bot.wait_for("button_click", check = lambda i: i.custom_id == "button1")
你可以像这样使用它
@bot.command()
async def button(ctx):
await ctx.send(
"When did the dinosours become extinct?",
components = [
Button(label="60 million years ago", custom_id = "button1", disabled = True),
Button(label="65 million years ago", custom_id="button2")
]
)
interaction = await bot.wait_for("button_click", check = lambda i: i.custom_id == "button1")
await interaction.send(content = "Button clicked!")