在discord.py中,我如何检查哪个按钮被点击了?Discord Bot



在这段代码中,变量交互是一个奇怪的计算机省略元素。我想知道点击了哪个按钮,这样我就可以检查用户点击的按钮是否正确。

if f==1:
await ctx.send("What was the color next to the word "+word_1+"?", components = [
[Button(label=":"+color_1+"_circle: "+color_1.upper(),style="3", custom_id="button1"), Button(label=":"+color_2+"_circle: "+color_2.upper(), style="3", custom_id="button2"), Button(label=":"+color_3+"_circle: "+color_3.upper(), style="3", custom_id="button3")]
])
answer=color_1
if f==2:
await ctx.send("What was the color next to the word "+word_2+"?", components = [
[Button(label=":"+color_1+"_circle: "+color_1.upper(),style="3", custom_id="button1"), Button(label=":"+color_2+"_circle: "+color_2.upper(), style="3", custom_id="button2"), Button(label=":"+color_3+"_circle: "+color_3.upper(), style="3", custom_id="button3")]
])
answer=color_2
if f==3:
await ctx.send("What was the color next to the word "+word_3+"?", components = [
[Button(label=":"+color_1+"_circle: "+color_1.upper(),style="3", custom_id="button1"), Button(label=":"+color_2+"_circle: "+color_2.upper(), style="3", custom_id="button2"), Button(label=":"+color_3+"_circle: "+color_3.upper(), style="3", custom_id="button3")]
])
answer=color_3
#interaction = await bot.wait_for("button_click",timeout=7)
#await interaction.send(content = "Button clicked!", ephemeral=False)
#def check(x):
#return x.author!=bot.user and x.author==ctx.author
try:
interaction = await bot.wait_for("button_click",timeout=7, check = lambda i: i.custom_id=="button1" or i.custom_id=="button2" or i.custom_id=="button3")

await interaction.send(ctx.custom_id)
#msg = await bot.wait_for('message', timeout=15, check=check)
except asyncio.TimeoutError:
a=random.randint(1,20)
await ctx.send("Too late! You will now face a consequence.")
else:
answeru=""

print(interaction)
if interaction==answer:
a=random.randint(20,100)
print(a)
await ctx.send(":white_check_mark: Good job!")
a+=users[str(user.id)]["WLuck"]
print(a)
#a=4```

通过使用此代码,您可以监听与按钮和上下文菜单等组件的交互

@bot.event
async def on_component(ctx):
# your code here
if f==1:
await ctx.send("What was the color next to the word "+word_1+"?", components = [
[Button(label=":"+color_1+"_circle: "+color_1.upper(),style="3", custom_id="button1"), Button(label=":"+color_2+"_circle: "+color_2.upper(), style="3", custom_id="button2"), Button(label=":"+color_3+"_circle: "+color_3.upper(), style="3", custom_id="button3")]
])
answer=color_1
if f==2:
await ctx.send("What was the color next to the word "+word_2+"?", components = [
[Button(label=":"+color_1+"_circle: "+color_1.upper(),style="3", custom_id="button2"), Button(label=":"+color_2+"_circle: "+color_2.upper(), style="3", custom_id="button2"), Button(label=":"+color_3+"_circle: "+color_3.upper(), style="3", custom_id="button3")]
])
answer=color_2
if f==3:
await ctx.send("What was the color next to the word "+word_3+"?", components = [
[Button(label=":"+color_1+"_circle: "+color_1.upper(),style="3", custom_id="button3"), Button(label=":"+color_2+"_circle: "+color_2.upper(), style="3", custom_id="button2"), Button(label=":"+color_3+"_circle: "+color_3.upper(), style="3", custom_id="button3")]
])
answer=color_3
@buttons.click
async def button1(ctx):
#write the code of button 1
...... #then write the code of button 2
#be careful on the custom id

最新更新