-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpagination.py
433 lines (332 loc) · 16.7 KB
/
pagination.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from collections.abc import Iterable
from discord import Embed, Member, Reaction
from discord.abc import User
from discord.ext.commands import Context, Paginator
from pydis_core.utils import logging
from bot.constants import Emojis
FIRST_EMOJI = "\u23EE" # [:track_previous:]
LEFT_EMOJI = "\u2B05" # [:arrow_left:]
RIGHT_EMOJI = "\u27A1" # [:arrow_right:]
LAST_EMOJI = "\u23ED" # [:track_next:]
DELETE_EMOJI = Emojis.trashcan # [:trashcan:]
PAGINATION_EMOJI = (FIRST_EMOJI, LEFT_EMOJI, RIGHT_EMOJI, LAST_EMOJI, DELETE_EMOJI)
log = logging.get_logger(__name__)
class EmptyPaginatorEmbedError(Exception):
"""Base Exception class for an empty paginator embed."""
class LinePaginator(Paginator):
"""A class that aids in paginating code blocks for Discord messages."""
def __init__(
self,
prefix: str = "```",
suffix: str = "```",
max_size: int = 2000,
max_lines: int | None = None,
linesep: str = "\n"
):
"""
Overrides the Paginator.__init__ from inside discord.ext.commands.
`prefix` and `suffix` will be prepended and appended respectively to every page.
`max_size` and `max_lines` denote the maximum amount of codepoints and lines
allowed per page.
"""
super().__init__(
prefix,
suffix,
max_size - len(suffix),
linesep
)
self.max_lines = max_lines
self._current_page = [prefix]
self._linecount = 0
self._count = len(prefix) + 1 # prefix + newline
self._pages = []
def add_line(self, line: str = "", *, empty: bool = False) -> None:
"""
Adds a line to the current page.
If the line exceeds the `max_size` then a RuntimeError is raised.
Overrides the Paginator.add_line from inside discord.ext.commands in order to allow
configuration of the maximum number of lines per page.
If `empty` is True, an empty line will be placed after the a given `line`.
"""
if len(line) > self.max_size - len(self.prefix) - 2:
raise RuntimeError("Line exceeds maximum page size %s" % (self.max_size - len(self.prefix) - 2))
if self.max_lines is not None:
if self._linecount >= self.max_lines:
self._linecount = 0
self.close_page()
self._linecount += 1
if self._count + len(line) + 1 > self.max_size:
self.close_page()
self._count += len(line) + 1
self._current_page.append(line)
if empty:
self._current_page.append("")
self._count += 1
@classmethod
async def paginate(cls, lines: Iterable[str], ctx: Context, embed: Embed,
prefix: str = "", suffix: str = "", max_lines: int | None = None,
max_size: int = 500, empty: bool = True, restrict_to_user: User = None,
timeout: int = 300, footer_text: str | None = None, url: str | None = None,
exception_on_empty_embed: bool = False) -> None:
"""
Use a paginator and set of reactions to provide pagination over a set of lines.
The reactions are used to switch page, or to finish with pagination.
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.
Pagination will also be removed automatically if no reaction is added for `timeout` seconds,
defaulting to five minutes (300 seconds).
If `empty` is True, an empty line will be placed between each given line.
>>> embed = Embed()
>>> embed.set_author(name="Some Operation", url=url, icon_url=icon)
>>> await LinePaginator.paginate(
... (line for line in lines),
... ctx, embed
... )
"""
def event_check(reaction_: Reaction, user_: Member) -> bool:
"""Make sure that this reaction is what we want to operate on."""
no_restrictions = (
# Pagination is not restricted
not restrict_to_user
# The reaction was by a whitelisted user
or user_.id == restrict_to_user.id
)
return (
# Conditions for a successful pagination:
all((
# Reaction is on this message
reaction_.message.id == message.id,
# Reaction is one of the pagination emotes
str(reaction_.emoji) in PAGINATION_EMOJI, # Note: DELETE_EMOJI is a string and not unicode
# Reaction was not made by the Bot
user_.id != ctx.bot.user.id,
# There were no restrictions
no_restrictions
))
)
paginator = cls(prefix=prefix, suffix=suffix, max_size=max_size, max_lines=max_lines)
current_page = 0
if not lines:
if exception_on_empty_embed:
log.exception("Pagination asked for empty lines iterable")
raise EmptyPaginatorEmbedError("No lines to paginate")
log.debug("No lines to add to paginator, adding '(nothing to display)' message")
lines.append("(nothing to display)")
for line in lines:
try:
paginator.add_line(line, empty=empty)
except Exception:
log.exception(f"Failed to add line to paginator: '{line}'")
raise # Should propagate
else:
log.trace(f"Added line to paginator: '{line}'")
log.debug(f"Paginator created with {len(paginator.pages)} pages")
embed.description = paginator.pages[current_page]
if len(paginator.pages) <= 1:
if footer_text:
embed.set_footer(text=footer_text)
log.trace(f"Setting embed footer to '{footer_text}'")
if url:
embed.url = url
log.trace(f"Setting embed url to '{url}'")
log.debug("There's less than two pages, so we won't paginate - sending single page on its own")
await ctx.send(embed=embed)
return None
if footer_text:
embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})")
else:
embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}")
log.trace(f"Setting embed footer to '{embed.footer.text}'")
if url:
embed.url = url
log.trace(f"Setting embed url to '{url}'")
log.debug("Sending first page to channel...")
message = await ctx.send(embed=embed)
log.debug("Adding emoji reactions to message...")
for emoji in PAGINATION_EMOJI:
# Add all the applicable emoji to the message
log.trace(f"Adding reaction: {emoji!r}")
await message.add_reaction(emoji)
while True:
try:
reaction, user = await ctx.bot.wait_for("reaction_add", timeout=timeout, check=event_check)
log.trace(f"Got reaction: {reaction}")
except TimeoutError:
log.debug("Timed out waiting for a reaction")
break # We're done, no reactions for the last 5 minutes
if str(reaction.emoji) == DELETE_EMOJI: # Note: DELETE_EMOJI is a string and not unicode
log.debug("Got delete reaction")
return await message.delete()
if reaction.emoji == FIRST_EMOJI:
await message.remove_reaction(reaction.emoji, user)
current_page = 0
log.debug(f"Got first page reaction - changing to page 1/{len(paginator.pages)}")
embed.description = paginator.pages[current_page]
if footer_text:
embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})")
else:
embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}")
await message.edit(embed=embed)
if reaction.emoji == LAST_EMOJI:
await message.remove_reaction(reaction.emoji, user)
current_page = len(paginator.pages) - 1
log.debug(f"Got last page reaction - changing to page {current_page + 1}/{len(paginator.pages)}")
embed.description = paginator.pages[current_page]
if footer_text:
embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})")
else:
embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}")
await message.edit(embed=embed)
if reaction.emoji == LEFT_EMOJI:
await message.remove_reaction(reaction.emoji, user)
if current_page <= 0:
log.debug("Got previous page reaction, but we're on the first page - ignoring")
continue
current_page -= 1
log.debug(f"Got previous page reaction - changing to page {current_page + 1}/{len(paginator.pages)}")
embed.description = paginator.pages[current_page]
if footer_text:
embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})")
else:
embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}")
await message.edit(embed=embed)
if reaction.emoji == RIGHT_EMOJI:
await message.remove_reaction(reaction.emoji, user)
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
log.debug(f"Got next page reaction - changing to page {current_page + 1}/{len(paginator.pages)}")
embed.description = paginator.pages[current_page]
if footer_text:
embed.set_footer(text=f"{footer_text} (Page {current_page + 1}/{len(paginator.pages)})")
else:
embed.set_footer(text=f"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
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: int = 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
str(reaction_.emoji) in PAGINATION_EMOJI, # Note: DELETE_EMOJI is a string and not unicode
# 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:
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) == DELETE_EMOJI: # 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 == FIRST_EMOJI:
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 == LAST_EMOJI:
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 == LEFT_EMOJI:
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 == RIGHT_EMOJI:
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