试图做出“是/否”反应确认,但机器人不响应反应

时间:2021-01-26 14:08:40

标签: python discord discord.py

我正在尝试对我的代码进行确认轮询,以便我的机器人不会在没有确认我想要这样做的情况下清除消息。

我正在尝试通过反应调查来做到这一点,当我输入命令时,它会发送一条消息以及 tickcross 反应。如果我用 tick 表情符号 (<:nonatick:803586318369292289>) 做出反应,它将继续清除消息,而如果我用 表情符号 (<:RedTick:801684348502933525> ),它将发送另一条消息,不执行任何其他操作。

我已经得到了它发送 Are you sure you want to delete 1 messages? 以及机器人的反应的部分。但是,如果我对任何一个表情符号做出反应,它都不会进一步响应。

@client.command(pass_context=True)
@commands.has_permissions(manage_messages=True)
async def clear(ctx, number):
    number = int(number)
    moreoma = ctx.author.id
    message = await ctx.send("Are you sure you want to delete " + str(number) + " messages?")
# Adds reaction to above message
    for emoji in ('<:nonatick:803586318369292289>', '<:RedTick:801684348502933525>'):
        await message.add_reaction(emoji)

    def check(reaction, user):
        return user.id == moreoma and str(reaction.emoji) in ['<:nonatick:803586318369292289>',
                                                              '<:RedTick:801684348502933525>']

    reaction, user = await client.wait_for('reaction_add', timeout=5, check=check)
    if reaction.emoji == '<:nonatick:803586318369292289>':
        await ctx.channel.purge(limit=number + 2, check=lambda msg: not msg.pinned)
    elif reaction.emoji == '<:RedTick:801684348502933525>':
        await ctx.send("aight looks like we are not clearing messages as of now. ")

在我的代码中启用了意图:

intents = discord.Intents(messages=True, guilds=True)
intents.reactions = True
intents.members = True

这是我使用命令时发生的情况。 This

它检查我是否有反应的方式有问题吗?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用 try/except

@client.command(pass_context=True)
@commands.has_permissions(manage_messages=True)
async def clear(ctx, number):
    number = int(number)
    moreoma = ctx.author.id
    message = await ctx.send("Are you sure you want to delete " + str(number) + " messages?")

    emojis = ['<:nonatick:803586318369292289>', '<:RedTick:801684348502933525>']

    # Adds reaction to above message
    for emoji in (emojis):
        await message.add_reaction(emoji)

    def check(reaction, user):
        reacted = reaction.emoji
        return user.id == moreoma and str(reaction.emoji) in emojis

    try:
        reaction, user = await client.wait_for('reaction_add', timeout=10, check=check)
    except asyncio.TimeoutError:
        await ctx.send("timeout")
    else:
        if str(reacted) == '<:nonatick:803586318369292289>':
            await ctx.channel.purge(limit=number + 2, check=lambda msg: not msg.pinned)
        elif reaction.emoji == '<:RedTick:801684348502933525>':
            await ctx.send("aight looks like we are not clearing messages as of now. ")