我刚刚想出了如何在 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()
我最近刚刚将这个添加到我的代码中,以检查这两种反应,但无法找出问题所在。
答案 0 :(得分:1)
我曾经遇到过这个问题,主要原因是:Heroku。 如果您将机器人托管在其他地方,也许您应该更改它们的前缀,以免同时触发它们!
答案 1 :(得分:0)
您的代码中没有任何内容应该发送两次消息,您可能正在运行两个版本的代码。打开任务管理器并杀死所有不需要的 python 进程或重新启动计算机
答案 2 :(得分:0)
我很确定你运行了两次程序,以确保不会发生这种情况,我建议实现一个终止开关,所以每次重新运行程序时,你首先在 Discord 上激活终止开关,然后再次运行该程序.我制作的 killswitch 的一个例子是这样的:
@client.command()
async def quit(ctx):
await ctx.send("Shutting down the bot")
return await client.logout() # this just shuts down the bot.