Skip to content

Commit dec0676

Browse files
authored
Merge pull request #589 from krittick/ext-menus
Rename ext.menus to ext.pages, fix docs and typing
2 parents b8c225a + 230dd1e commit dec0676

File tree

6 files changed

+121
-120
lines changed

6 files changed

+121
-120
lines changed

.github/ISSUE_TEMPLATE/feature_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ body:
1717
- The core library
1818
- discord.ext.commands
1919
- discord.ext.tasks
20-
- discord.ext.menus
20+
- discord.ext.pages
2121
- The documentation
2222
validations:
2323
required: true

discord/ext/menus/__init__.py renamed to discord/ext/pages/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
discord.ext.menus
2+
discord.ext.pages
33
~~~~~~~~~~~~~~~~~~~~~
44
An extension module to provide useful menu options.
55

discord/ext/menus/pagination.py renamed to discord/ext/pages/pagination.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ class PaginatorButton(discord.ui.Button):
3434
3535
Parameters
3636
----------
37-
3837
button_type: :class:`str`
3938
The type of button being created.
4039
Must be one of ``first``, ``prev``, ``next``, or ``last``.
4140
paginator: :class:`Paginator`
42-
The Paginator class where this button will be used
41+
The paginator class where this button will be used.
4342
"""
4443

4544
def __init__(self, label, emoji, style, disabled, button_type, paginator):
@@ -69,30 +68,30 @@ class Paginator(discord.ui.View):
6968
Attributes
7069
----------
7170
current_page: :class:`int`
72-
Zero-indexed value showing the current page number
71+
A zero-indexed value showing the current page number.
7372
page_count: :class:`int`
74-
Zero-indexed value showing the total number of pages
73+
A zero-indexed value showing the total number of pages.
7574
buttons: Dict[:class:`str`, Dict[:class:`str`, Union[:class:`~PaginatorButton`, :class:`bool`]]]
76-
Dictionary containing the :class:`~PaginatorButton` objects included in this Paginator
75+
A dictionary containing the :class:`~PaginatorButton` objects included in this paginator.
7776
user: Optional[Union[:class:`~discord.User`, :class:`~discord.Member`]]
78-
The user or member that invoked the Paginator.
77+
The user or member that invoked the paginator.
7978
message: Union[:class:`~discord.Message`, :class:`~discord.WebhookMessage`]
80-
The message sent from the Paginator.
79+
The message the paginator is attached to.
8180
8281
Parameters
8382
----------
8483
pages: Union[List[:class:`str`], List[:class:`discord.Embed`]]
85-
Your list of strings or embeds to paginate
84+
The list of strings and/or embeds to paginate.
8685
show_disabled: :class:`bool`
87-
Choose whether or not to show disabled buttons
86+
Whether to show disabled buttons.
8887
show_indicator: :class:`bool`
89-
Choose whether to show the page indicator
88+
Whether to show the page indicator.
9089
author_check: :class:`bool`
91-
Choose whether or not only the original user of the command can change pages
90+
Whether only the original user of the command can change pages.
9291
disable_on_timeout: :class:`bool`
93-
Should the buttons be disabled when the pagintator view times out?
92+
Whether the buttons get disabled when the pagintator view times out.
9493
custom_view: Optional[:class:`discord.ui.View`]
95-
A custom view whose items are appended below the pagination buttons
94+
A custom view whose items are appended below the pagination buttons.
9695
"""
9796

