【问题标题】:Bot sends message twiceBot 两次发送消息
【发布时间】: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


【解决方案1】:

您的代码中没有任何内容应该发送两次消息,您可能正在运行两个版本的代码。打开任务管理器并杀死任何不需要的 python 进程或重新启动计算机

【讨论】:

  • 好吧,我正在 repl.it 中运行机器人。代码有 12 个不同的文件,因为我正在为每个团队制作带有表情符号前缀的机器人。我知道我可以将所有内容合二为一,但我将其分开以进行更多定制。这会影响什么吗?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我曾经遇到过这个问题,主要原因是:Heroku。 如果您将机器人托管在其他地方,也许您应该更改它们的前缀,以免同时触发它们!

    【讨论】:

    • 好吧,我将它托管在不同的地方,但我确实更改了前缀。它只在需要响应的命令上发送消息两次。我不确定在检测到第一反应后是否可以使用 killswitch,所以请告诉我是否可以@Phan Nhật Huy
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 2021-06-17
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多