python - Bot 两次发送消息
问题描述
我刚刚想出了如何检查 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()
我最近刚刚将它添加到我的代码中以检查两种反应,但无法找出问题所在。
解决方案
我很确定你运行了两次程序,以确保不会发生这种情况,我建议实施一个终止开关,这样每次重新运行程序时,你首先在 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.
推荐阅读
- multithreading - 在非静态方法中使用同步块锁定实例
- java - 你如何搜索二维数组中的任何索引?
- python-3.x - 选择元素的文本,但不选择子项的文本
- ruby-on-rails - 重定向到 Ruby on Rails 中的另一个页面
- java - Java while循环中的用户验证测试
- printf - 在 OCaml 中的列表中打印元素
- php - 在codeigniter中批量上传图片
- python - 如何用python下载python?
- apache-poi - 如何使用 POI 4.0.0 绘制 2 线时间序列?
- java - SparkSession 从哪里获取 AWS 凭证?SparkSession 还是 HadoopConfiguration?