【发布时间】:2021-01-04 12:21:16
【问题描述】:
我刚刚想出了如何在 discord.py 中检查反应,但现在我遇到了另一个问题。当只发生一种反应时,机器人会为两种反应发送两条消息。
@bot.command()
@commands.has_any_role("Franchise Owner", "General Manager", "Head Coach")
async def offer(ctx, member:discord.Member):
embed = discord.Embed()
embed.add_field(name="<a:Loading:768095883664424971> Incoming Offer", value=f"The <:DallasCowboys:788796627161710592> have offered {member.mention}.")
offer_sent = await ctx.send(embed=embed)
await offer_sent.add_reaction("<a:CheckMark:768095274949935146>")
await offer_sent.add_reaction("<a:XMark:768095331555606528>")
await member.send("You have been offered to the <:DallasCowboys:788796627161710592>. You have 30 minutes to accept/decline.")
channel = ctx.channel
def check(reaction, member):
return member == member and str(reaction.emoji) == '<a:CheckMark:768095274949935146>'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=1800.0, check=check)
except asyncio.TimeoutError:
await channel.send(f"{member.mention} hasn't reacted in time.")
else:
await channel.send(f"{ctx.author.mention}, {member.mention} has accepted <:DallasCowboys:788796627161710592> offer.")
def check(reaction, member):
return member == member and str(reaction.emoji) == '<a:XMark:768095331555606528>'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=1800.0, check=check)
except asyncio.TimeoutError:
await channel.send(f"{member.mention} hasn't reacted in time.")
else:
await channel.send(f"{ctx.author.mention}, {member.mention} has declined <:DallasCowboys:788796627161710592> offer.")
await asyncio.sleep(1800)
await offer_sent.delete()
我最近刚刚将此添加到我的代码中以检查两种反应,但无法找出问题。
【问题讨论】:
-
我测试了代码,但无法重现问题。试试 LaughlanMcG 所说的,如果不起作用,请考虑添加更多细节。
-
检查您是否多次运行代码,您将不得不关闭其他版本。如果这不是问题,可能是因为您正在使用
on_message()事件。process_commands()可能被多次调用。 -
哦,好吧,当我测试时,我发现只有当它们对两者都做出反应时才会发送它们。有没有办法一有反应就结束命令?
标签: python discord discord.py discord.py-rewrite