首页 > 解决方案 > 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()

我最近刚刚将它添加到我的代码中以检查两种反应,但无法找出问题所在。

标签: pythondiscorddiscord.pydiscord.py-rewrite

解决方案


我很确定你运行了两次程序,以确保不会发生这种情况,我建议实施一个终止开关,这样每次重新运行程序时,你首先在 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.

推荐阅读