-
-
Notifications
You must be signed in to change notification settings - Fork 455
/
pagination.py
1039 lines (917 loc) · 44.8 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The MIT License (MIT)
Copyright (c) 2021-present Pycord Development
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from typing import Dict, List, Optional, Union
import discord
from discord.ext.commands import Context
__all__ = (
"PaginatorButton",
"Paginator",
"PageGroup",
"PaginatorMenu",
"Page",
)
class PaginatorButton(discord.ui.Button):
"""Creates a button used to navigate the paginator.
Parameters
----------
button_type: :class:`str`
The type of button being created.
Must be one of ``first``, ``prev``, ``next``, ``last``, or ``page_indicator``.
label: :class:`str`
The label shown on the button.
Defaults to a capitalized version of ``button_type`` (e.g. "Next", "Prev", etc.)
emoji: Union[:class:`str`, :class:`discord.Emoji`, :class:`discord.PartialEmoji`]
The emoji shown on the button in front of the label.
disabled: :class:`bool`
Whether to initially show the button as disabled.
loop_label: :class:`str`
The label shown on the button when ``loop_pages`` is set to ``True`` in the Paginator class.
Attributes
----------
paginator: :class:`Paginator`
The paginator class where this button is being used.
Assigned to the button when ``Paginator.add_button`` is called.
"""
def __init__(
self,
button_type: str,
label: str = None,
emoji: Union[str, discord.Emoji, discord.PartialEmoji] = None,
style: discord.ButtonStyle = discord.ButtonStyle.green,
disabled: bool = False,
custom_id: str = None,
row: int = 0,
loop_label: str = None,
):
super().__init__(
label=label if label or emoji else button_type.capitalize(),
emoji=emoji,
style=style,
disabled=disabled,
custom_id=custom_id,
row=row,
)
self.button_type = button_type
self.label = label if label or emoji else button_type.capitalize()
self.emoji: Union[str, discord.Emoji, discord.PartialEmoji] = emoji
self.style = style
self.disabled = disabled
self.loop_label = self.label if not loop_label else loop_label
self.paginator = None
async def callback(self, interaction: discord.Interaction):
"""|coro|
The coroutine that is called when the navigation button is clicked.
Parameters
-----------
interaction: :class:`discord.Interaction`
The interaction created by clicking the navigation button.
"""
if self.button_type == "first":
self.paginator.current_page = 0
elif self.button_type == "prev":
if self.paginator.loop_pages and self.paginator.current_page == 0:
self.paginator.current_page = self.paginator.page_count
else:
self.paginator.current_page -= 1
elif self.button_type == "next":
if self.paginator.loop_pages and self.paginator.current_page == self.paginator.page_count:
self.paginator.current_page = 0
else:
self.paginator.current_page += 1
elif self.button_type == "last":
self.paginator.current_page = self.paginator.page_count
await self.paginator.goto_page(page_number=self.paginator.current_page, interaction=interaction)
class Page:
"""Represents a page shown in the paginator.
Allows for directly referencing and modifying each page as a class instance.
Parameters
----------
content: :class:`str`
The content of the page. Corresponds to the :class:`discord.Message.content` attribute.
embeds: Optional[List[Union[List[:class:`discord.Embed`], :class:`discord.Embed`]]]
The embeds of the page. Corresponds to the :class:`discord.Message.embeds` attribute.
custom_view: Optional[:class:`discord.ui.View`]
The custom view shown when the page is visible. Overrides the `custom_view` attribute of the main paginator.
"""
def __init__(
self,
content: Optional[str] = None,
embeds: Optional[List[Union[List[discord.Embed], discord.Embed]]] = None,
custom_view: Optional[discord.ui.View] = None,
**kwargs,
):
if content is None and embeds is None:
raise discord.InvalidArgument("A page cannot have both content and embeds equal to None.")
self._content = content
self._embeds = embeds or []
self._custom_view = custom_view
async def callback(self, interaction: Optional[discord.Interaction] = None):
"""|coro|
The coroutine associated to a specific page. If `Paginator.page_action()` is used, this coroutine is called.
Parameters
----------
interaction: Optional[:class:`discord.Interaction`]
The interaction associated with the callback, if any.
"""
pass
@property
def content(self) -> Optional[str]:
"""Gets the content for the page."""
return self._content
@content.setter
def content(self, value: Optional[str]):
"""Sets the content for the page."""
self._content = value
@property
def embeds(self) -> Optional[List[Union[List[discord.Embed], discord.Embed]]]:
"""Gets the embeds for the page."""
return self._embeds
@embeds.setter
def embeds(self, value: Optional[List[Union[List[discord.Embed], discord.Embed]]]):
"""Sets the embeds for the page."""
self._embeds = value
@property
def custom_view(self) -> Optional[discord.ui.View]:
"""Gets the custom view assigned to the page."""
return self._custom_view
@custom_view.setter
def custom_view(self, value: Optional[discord.ui.View]):
"""Assigns a custom view to be shown when the page is displayed."""
self._custom_view = value
class PageGroup:
"""Creates a group of pages which the user can switch between.
Each group of pages can have its own options, custom buttons, custom views, etc.
.. note::
If multiple ``PageGroup`` objects have different options, they should all be set explicitly when creating each instance.
Parameters
----------
pages: Union[List[:class:`str`], List[:class:`Page`], List[Union[List[:class:`discord.Embed`], :class:`discord.Embed`]]]
The list of :class:`Page` objects, strings, embeds, or list of embeds to include in the page group.
label: :class:`str`
The label shown on the corresponding PaginatorMenu dropdown option.
Also used as the SelectOption value.
description: :class:`str`
The description shown on the corresponding PaginatorMenu dropdown option.
emoji: Union[:class:`str`, :class:`discord.Emoji`, :class:`discord.PartialEmoji`]
The emoji shown on the corresponding PaginatorMenu dropdown option.
show_disabled: :class:`bool`
Whether to show disabled buttons.
show_indicator: :class:`bool`
Whether to show the page indicator when using the default buttons.
author_check: :class:`bool`
Whether only the original user of the command can change pages.
disable_on_timeout: :class:`bool`
Whether the buttons get disabled when the paginator view times out.
use_default_buttons: :class:`bool`
Whether to use the default buttons (i.e. ``first``, ``prev``, ``page_indicator``, ``next``, ``last``)
default_button_row: :class:`int`
The row where the default paginator buttons are displayed. Has no effect if custom buttons are used.
loop_pages: :class:`bool`
Whether to loop the pages when clicking prev/next while at the first/last page in the list.
custom_view: Optional[:class:`discord.ui.View`]
A custom view whose items are appended below the pagination buttons.
timeout: Optional[:class:`float`]
Timeout in seconds from last interaction with the paginator before no longer accepting input.
custom_buttons: Optional[List[:class:`PaginatorButton`]]
A list of PaginatorButtons to initialize the Paginator with.
If ``use_default_buttons`` is ``True``, this parameter is ignored.
trigger_on_display: :class:`bool`
Whether to automatically trigger the callback associated with a `Page` whenever it is displayed.
Has no effect if no callback exists for a `Page`.
"""
def __init__(
self,
pages: Union[List[str], List[Page], List[Union[List[discord.Embed], discord.Embed]]],
label: str,
description: str,
emoji: Union[str, discord.Emoji, discord.PartialEmoji] = None,
show_disabled: Optional[bool] = None,
show_indicator: Optional[bool] = None,
author_check: Optional[bool] = None,
disable_on_timeout: Optional[bool] = None,
use_default_buttons: Optional[bool] = None,
default_button_row: int = 0,
loop_pages: Optional[bool] = None,
custom_view: Optional[discord.ui.View] = None,
timeout: Optional[float] = None,
custom_buttons: Optional[List[PaginatorButton]] = None,
trigger_on_display: Optional[bool] = None,
):
self.label = label
self.description = description
self.emoji: Union[str, discord.Emoji, discord.PartialEmoji] = emoji
self.pages: Union[List[str], List[Union[List[discord.Embed], discord.Embed]]] = pages
self.show_disabled = show_disabled
self.show_indicator = show_indicator
self.author_check = author_check
self.disable_on_timeout = disable_on_timeout
self.use_default_buttons = use_default_buttons
self.default_button_row = default_button_row
self.loop_pages = loop_pages
self.custom_view: discord.ui.View = custom_view
self.timeout: float = timeout
self.custom_buttons: List = custom_buttons
self.trigger_on_display = trigger_on_display
class Paginator(discord.ui.View):
"""Creates a paginator which can be sent as a message and uses buttons for navigation.
Parameters
----------
pages: Union[List[:class:`PageGroup`], List[:class:`Page`], List[:class:`str`], List[Union[List[:class:`discord.Embed`], :class:`discord.Embed`]]]
The list of :class:`PageGroup` objects, :class:`Page` objects, strings, embeds, or list of embeds to paginate.
If a list of :class:`PageGroup` objects is provided and `show_menu` is ``False``, only the first page group will be displayed.
show_disabled: :class:`bool`
Whether to show disabled buttons.
show_indicator: :class:`bool`
Whether to show the page indicator when using the default buttons.
show_menu: :class:`bool`
Whether to show a select menu that allows the user to switch between groups of pages.
menu_placeholder: :class:`str`
The placeholder text to show in the page group menu when no page group has been selected yet.
Defaults to "Select Page Group" if not provided.
author_check: :class:`bool`
Whether only the original user of the command can change pages.
disable_on_timeout: :class:`bool`
Whether the buttons get disabled when the paginator view times out.
use_default_buttons: :class:`bool`
Whether to use the default buttons (i.e. ``first``, ``prev``, ``page_indicator``, ``next``, ``last``)
default_button_row: :class:`int`
The row where the default paginator buttons are displayed. Has no effect if custom buttons are used.
loop_pages: :class:`bool`
Whether to loop the pages when clicking prev/next while at the first/last page in the list.
custom_view: Optional[:class:`discord.ui.View`]
A custom view whose items are appended below the pagination components.
If the currently displayed page has a `custom_view` assigned, it will replace these view components when that page is displayed.
timeout: Optional[:class:`float`]
Timeout in seconds from last interaction with the paginator before no longer accepting input.
custom_buttons: Optional[List[:class:`PaginatorButton`]]
A list of PaginatorButtons to initialize the Paginator with.
If ``use_default_buttons`` is ``True``, this parameter is ignored.
trigger_on_display: :class:`bool`
Whether to automatically trigger the callback associated with a `Page` whenever it is displayed.
Has no effect if no callback exists for a `Page`.
Attributes
----------
menu: Optional[List[:class:`PaginatorMenu`]]
The page group select menu associated with this paginator.
page_groups: Optional[List[:class:`PageGroup`]]
List of :class:`PageGroup` objects the user can switch between.
current_page: :class:`int`
A zero-indexed value showing the current page number.
page_count: :class:`int`
A zero-indexed value showing the total number of pages.
buttons: Dict[:class:`str`, Dict[:class:`str`, Union[:class:`~PaginatorButton`, :class:`bool`]]]
A dictionary containing the :class:`~PaginatorButton` objects included in this paginator.
user: Optional[Union[:class:`~discord.User`, :class:`~discord.Member`]]
The user or member that invoked the paginator.
message: Union[:class:`~discord.Message`, :class:`~discord.WebhookMessage`]
The message the paginator is attached to.
"""
def __init__(
self,
pages: Union[List[PageGroup], List[Page], List[str], List[Union[List[discord.Embed], discord.Embed]]],
show_disabled: bool = True,
show_indicator=True,
show_menu=False,
menu_placeholder: str = "Select Page Group",
author_check=True,
disable_on_timeout=True,
use_default_buttons=True,
default_button_row: int = 0,
loop_pages=False,
custom_view: Optional[discord.ui.View] = None,
timeout: Optional[float] = 180.0,
custom_buttons: Optional[List[PaginatorButton]] = None,
trigger_on_display: Optional[bool] = None,
) -> None:
super().__init__(timeout=timeout)
self.timeout: float = timeout
self.pages: Union[
List[PageGroup], List[str], List[Page], List[Union[List[discord.Embed], discord.Embed]]
] = pages
self.current_page = 0
self.menu: Optional[PaginatorMenu] = None
self.show_menu = show_menu
self.menu_placeholder = menu_placeholder
self.page_groups: Optional[List[PageGroup]] = None
if all(isinstance(pg, PageGroup) for pg in pages):
self.page_groups = self.pages if show_menu else None
self.pages: Union[
List[str], List[Page], List[Union[List[discord.Embed], discord.Embed]]
] = self.page_groups[0].pages
self.page_count = max(len(self.pages) - 1, 0)
self.buttons = {}
self.custom_buttons: List = custom_buttons
self.show_disabled = show_disabled
self.show_indicator = show_indicator
self.disable_on_timeout = disable_on_timeout
self.use_default_buttons = use_default_buttons
self.default_button_row = default_button_row
self.loop_pages = loop_pages
self.custom_view: discord.ui.View = custom_view
self.trigger_on_display = trigger_on_display
self.message: Union[discord.Message, discord.WebhookMessage, None] = None
if self.custom_buttons and not self.use_default_buttons:
for button in custom_buttons:
self.add_button(button)
elif not self.custom_buttons and self.use_default_buttons:
self.add_default_buttons()
if self.show_menu:
self.add_menu()
self.usercheck = author_check
self.user = None
async def update(
self,
pages: Optional[Union[List[str], List[Page], List[Union[List[discord.Embed], discord.Embed]]]] = None,
show_disabled: Optional[bool] = None,
show_indicator: Optional[bool] = None,
author_check: Optional[bool] = None,
menu_placeholder: Optional[str] = None,
disable_on_timeout: Optional[bool] = None,
use_default_buttons: Optional[bool] = None,
default_button_row: Optional[int] = None,
loop_pages: Optional[bool] = None,
custom_view: Optional[discord.ui.View] = None,
timeout: Optional[float] = None,
custom_buttons: Optional[List[PaginatorButton]] = None,
trigger_on_display: Optional[bool] = None,
):
"""Updates the existing :class:`Paginator` instance with the provided options.
Parameters
----------
pages: Optional[Union[List[:class:`PageGroup`], List[:class:`Page`], List[:class:`str`], List[Union[List[:class:`discord.Embed`], :class:`discord.Embed`]]]]
The list of :class:`PageGroup` objects, :class:`Page` objects, strings, embeds, or list of embeds to paginate.
show_disabled: :class:`bool`
Whether to show disabled buttons.
show_indicator: :class:`bool`
Whether to show the page indicator when using the default buttons.
author_check: :class:`bool`
Whether only the original user of the command can change pages.
menu_placeholder: :class:`str`
The placeholder text to show in the page group menu when no page group has been selected yet.
Defaults to "Select Page Group" if not provided.
disable_on_timeout: :class:`bool`
Whether the buttons get disabled when the paginator view times out.
use_default_buttons: :class:`bool`
Whether to use the default buttons (i.e. ``first``, ``prev``, ``page_indicator``, ``next``, ``last``)
default_button_row: Optional[:class:`int`]
The row where the default paginator buttons are displayed. Has no effect if custom buttons are used.
loop_pages: :class:`bool`
Whether to loop the pages when clicking prev/next while at the first/last page in the list.
custom_view: Optional[:class:`discord.ui.View`]
A custom view whose items are appended below the pagination components.
timeout: Optional[:class:`float`]
Timeout in seconds from last interaction with the paginator before no longer accepting input.
custom_buttons: Optional[List[:class:`PaginatorButton`]]
A list of PaginatorButtons to initialize the Paginator with.
If ``use_default_buttons`` is ``True``, this parameter is ignored.
trigger_on_display: :class:`bool`
Whether to automatically trigger the callback associated with a `Page` whenever it is displayed.
Has no effect if no callback exists for a `Page`.
"""
# Update pages and reset current_page to 0 (default)
self.pages: Union[List[PageGroup], List[str], List[Page], List[Union[List[discord.Embed], discord.Embed]]] = (
pages if pages is not None else self.pages
)
self.page_count = max(len(self.pages) - 1, 0)
self.current_page = 0
# Apply config changes, if specified
self.show_disabled = show_disabled if show_disabled is not None else self.show_disabled
self.show_indicator = show_indicator if show_indicator is not None else self.show_indicator
self.usercheck = author_check if author_check is not None else self.usercheck
self.menu_placeholder = menu_placeholder if menu_placeholder is not None else self.menu_placeholder
self.disable_on_timeout = disable_on_timeout if disable_on_timeout is not None else self.disable_on_timeout
self.use_default_buttons = use_default_buttons if use_default_buttons is not None else self.use_default_buttons
self.default_button_row = default_button_row if default_button_row is not None else self.default_button_row
self.loop_pages = loop_pages if loop_pages is not None else self.loop_pages
self.custom_view: discord.ui.View = None if custom_view is None else custom_view
self.timeout: float = timeout if timeout is not None else self.timeout
self.trigger_on_display = trigger_on_display if trigger_on_display is not None else self.trigger_on_display
if custom_buttons and not self.use_default_buttons:
self.buttons = {}
for button in custom_buttons:
self.add_button(button)
else:
self.buttons = {}
self.add_default_buttons()
await self.goto_page(self.current_page)
async def on_timeout(self) -> None:
"""Disables all buttons when the view times out."""
if self.disable_on_timeout:
for item in self.children:
item.disabled = True
await self.message.edit(view=self)
async def disable(
self,
include_custom: bool = False,
page: Optional[Union[str, Page, Union[List[discord.Embed], discord.Embed]]] = None,
) -> None:
"""Stops the paginator, disabling all of its components.
Parameters
----------
include_custom: :class:`bool`
Whether to disable components added via custom views.
page: Optional[Union[:class:`str`, Union[List[:class:`discord.Embed`], :class:`discord.Embed`]]]
The page content to show after disabling the paginator.
"""
page = self.get_page_content(page)
for item in self.children:
if include_custom or not self.custom_view or item not in self.custom_view.children:
item.disabled = True
if page:
await self.message.edit(
content=page.content,
embeds=page.embeds,
view=self,
)
else:
await self.message.edit(view=self)
async def cancel(
self,
include_custom: bool = False,
page: Optional[Union[str, Page, Union[List[discord.Embed], discord.Embed]]] = None,
) -> None:
"""Cancels the paginator, removing all of its components from the message.
Parameters
----------
include_custom: :class:`bool`
Whether to remove components added via custom views.
page: Optional[Union[:class:`str`, Union[List[:class:`discord.Embed`], :class:`discord.Embed`]]]
The page content to show after canceling the paginator.
"""
items = self.children.copy()
page = self.get_page_content(page)
for item in items:
if include_custom or not self.custom_view or item not in self.custom_view.children:
self.remove_item(item)
if page:
await self.message.edit(
content=page.content,
embeds=page.embeds,
view=self,
)
else:
await self.message.edit(view=self)
async def goto_page(self, page_number: int = 0, *, interaction: Optional[discord.Interaction] = None) -> None:
"""Updates the paginator message to show the specified page number.
Parameters
----------
page_number: :class:`int`
The page to display.
.. note::
Page numbers are zero-indexed when referenced internally, but appear as one-indexed when shown to the user.
interaction: Optional[:class:`discord.Interaction`]
The interaction to use when editing the message. If not provided, the message will be edited using the paginator's
stored :attr:`message` attribute instead.
Returns
-------
:class:`~discord.Message`
The message associated with the paginator.
"""
self.update_buttons()
self.current_page = page_number
if self.show_indicator:
self.buttons["page_indicator"]["object"].label = f"{self.current_page + 1}/{self.page_count + 1}"
page = self.pages[page_number]
page = self.get_page_content(page)
if page.custom_view:
self.update_custom_view(page.custom_view)
if interaction:
await interaction.response.edit_message(content=page.content, embeds=page.embeds, view=self)
else:
await self.message.edit(
content=page.content,
embeds=page.embeds,
view=self,
)
if self.trigger_on_display:
await self.page_action(interaction=interaction)
async def interaction_check(self, interaction: discord.Interaction) -> bool:
if self.usercheck:
return self.user == interaction.user
return True
def add_menu(self):
"""Adds the default :class:`PaginatorMenu` instance to the paginator."""
self.menu = PaginatorMenu(self.page_groups, placeholder=self.menu_placeholder, custom_id="pages_group_menu")
self.menu.paginator = self
self.add_item(self.menu)
def add_default_buttons(self):
"""Adds the full list of default buttons that can be used with the paginator.
Includes ``first``, ``prev``, ``page_indicator``, ``next``, and ``last``."""
default_buttons = [
PaginatorButton(
"first",
label="<<",
style=discord.ButtonStyle.blurple,
row=self.default_button_row,
custom_id="pages_first_button",
),
PaginatorButton(
"prev",
label="<",
style=discord.ButtonStyle.red,
loop_label="↪",
row=self.default_button_row,
custom_id="pages_prev_button",
),
PaginatorButton(
"page_indicator",
style=discord.ButtonStyle.gray,
disabled=True,
row=self.default_button_row,
custom_id="pages_indicator_button",
),
PaginatorButton(
"next",
label=">",
style=discord.ButtonStyle.green,
loop_label="↩",
row=self.default_button_row,
custom_id="pages_next_button",
),
PaginatorButton(
"last",
label=">>",
style=discord.ButtonStyle.blurple,
row=self.default_button_row,
custom_id="pages_last_button",
),
]
for button in default_buttons:
self.add_button(button)
def add_button(self, button: PaginatorButton):
"""Adds a :class:`PaginatorButton` to the paginator."""
self.buttons[button.button_type] = {
"object": discord.ui.Button(
style=button.style,
label=button.label
if button.label or button.emoji
else button.button_type.capitalize()
if button.button_type != "page_indicator"
else f"{self.current_page + 1}/{self.page_count + 1}",
disabled=button.disabled,
custom_id=button.custom_id,
emoji=button.emoji,
row=button.row,
),
"label": button.label,
"loop_label": button.loop_label,
"hidden": button.disabled if button.button_type != "page_indicator" else not self.show_indicator,
}
self.buttons[button.button_type]["object"].callback = button.callback
button.paginator = self
def remove_button(self, button_type: str):
"""Removes a :class:`PaginatorButton` from the paginator."""
if button_type not in self.buttons.keys():
raise ValueError(f"no button_type {button_type} was found in this paginator.")
self.buttons.pop(button_type)
def update_buttons(self) -> Dict:
"""Updates the display state of the buttons (disabled/hidden)
Returns
-------
Dict[:class:`str`, Dict[:class:`str`, Union[:class:`~PaginatorButton`, :class:`bool`]]]
The dictionary of buttons that were updated.
"""
for key, button in self.buttons.items():
if key == "first":
if self.current_page <= 1:
button["hidden"] = True
elif self.current_page >= 1:
button["hidden"] = False
elif key == "last":
if self.current_page >= self.page_count - 1:
button["hidden"] = True
if self.current_page < self.page_count - 1:
button["hidden"] = False
elif key == "next":
if self.current_page == self.page_count:
if not self.loop_pages:
button["hidden"] = True
button["object"].label = button["label"]
else:
button["object"].label = button["loop_label"]
elif self.current_page < self.page_count:
button["hidden"] = False
button["object"].label = button["label"]
elif key == "prev":
if self.current_page <= 0:
if not self.loop_pages:
button["hidden"] = True
button["object"].label = button["label"]
else:
button["object"].label = button["loop_label"]
elif self.current_page >= 0:
button["hidden"] = False
button["object"].label = button["label"]
self.clear_items()
if self.show_indicator:
self.buttons["page_indicator"]["object"].label = f"{self.current_page + 1}/{self.page_count + 1}"
for key, button in self.buttons.items():
if key != "page_indicator":
if button["hidden"]:
button["object"].disabled = True
if self.show_disabled:
self.add_item(button["object"])
else:
button["object"].disabled = False
self.add_item(button["object"])
elif self.show_indicator:
self.add_item(button["object"])
if self.show_menu:
self.add_menu()
# We're done adding standard buttons and menus, so we can now add any specified custom view items below them
# The bot developer should handle row assignments for their view before passing it to Paginator
if self.custom_view:
self.update_custom_view(self.custom_view)
return self.buttons
def update_custom_view(self, custom_view: discord.ui.View):
"""Updates the custom view shown on the paginator."""
if isinstance(self.custom_view, discord.ui.View):
for item in self.custom_view.children:
self.remove_item(item)
for item in custom_view.children:
self.add_item(item)
@staticmethod
def get_page_content(page: Union[Page, str, discord.Embed, List[discord.Embed]]) -> Page:
"""Converts a page into a :class:`Page` object based on its content."""
if isinstance(page, Page):
return page
elif isinstance(page, str):
return Page(content=page, embeds=[])
elif isinstance(page, discord.Embed):
return Page(content=None, embeds=[page])
elif isinstance(page, List):
if all(isinstance(x, discord.Embed) for x in page):
return Page(content=None, embeds=page)
else:
raise TypeError("All list items must be embeds.")
async def page_action(self, interaction: Optional[discord.Interaction] = None) -> None:
"""Triggers the callback associated with the current page, if any.
Parameters
----------
interaction: Optional[:class:`discord.Interaction`]
The interaction that was used to trigger the page action.
"""
if self.get_page_content(self.pages[self.current_page]).callback:
await self.get_page_content(self.pages[self.current_page]).callback(interaction=interaction)
async def send(
self,
ctx: Context,
target: Optional[discord.abc.Messageable] = None,
target_message: Optional[str] = None,
reference: Optional[Union[discord.Message, discord.MessageReference, discord.PartialMessage]] = None,
allowed_mentions: Optional[discord.AllowedMentions] = None,
mention_author: Optional[bool] = None,
delete_after: Optional[float] = None,
) -> discord.Message:
"""Sends a message with the paginated items.
Parameters
------------
ctx: Union[:class:`~discord.ext.commands.Context`]
A command's invocation context.
target: Optional[:class:`~discord.abc.Messageable`]
A target where the paginated message should be sent, if different from the original :class:`Context`
target_message: Optional[:class:`str`]
An optional message shown when the paginator message is sent elsewhere.
reference: Optional[Union[:class:`discord.Message`, :class:`discord.MessageReference`, :class:`discord.PartialMessage`]]
A reference to the :class:`~discord.Message` to which you are replying with the paginator. This can be created using
:meth:`~discord.Message.to_reference` or passed directly as a :class:`~discord.Message`. You can control
whether this mentions the author of the referenced message using the :attr:`~discord.AllowedMentions.replied_user`
attribute of ``allowed_mentions`` or by setting ``mention_author``.
allowed_mentions: Optional[:class:`~discord.AllowedMentions`]
Controls the mentions being processed in this message. If this is
passed, then the object is merged with :attr:`~discord.Client.allowed_mentions`.
The merging behaviour only overrides attributes that have been explicitly passed
to the object, otherwise it uses the attributes set in :attr:`~discord.Client.allowed_mentions`.
If no object is passed at all then the defaults given by :attr:`~discord.Client.allowed_mentions`
are used instead.
mention_author: Optional[:class:`bool`]
If set, overrides the :attr:`~discord.AllowedMentions.replied_user` attribute of ``allowed_mentions``.
delete_after: Optional[:class:`float`]
If set, deletes the paginator after the specified time.
Returns
--------
:class:`~discord.Message`
The message that was sent with the paginator.
"""
if not isinstance(ctx, Context):
raise TypeError(f"expected Context not {ctx.__class__!r}")
if target is not None and not isinstance(target, discord.abc.Messageable):
raise TypeError(f"expected abc.Messageable not {target.__class__!r}")
if reference is not None and not isinstance(
reference, (discord.Message, discord.MessageReference, discord.PartialMessage)
):
raise TypeError(f"expected Message, MessageReference, or PartialMessage not {reference.__class__!r}")
if allowed_mentions is not None and not isinstance(allowed_mentions, discord.AllowedMentions):
raise TypeError(f"expected AllowedMentions not {allowed_mentions.__class__!r}")
if mention_author is not None and not isinstance(mention_author, bool):
raise TypeError(f"expected bool not {mention_author.__class__!r}")
self.update_buttons()
page = self.pages[self.current_page]
page_content = self.get_page_content(page)
if page_content.custom_view:
self.update_custom_view(page_content.custom_view)
self.user = ctx.author
if target:
if target_message:
await ctx.send(
target_message,
reference=reference,
allowed_mentions=allowed_mentions,
mention_author=mention_author,
)
ctx = target
self.message = await ctx.send(
content=page_content.content,
embeds=page_content.embeds,
view=self,
reference=reference,
allowed_mentions=allowed_mentions,
mention_author=mention_author,
delete_after=delete_after,
)
return self.message
async def edit(
self,
message: discord.Message,
suppress: Optional[bool] = None,
allowed_mentions: Optional[discord.AllowedMentions] = None,
delete_after: Optional[float] = None,
) -> Optional[discord.Message]:
"""Edits an existing message to replace it with the paginator contents.
.. note::
If invoked from an interaction, you will still need to respond to the interaction.
Parameters
-----------
message: :class:`discord.Message`
The message to edit with the paginator.
suppress: :class:`bool`
Whether to suppress embeds for the message. This removes
all the embeds if set to ``True``. If set to ``False``
this brings the embeds back if they were suppressed.
Using this parameter requires :attr:`~.Permissions.manage_messages`.
allowed_mentions: Optional[:class:`~discord.AllowedMentions`]
Controls the mentions being processed in this message. If this is
passed, then the object is merged with :attr:`~discord.Client.allowed_mentions`.
The merging behaviour only overrides attributes that have been explicitly passed
to the object, otherwise it uses the attributes set in :attr:`~discord.Client.allowed_mentions`.
If no object is passed at all then the defaults given by :attr:`~discord.Client.allowed_mentions`
are used instead.
delete_after: Optional[:class:`float`]
If set, deletes the paginator after the specified time.
Returns
--------
Optional[:class:`discord.Message`]
The message that was edited. Returns ``None`` if the operation failed.
"""
if not isinstance(message, discord.Message):
raise TypeError(f"expected Message not {message.__class__!r}")
self.update_buttons()
page: Union[Page, str, discord.Embed, List[discord.Embed]] = self.pages[self.current_page]
page_content: Page = self.get_page_content(page)
if page_content.custom_view:
self.update_custom_view(page_content.custom_view)
self.user = message.author
try:
self.message = await message.edit(
content=page_content.content,
embeds=page_content.embeds,
view=self,
suppress=suppress,
allowed_mentions=allowed_mentions,
delete_after=delete_after,
)
except (discord.NotFound, discord.Forbidden):
pass
return self.message
async def respond(
self,
interaction: discord.Interaction,
ephemeral: bool = False,
target: Optional[discord.abc.Messageable] = None,
target_message: str = "Paginator sent!",
) -> Union[discord.Message, discord.WebhookMessage]:
"""Sends an interaction response or followup with the paginated items.
Parameters
------------
interaction: :class:`discord.Interaction`
The interaction which invoked the paginator.
ephemeral: :class:`bool`
Whether the paginator message and its components are ephemeral.
If ``target`` is specified, the ephemeral message content will be ``target_message`` instead.
.. warning::
If your paginator is ephemeral, it cannot have a timeout longer than 15 minutes (and cannot be persistent).
target: Optional[:class:`~discord.abc.Messageable`]
A target where the paginated message should be sent, if different from the original :class:`discord.Interaction`
target_message: :class:`str`
The content of the interaction response shown when the paginator message is sent elsewhere.
Returns
--------
Union[:class:`~discord.Message`, :class:`~discord.WebhookMessage`]
The :class:`~discord.Message` or :class:`~discord.WebhookMessage` that was sent with the paginator.
"""
if not isinstance(interaction, discord.Interaction):
raise TypeError(f"expected Interaction not {interaction.__class__!r}")
if target is not None and not isinstance(target, discord.abc.Messageable):
raise TypeError(f"expected abc.Messageable not {target.__class__!r}")
if ephemeral and (self.timeout >= 900 or self.timeout is None):
raise ValueError(
"paginator responses cannot be ephemeral if the paginator timeout is 15 minutes or greater"
)
self.update_buttons()
page: Union[Page, str, discord.Embed, List[discord.Embed]] = self.pages[self.current_page]
page_content: Page = self.get_page_content(page)
if page_content.custom_view:
self.update_custom_view(page_content.custom_view)
self.user = interaction.user
if target:
await interaction.response.send_message(target_message, ephemeral=ephemeral)
self.message = await target.send(
content=page_content.content,
embeds=page_content.embeds,
view=self,
)
else:
if interaction.response.is_done():
msg = await interaction.followup.send(
content=page_content.content,
embeds=page_content.embeds,
view=self,
ephemeral=ephemeral,
)
# convert from WebhookMessage to Message reference to bypass 15min webhook token timeout (non-ephemeral messages only)
if not ephemeral:
msg = await msg.channel.fetch_message(msg.id)
else:
msg = await interaction.response.send_message(
content=page_content.content,
embeds=page_content.embeds,
view=self,
ephemeral=ephemeral,
)
if isinstance(msg, (discord.Message, discord.WebhookMessage)):
self.message = msg
elif isinstance(msg, discord.Interaction):
self.message = await msg.original_message()
return self.message
class PaginatorMenu(discord.ui.Select):
"""Creates a select menu used to switch between page groups, which can each have their own set of buttons.
Parameters
----------
placeholder: :class:`str`
The placeholder text that is shown if nothing is selected.
Attributes
----------