7

I am using discord.py to make a bot, and there are more commands than can fit on one page for my custom help command. I want the bot to add 2 reactions, back and forward, then the user that sent the help message can pick one, and go onto different pages of the help command. I want the bot to be able to edit the message to show the second page, and if they go back, then edit back to the original first page. Could anyone help with this? This is similar to the owobot definitions, where you can scroll back and forth between definitions.

Oblique
  • 560
  • 1
  • 4
  • 18
  • 1
    Here's an example of pagination for a regular command, should be easy to adapt to the help command: https://stackoverflow.com/questions/51796005/reaction-pagination-button-forward-and-back-python/51801449#51801449 – Patrick Haugh May 14 '20 at 01:44
  • Discord.py menu pagination could make your life easier [menus](https://github.com/Rapptz/discord-ext-menus) – Ceres Feb 20 '21 at 12:10

3 Answers3

24

This method would be using Client.wait_For(), and can be easily adapted if you have any other ideas for it.

Example

@bot.command()
async def pages(ctx):
    contents = ["This is page 1!", "This is page 2!", "This is page 3!", "This is page 4!"]
    pages = 4
    cur_page = 1
    message = await ctx.send(f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
    # getting the message object for editing and reacting

    await message.add_reaction("◀️")
    await message.add_reaction("▶️")

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]
        # This makes sure nobody except the command sender can interact with the "menu"

    while True:
        try:
            reaction, user = await bot.wait_for("reaction_add", timeout=60, check=check)
            # waiting for a reaction to be added - times out after x seconds, 60 in this
            # example

            if str(reaction.emoji) == "▶️" and cur_page != pages:
                cur_page += 1
                await message.edit(content=f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
                await message.remove_reaction(reaction, user)

            elif str(reaction.emoji) == "◀️" and cur_page > 1:
                cur_page -= 1
                await message.edit(content=f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
                await message.remove_reaction(reaction, user)

            else:
                await message.remove_reaction(reaction, user)
                # removes reactions if the user tries to go forward on the last page or
                # backwards on the first page
        except asyncio.TimeoutError:
            await message.delete()
            break
            # ending the loop if user doesn't react after x seconds

If your editor doesn't support pasting in the emojis directly, you can use a website such as this one to find the unicodes of the emojis instead. In this case, the forwards arrow was \u25c0 and the backwards arrow was \u25b6.

Other than that, you should be good to go! The message will delete itself after 60 seconds of inactivity in that message (i.e. nobody reacting with the arrows), but just change the number if you want a longer period before deletion.

Alternatively, you can add in a third emoji, such as a cross, which which delete the message on demand.


References:

Community
  • 1
  • 1
Diggy.
  • 6,744
  • 3
  • 19
  • 38
1

I wanted to highlight a slightly more modern approach with Discord.py UI components. The idea is you have a subclassed a BackButton, a ForwardButton, and a View to use the buttons in, and these can work alongside a list of embeds that you similarly use according to the accepted answer above. Since there is not a lot of examples online for this, I wanted to share it.

The trick is using your custom view to track whatever stats you want to track, in our case the current page, and the list of embeds. Then, the buttons can be aware of any stats that the view is aware of, and adjust them as well. It's actually super helpful and allows us to add features to UI that couldn't exist otherwise, such as dynamically adding and removing the buttons when you're at the first and last page.

A link to the GitHub repo file is here.

https://github.com/Bernbark/discordBot/blob/master/src/cogs/inventory.py

Bernbark
  • 41
  • 6
-2

If you're using client.command() instead of bot.command() replace the two variables bot to client.

Ruli
  • 2,592
  • 12
  • 30
  • 40
Cyll
  • 7