Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

python - Trying to make a "yes/no" reaction confirmation but bot does not respond to reactions

I am trying to make a confirmation poll on my code so that my bot will not clear messages without confirming that I want to do so.

I'm trying to do that with a reaction poll, where when I enter the command, it will send a message along with the tick and cross reactions. If I react with a tick emoji (<:nonatick:803586318369292289>), it will proceed to purge messages, while if I react with a cross emoji (<:RedTick:801684348502933525>), it will send another message and do nothing else.

I have gotten the part where it sends Are you sure you want to delete 1 messages? along with the bot's reactions. However, it does not respond further if I react to either of the emojis.

@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 are enabled in my code:

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

This is what happens when I use the command. This

Is it a problem with how it checks whether I have reacted?

Thanks for any help in advance!

question from:https://stackoverflow.com/questions/65902886/trying-to-make-a-yes-no-reaction-confirmation-but-bot-does-not-respond-to-reac

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can do this using 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. ")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...