Search code examples
pythondiscorddiscord.pybotsnextcord

Clear reactions in setting time [nextcord.py]


I want clear or disable reactions in setting time using nextcord.py

I making poll command and wanna delete or disable(I dont know can I or not) reactions in setting time

I have code like this

    @commands.command()
    @commands.has_permissions(administrator=True)
    async def poll(self, ctx, time: int, title, *options):
        await ctx.message.delete()
        if time == None:
            await ctx.send("Укажите время!", delete_after = 5)
        elif title == None:
            await ctx.send("Укажите название!", delete_after = 5)
        elif options == None:
            await ctx.send("Укажите вопросы!", delete_after = 5)

        
        if len(options) >= 6:
            await ctx.send("Максимальное количество вопросов - 6!")
        else:
            time1 = datetime.now()
            time2 = timedelta(minutes=time)
            time_plus = time1 + time2
            time_plus = nextcord.utils.format_dt(time_plus,style = "T")

            embed = nextcord.Embed(color = 0xFFFFFF, title = title, description=f"""
            Опроc закончится в {time_plus}\n
            """)
            embed.set_thumbnail(url = "https://s10.gifyu.com/images/ezgif.com-gif-maker2423fb57189d7e5fb.gif")
            if len(options) == 1:
                embed.add_field(name = f"1",value=f"{options[0]}")
                m = await ctx.send(embed = embed)
                await m.add_reaction("1️⃣")
                if datetime.now() == time_plus:
                    m.clear_reaction()
                    
            if len(options) == 2:
                embed.add_field(name = f"1",value=f"{options[0]}")
                embed.add_field(name = f"2",value=f"{options[0]}")
                m = await ctx.send(embed = embed)
                await m.add_reaction("1️⃣")
                await m.add_reaction("2️⃣")

I have my time time_plus and wanna do something when time invoke


Solution

  • You can use reaction.clear or reaction.remove to remove reaction from your message.
    Here's an example to remove specific reaction from user by using on_raw_reaction_add

    @bot.event
    async def on_raw_reaction_add(payload):
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        emoji = payload.emoji.name
    
        if message.author.id != bot.user.id or payload.member.id == bot.user.id:
            return
        if emoji == '1️⃣': #if user react 1️⃣ to your message   
            #do your stuff here
            reaction = nextcord.utils.get(message.reactions, emoji=emoji)
            await reaction.remove(payload.member) #remove the reaction