2
votes

I've been trying to make a command that will display some information and then, when i react to an emote, its supposed to display another set of information.

I tried to use parts of this, specifically the parts in line 335 to 393 to get it to work. However, it just does nothing. Not even an error message.

This is the code I use right now.

            def check_react(reaction, user):
            if reaction.message.id != msg.id:
                return False
            if user != ctx.message.author:
                return False
            return True
        res, user = await bot.wait_for('reaction_add', check=check_react, timeout=None,)
        if user != ctx.message.author:
            print('if user != ctx.message.author:')
        elif '⬅️' in str(res.emoji):
            page -=1
            print(page)
            embed = discord.Embed(title='generic title', description='generic description', color=0x52fffc)
            await msg.edit(embed=embed)
        elif '➡️' in str(res.emoji):
            page +=1
            print(page)
            embed = discord.Embed(title='generic title 2', description='generic description 2', color=0x52fffc)
            await msg.edit(embed=embed)

It seems to stop at

await bot.wait_for('reaction_add', ..)

why is that? and how can i make the code work? Thats in a cog btw. I'll happily provide more code if needed.

2
"It seems to stop at: await bot.wait_for('reaction_add)'". The function waits for an reaction_add. Or in other words it stops at that line.Deru

2 Answers

4
votes

Better and easier way is to use DiscordUtils

Here is an example:

@commands.command()
async def paginate(self, ctx):
    embed1 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 1")
    embed2 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 2")
    embed3 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 3")
    paginator = DiscordUtils.Pagination.CustomEmbedPaginator(ctx, remove_reactions=True)
    paginator.add_reaction('⏮️', "first")
    paginator.add_reaction('⏪', "back")
    paginator.add_reaction('🔐', "lock")
    paginator.add_reaction('⏩', "next")
    paginator.add_reaction('⏭️', "last")
    embeds = [embed1, embed2, embed3]
    await paginator.run(embeds)
0
votes

Forgot about this, and i figured it out eventually. Here's how i did it:

At first i put all the content of the pages in a list. Each entry is a page.

Then i define a function that will act as a check. This one will restrict the reaction listener to the actual embed that was sent and prevents anyone from reaction other than the command invoker. Sorry for any Formatting Errors.

def check(reaction, user):
   return reaction.message.id == msg.id and user == ctx.author #msg.id is the id of the embed sent by the bot.

page = 0

while True: #can be changed to a variable to let it work a certain amount of times.
   try:
       reaction, _ = await bot.wait_for('reaction_add', timeout= 20.0, check=check)
       if reaction.emoji == 'emote of choice here' and page > 0:
           page -= 1
           embed = discord.Embed(title='Title Here', description=pages[page]
           await msg.edit(embed=embed)
       if reaction.emoji == 'emote of choice here' and page < len(pages) -1:
           page += 1
           embed = discord.Embed(title='Title Here', description= pages[page]
           await msg.edit(embed=embed)
   except asyncio.TimeoutError:
      #do stuff here, could be pass or a message saying the timeout has been reached or something similar.