1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
from collections.abc import Sequence
from discord import Embed, Interaction, Member, Message, Reaction
from discord.abc import User
from discord.ext.commands import Context, Paginator
from pydis_core.utils.logging import get_logger
from pydis_core.utils.pagination import EmptyPaginatorEmbedError, LinePaginator as _LinePaginator, PaginationEmojis
from bot.constants import Emojis
PAGINATION_EMOJI = PaginationEmojis(delete=Emojis.trashcan)
log = get_logger(__name__)
class LinePaginator(_LinePaginator):
"""A class that aids in paginating code blocks for Discord messages."""
@classmethod
async def paginate(
cls,
lines: list[str],
ctx: Context | Interaction,
embed: Embed,
prefix: str = "",
suffix: str = "",
max_lines: int | None = None,
max_size: int = 500,
scale_to_size: int = 4000,
empty: bool = True,
restrict_to_user: User | None = None,
timeout: int = 300,
footer_text: str | None = None,
url: str | None = None,
exception_on_empty_embed: bool = False,
reply: bool = False,
allowed_roles: Sequence[int] | None = None,
) -> Message | None:
"""
Use a paginator and set of reactions to provide pagination over a set of lines.
Acts as a wrapper for the super class' `paginate` method to provide the pagination emojis by default.
Consult the super class's `paginate` method for detailed information.
"""
return await super().paginate(
pagination_emojis=PAGINATION_EMOJI,
lines=lines,
ctx=ctx,
embed=embed,
prefix=prefix,
suffix=suffix,
max_lines=max_lines,
max_size=max_size,
scale_to_size=scale_to_size,
empty=empty,
restrict_to_user=restrict_to_user,
timeout=timeout,
footer_text=footer_text,
url=url,
exception_on_empty_embed=exception_on_empty_embed,
reply=reply,
allowed_roles=allowed_roles,
)
class ImagePaginator(Paginator):
"""
Helper class that paginates images for embeds in messages.
Close resemblance to LinePaginator, except focuses on images over text.
Refer to ImagePaginator.paginate for documentation on how to use.
"""
def __init__(self, prefix: str = "", suffix: str = ""):
super().__init__(prefix, suffix)
self._current_page = [prefix]
self.images = []
self._pages = []
def add_line(self, line: str = "", *, empty: bool = False) -> None:
"""
Adds a line to each page, usually just 1 line in this context.
If `empty` is True, an empty line will be placed after a given `line`.
"""
if line:
self._count = len(line)
else:
self._count = 0
self._current_page.append(line)
self.close_page()
def add_image(self, image: str | None = None) -> None:
"""Adds an image to a page given the url."""
self.images.append(image)
@classmethod
async def paginate(cls, pages: list[tuple[str, str]], ctx: Context, embed: Embed,
prefix: str = "", suffix: str = "", timeout: float = 300,
exception_on_empty_embed: bool = False) -> None:
"""
Use a paginator and set of reactions to provide pagination over a set of title/image pairs.
`pages` is a list of tuples of page title/image url pairs.
`prefix` and `suffix` will be prepended and appended respectively to the message.
When used, this will send a message using `ctx.send()` and apply a set of reactions to it.
These reactions may be used to change page, or to remove pagination from the message.
Note: Pagination will be removed automatically if no reaction is added for `timeout` seconds,
defaulting to five minutes (300 seconds).
>>> embed = Embed()
>>> embed.set_author(name="Some Operation", url=url, icon_url=icon)
>>> await ImagePaginator.paginate(pages, ctx, embed)
"""
def check_event(reaction_: Reaction, member: Member) -> bool:
"""Checks each reaction added, if it matches our conditions pass the wait_for."""
return all((
# Reaction is on the same message sent
reaction_.message.id == message.id,
# The reaction is part of the navigation menu
# Note: DELETE_EMOJI is a string and not unicode
str(reaction_.emoji) in PAGINATION_EMOJI.model_dump().values(),
# The reactor is not a bot
not member.bot
))
paginator = cls(prefix=prefix, suffix=suffix)
current_page = 0
if not pages:
if exception_on_empty_embed:
log.exception("Pagination asked for empty image list")
raise EmptyPaginatorEmbedError("No images to paginate")
log.debug("No images to add to paginator, adding '(no images to display)' message")
pages.append(("(no images to display)", ""))
for text, image_url in pages:
paginator.add_line(text)
paginator.add_image(image_url)
embed.description = paginator.pages[current_page]
image = paginator.images[current_page]
if image:
embed.set_image(url=image)
if len(paginator.pages) <= 1:
await ctx.send(embed=embed)
return None
embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}")
message = await ctx.send(embed=embed)
for emoji in PAGINATION_EMOJI.model_dump().values():
await message.add_reaction(emoji)
while True:
# Start waiting for reactions
try:
reaction, user = await ctx.bot.wait_for("reaction_add", timeout=timeout, check=check_event)
except TimeoutError:
log.debug("Timed out waiting for a reaction")
break # We're done, no reactions for the last 5 minutes
# Deletes the users reaction
await message.remove_reaction(reaction.emoji, user)
# Delete reaction press - [:trashcan:]
if str(reaction.emoji) == PAGINATION_EMOJI.delete: # Note: DELETE_EMOJI is a string and not unicode
log.debug("Got delete reaction")
return await message.delete()
# First reaction press - [:track_previous:]
if reaction.emoji == PAGINATION_EMOJI.first:
if current_page == 0:
log.debug("Got first page reaction, but we're on the first page - ignoring")
continue
current_page = 0
reaction_type = "first"
# Last reaction press - [:track_next:]
if reaction.emoji == PAGINATION_EMOJI.last:
if current_page >= len(paginator.pages) - 1:
log.debug("Got last page reaction, but we're on the last page - ignoring")
continue
current_page = len(paginator.pages) - 1
reaction_type = "last"
# Previous reaction press - [:arrow_left: ]
if reaction.emoji == PAGINATION_EMOJI.left:
if current_page <= 0:
log.debug("Got previous page reaction, but we're on the first page - ignoring")
continue
current_page -= 1
reaction_type = "previous"
# Next reaction press - [:arrow_right:]
if reaction.emoji == PAGINATION_EMOJI.right:
if current_page >= len(paginator.pages) - 1:
log.debug("Got next page reaction, but we're on the last page - ignoring")
continue
current_page += 1
reaction_type = "next"
# Magic happens here, after page and reaction_type is set
embed.description = paginator.pages[current_page]
image = paginator.images[current_page] or None
embed.set_image(url=image)
embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}")
log.debug(f"Got {reaction_type} page reaction - changing to page {current_page + 1}/{len(paginator.pages)}")
await message.edit(embed=embed)
log.debug("Ending pagination and clearing reactions...")
await message.clear_reactions()
return None
|