Discord stops responding on reaction after second iteration

33 views Asked by At

I have a problem with my discord bot. It stops working when I click on second iteration specifically, and it does nothing when I click on any other iteration. It's supposed to call check() function when I click a reaction.

import asyncio
from discord.ui import Button, View
import discord
from discord.commands import slash_command
from discord.ext import commands

class Test(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.stop_loop = False

    @slash_command(name='test', description='')
    async def test(self, ctx):

        def check(reaction, user):  # Our check for the reaction
            self.stop_loop = True
            print("clicked")
            return user == ctx.author and str(reaction.emoji) in ["✅"]


        for i in range(1, 100):
            if self.stop_loop:
                return

            if i == 1:
                embed = discord.Embed(title="1x")
                interaction: discord.Interaction = await ctx.respond(embed=embed)
                sent_embed = await interaction.original_response()
                await sent_embed.add_reaction('✅')
                await asyncio.sleep(2)

            embed = discord.Embed(title=f"{i}x")
            await interaction.edit_original_response(embed=embed)
            await sent_embed.add_reaction('✅')
            try:
                reaction, user = await self.bot.wait_for("reaction_add", check=check, timeout=2.0)
                print(reaction)
                if reaction:
                    embed = discord.Embed(title=f"You clicked on {i}")
                    await interaction.edit_original_response(embed=embed)
                    return
            except asyncio.exceptions.TimeoutError:
                await asyncio.sleep(1)

def setup(bot):
    bot.add_cog(Test(bot))

My result:

enter image description here

No errors are being thrown, but doesn't stop working.

1

There are 1 answers

0
Rexy On

Use event instead of bot.wait_for()

bot.wait_for() waits until the message gets a reaction. It will stop waiting when the message got it's first reaction besides the bot's reaction.

Use on_raw_reaction_add() or on_reaction_add() event. These are triggered when the message receive a new reaction by a user.