9897
def __init__(
@@ -104,7 +103,7 @@ def __init__(
104103
disable_on_timeout=True,
105104
custom_view: Optional[discord.ui.View] = None,
106105
timeout: Optional[float] = 180.0,
107-
):
106+
) -> None:
108107
super().__init__(timeout=timeout)
109108
self.timeout = timeout
110109
self.pages = pages
@@ -114,7 +113,7 @@ def __init__(
114113
self.show_indicator = show_indicator
115114
self.disable_on_timeout = disable_on_timeout
116115
self.custom_view = custom_view
117-
self.message: Union[discord.Message, discord.WebhookMessage]
116+
self.message: Union[discord.Message, discord.WebhookMessage, None] = None
118117
self.buttons = {
119118
"first": {
120119
"object": PaginatorButton(
@@ -182,7 +181,7 @@ async def on_timeout(self) -> None:
182181
item.disabled = True
183182
await self.message.edit(view=self)
184183

185-
async def goto_page(self, interaction: discord.Interaction, page_number=0):
184+
async def goto_page(self, interaction: discord.Interaction, page_number=0) -> None:
186185
"""Updates the interaction response message to show the specified page number.
187186
188187
Parameters
@@ -195,47 +194,43 @@ async def goto_page(self, interaction: discord.Interaction, page_number=0):
195194
.. note::
196195
197196
Page numbers are zero-indexed when referenced internally, but appear as one-indexed when shown to the user.
198-
199-
Returns
200-
---------
201-
:class:`~Paginator`
202-
The Paginator class
203197
"""
204198
self.update_buttons()
205199
page = self.pages[page_number]
206200
await interaction.response.edit_message(
207201
content=page if isinstance(page, str) else None, embed=page if isinstance(page, discord.Embed) else None, view=self
208202
)
209203

210-
async def interaction_check(self, interaction):
204+
async def interaction_check(self, interaction: discord.Interaction) -> bool:
211205
if self.usercheck:
212206
return self.user == interaction.user
213207
return True
214208

215209
def customize_button(
216210
self, button_name: str = None, button_label: str = None, button_emoji=None, button_style: discord.ButtonStyle = discord.ButtonStyle.gray
217-
) -> Union[PaginatorButton, bool]:
211+
) -> PaginatorButton:
218212
"""Allows you to easily customize the various pagination buttons.
219213
220214
Parameters
221215
----------
222216
button_name: :class:`str`
223-
Name of the button to customize
217+
The name of the button to customize.
218+
Must be one of ``first``, ``prev``, ``next``, or ``last``.
224219
button_label: :class:`str`
225-
Label to display on the button
220+
The label to display on the button.
226221
button_emoji:
227-
Emoji to display on the button
222+
The emoji to display on the button.
228223
button_style: :class:`~discord.ButtonStyle`
229-
ButtonStyle to use for the button
224+
The ButtonStyle to use for the button.
230225
231226
Returns
232227
-------
233228
:class:`~PaginatorButton`
234-
The button that was customized
229+
The button that was customized.
235230
"""
236231

237232
if button_name not in self.buttons.keys():
238-
return False
233+
raise ValueError(f"no button named {button_name} was found in this view.")
239234
button: PaginatorButton = self.buttons[button_name]["object"]
240235
button.label = button_label
241236
button.emoji = button_emoji
@@ -248,7 +243,7 @@ def update_buttons(self) -> Dict:
248243
Returns
249244
-------
250245
Dict[:class:`str`, Dict[:class:`str`, Union[:class:`~PaginatorButton`, :class:`bool`]]]
251-
The dictionary of buttons that was updated.
246+
The dictionary of buttons that were updated.
252247
"""
253248
for key, button in self.buttons.items():
254249
if key == "first":
@@ -294,7 +289,7 @@ def update_buttons(self) -> Dict:
294289

295290
return self.buttons
296291

297-
async def send(self, messageable: abc.Messageable, ephemeral: bool = False):
292+
async def send(self, messageable: abc.Messageable, ephemeral: bool = False) -> Union[discord.Message, discord.WebhookMessage]:
298293
"""Sends a message with the paginated items.
299294
300295
@@ -308,7 +303,7 @@ async def send(self, messageable: abc.Messageable, ephemeral: bool = False):
308303
Returns
309304
--------
310305
Union[:class:`~discord.Message`, :class:`~discord.WebhookMessage`]
311-
The message that was sent with the Paginator.
306+
The message that was sent with the paginator.
312307
"""
313308

314309
if not isinstance(messageable, abc.Messageable):
@@ -354,7 +349,7 @@ async def respond(self, interaction: discord.Interaction, ephemeral: bool = Fals
354349
Returns
355350
--------
356351
:class:`~discord.Interaction`
357-
The message sent with the paginator
352+
The message sent with the paginator.
358353
"""
359354
page = self.pages[0]
360355
self.user = interaction.user

docs/ext/menus/index.rst

Lines changed: 0 additions & 76 deletions
This file was deleted.

docs/ext/pages/index.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _discord_ext_pages:
2+
3+
``discord.ext.pages`` -- An extension module to provide useful pagination options
4+
===========================================================================
5+
6+
.. versionadded:: 2.0
7+
8+
This module provides an easy pagination system with buttons.
9+
10+
Example usage in a cog:
11+
12+
.. code-block:: python3
13+
14+
import discord
15+
from discord.commands import slash_command
16+
from discord.ext import commands, pages
17+
18+
19+
class PageTest(commands.Cog):
20+
def __init__(self, bot):
21+
self.bot = bot
22+
23+
self.pages = [
24+
"Page One", # string (text)
25+
discord.Embed(title="Page Two"), # rich embed
26+
discord.Embed(title="Page Three"), # another rich embed
27+
]
28+
self.pages[1].set_image(url="https://c.tenor.com/pPKOYQpTO8AAAAAM/monkey-developer.gif")
29+
self.pages[2].add_field(name="Example Field", value="Example Value", inline=False)
30+
self.pages[2].add_field(name="Another Example Field", value="Another Example Value", inline=False)
31+
32+
def get_pages(self):
33+
return self.pages
34+
35+
@slash_command(name="pagetest")
36+
async def pagetest(self, ctx):
37+
await ctx.defer()
38+
# initializing the paginator
39+
paginator = menus.Paginator(pages=self.get_pages(), show_disabled=False, show_indicator=True)
40+
41+
# customising buttons
42+
paginator.customize_button("next", button_label=">", button_style=discord.ButtonStyle.green)
43+
paginator.customize_button("prev", button_label="<", button_style=discord.ButtonStyle.green)
44+
paginator.customize_button("first", button_label="<<", button_style=discord.ButtonStyle.blurple)
45+
paginator.customize_button("last", button_label=">>", button_style=discord.ButtonStyle.blurple)
46+
47+
# start paginating
48+
await paginator.send(ctx, ephemeral=False)
49+
50+
# using a custom view
51+
@slash_command(name="pagetest_custom")
52+
async def pagetest_custom(self, ctx):
53+
await ctx.defer()
54+
view = discord.ui.View()
55+
view.add_item(discord.ui.Button(label="Test Button, Does Nothing", row=1))
56+
view.add_item(
57+
discord.ui.Select(
58+
placeholder="Test Select Menu, Does Nothing",
59+
options=[discord.SelectOption(label="Example Option", value="Example Value", description="This menu does nothing!")],
60+
)
61+
)
62+
paginator = menus.Paginator(pages=self.get_pages(), show_disabled=False, show_indicator=True, custom_view=view)
63+
await paginator.send(ctx, ephemeral=False)
64+
65+
66+
def setup(bot):
67+
bot.add_cog(PageTest(bot))
68+
69+
.. _discord_ext_pages_api:
70+
71+
API Reference
72+
-------------
73+
74+
.. attributetable:: discord.ext.pages.Paginator
75+
76+
.. autoclass:: discord.ext.pages.Paginator
77+
:members:
78+
79+
.. autoclass:: discord.ext.pages.PaginatorButton
80+
:members:

examples/views/paginator.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# Docs: https://docs.pycord.dev/en/master/ext/pages/index.html
2+
13
import discord
24
from discord.commands import slash_command
3-
from discord.ext import commands, menus
5+
from discord.ext import commands, pages
46

57

68
class PageTest(commands.Cog):
@@ -22,12 +24,12 @@ def get_pages(self):
2224
@slash_command(name="pagetest")
2325
async def pagetest(self, ctx):
2426
await ctx.defer()
25-
pages = menus.Paginator(pages=self.get_pages(), show_disabled=False, show_indicator=True)
26-
pages.customize_button("next", button_label=">", button_style=discord.ButtonStyle.green)
27-
pages.customize_button("prev", button_label="<", button_style=discord.ButtonStyle.green)
28-
pages.customize_button("first", button_label="<<", button_style=discord.ButtonStyle.blurple)
29-
pages.customize_button("last", button_label=">>", button_style=discord.ButtonStyle.blurple)
30-
await pages.send(ctx, ephemeral=False)
27+
paginator = pages.Paginator(pages=self.get_pages(), show_disabled=False, show_indicator=True)
28+
paginator.customize_button("next", button_label=">", button_style=discord.ButtonStyle.green)
29+
paginator.customize_button("prev", button_label="<", button_style=discord.ButtonStyle.green)
30+
paginator.customize_button("first", button_label="<<", button_style=discord.ButtonStyle.blurple)
31+
paginator.customize_button("last", button_label=">>", button_style=discord.ButtonStyle.blurple)
32+
await paginator.send(ctx, ephemeral=False)
3133

3234
@slash_command(name="pagetest_custom")
3335
async def pagetest_custom(self, ctx):
@@ -40,8 +42,8 @@ async def pagetest_custom(self, ctx):
4042
options=[discord.SelectOption(label="Example Option", value="Example Value", description="This menu does nothing!")],
4143
)
4244
)
43-
pages = menus.Paginator(pages=self.get_pages(), show_disabled=False, show_indicator=True, custom_view=view)
44-
await pages.send(ctx, ephemeral=False)
45+
paginator = pages.Paginator(pages=self.get_pages(), show_disabled=False, show_indicator=True, custom_view=view)
46+
await paginator.send(ctx, ephemeral=False)
4547

4648

4749
def setup(bot):

0 commit comments

Comments
 (0)