A paginator utility for discord.py
Project description
dpy-paginator
Table of contents
Built and tested on discord.py 2.1.1
A discord.py utility with no external dependencies that makes paginating embeds easier.
Some of it's features are -
- Easy to use.
- Supports both ephemeral and non-ephemeral responses.
- Buttons are enabled/disabled automatically depending on the current page number, number of embeds provided or at timeout.
The paginator consists of 4 buttons - ⏪, ◀️, 'Jump To' modal, ▶️, ⏩
- ⏪ - Shows the first embed. Disabled if there are less than 3 embeds or if already on the first embed.
- ◀️ - Shows the previous embed. Disabled if already on the first embed.
- 'Jump To' modal - Triggers a
discord.ui.Modal
that takes you to the page number you input. Disabled if there are less than 4 embeds. - ▶️ - Shows the next embed. Disabled if already on the last embed.
- ⏩ - Shows the last embed. Disabled if there are less than 3 embeds or if already on the last page.
Installation
pip install git+https://github.com/onceyt/dpy-paginator.git@v1.0.0
Usage
Basic usage:
import discord
from dpy_paginator import paginate
embed1 = discord.Embed(title = "This is embed#1")
embed2 = discord.Embed(title = "This is embed#2")
output = await paginate(embeds = [embed1, embed2])
# output.embed gives you the first embed of the pagination
# output.view gives you the discord.ui.View that controls the pagination
await Messagable.send(embed = output.embed, view = output.view)
# you want to send both in your Messageable.send
discord.ext.Commands example:
import discord
from discord.ext import commands
from dpy_paginator import paginate
bot = discord.Bot() # your discord.Bot object
@bot.command()
async def example(ctx: commands.Context):
embed1 = discord.Embed(title = "This is Embed#1")
embed2 = discord.Embed(title = "This is Embed#2")
output = await paginate(embeds = [embed1, embed2])
await ctx.send(embed = output.embed, view = output.view)
This command has the following output:
discord.app_commands usage: (ephemeral)
from discord import app_commands
from dpy_paginator import paginate
@app_commands.command(name='example')
async def example_command(interaction: discord.Interaction):
await interaction.response.defer(ephemeral = True, thinking = True)
embed1 = discord.Embed(title = "This is Embed#1")
embed2 = discord.Embed(title = "This is Embed#2")
output = await paginate(embeds = [embed1, embed2])
await interaction.followup.send(embed = output.embed, view = output.view)
This command has the following output:
Options and Parameters
Control who can interact: (author_ids: list[int]
param)
You can control which user(s) can interact with the view by passing a author_ids
list.
...
await paginate(embeds = [embed1, embed2], author_ids = [#ID1, #ID2])
When anyone except the specified user(s) try to interact, the paginator ignores that interaction:
Adding a timeout: (timeout: int
param)
By default, the view has a timeout of 90 seconds but this can be changed by passing a timeout
parameter.
...
await paginate(embeds = [embed1, embed2], timeout = 60)
The buttons get automatically disabled after timeout (except when no button is interacted with)[^1]. You can also use timeout = None
for no timeout.
Example of a timedout view:
In the scenario that no button is interacted with and the view gets timedout, the buttons will not be automatically disabled resulting in the need of an extra step. output.view.timedout
returns a boolean which we can use to check if the view has timedout.
import asyncio
...
timeout = 60
output = await paginate(embeds = [embed1, embed2], timeout = timeout)
message = await Messageable.send(embed = output.embed, view = output.view)
await asyncio.sleep(timeout + 0.5) # add 0.5 to the timeout to account for processing delays
if output.view.timedout: # check if the view is timedout
await message.edit(view = output.view) # manually edit the buttons if the output is timedout
# the view will automatically timeout incase this check returns False
Note that incase of ephemeral responses (or scenarios where the output will be deleted before the timeout), this extra step is probably not worth it.
[^1]: To explain this, the paginateButtons
view class receives the discord.Interaction
object only when one of the buttons is interacted with which is then used to edit the message with the disabled buttons upon timeout. Only running paginate()
and sending the output does not give the class access to the message sent, thus resulting in the need of an extra step to satisfy this possibility.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dpy_paginator-1.0.0.tar.gz
.
File metadata
- Download URL: dpy_paginator-1.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c12d528e6f400c18d0adbfa0eea53800b089b37472fd3c294b6e287e582762e |
|
MD5 | 1285307fcd18442424f75d6994fe2c5d |
|
BLAKE2b-256 | 86f3d874c3aeee592353caf89ee1bc5e67ac7064cd8fd46e781f89bb01bb7704 |
File details
Details for the file dpy_paginator-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: dpy_paginator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39714daafc560b09971bffdd165afc8c72f3e781fdcf6491d3ad3587ba1fdc0a |
|
MD5 | 21e5daa8b04a02cbd8e4fdcdded993fd |
|
BLAKE2b-256 | 0f349194c7dca2d8f5d130d7dd89c506325528071f39d1e26692f97482fc4b2b |