Skip to content

Commit 792a3c7

Browse files
committed
add back ext.menus
1 parent 5d62aac commit 792a3c7

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

discord/ext/menus/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
discord.ext.menus
3+
~~~~~~~~~~~~~~~~~~~~~
4+
An extension module to provide useful menu options.
5+
"""
6+
7+
from .pagination import *

discord/ext/menus/pagination.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
from typing import List, Union
2+
3+
import discord
4+
from discord import abc
5+
from discord.commands import ApplicationContext
6+
from discord.ext.commands import Context
7+
from discord.utils import MISSING
8+
9+
10+
class Paginate(discord.ui.View):
11+
"""Creates a paginator for a message that is navigated with buttons.
12+
13+
Parameters
14+
------------
15+
pages: Union[List[:class:`str`], List[:class:`discord.Embed`]]
16+
Your list of strings or embeds to paginate
17+
show_disabled: :class:`bool`
18+
Choose whether or not to show disabled buttons
19+
"""
20+
21+
def __init__(self, pages: Union[List[str], List[discord.Embed]], show_disabled=True, author_check=True):
22+
super().__init__()
23+
self.pages = pages
24+
self.current_page = 1
25+
self.page_count = len(self.pages)
26+
self.show_disabled = show_disabled
27+
self.forbutton = self.children[1]
28+
self.prevbutton = self.children[0]
29+
self.usercheck = author_check
30+
self.user = None
31+
if not self.show_disabled:
32+
self.remove_item(self.children[0])
33+
if self.page_count == 1:
34+
self.remove_item(self.children[1])
35+
36+
async def interaction_check(self, interaction):
37+
if self.usercheck:
38+
return self.user == interaction.user
39+
return True
40+
41+
@discord.ui.button(label="<", style=discord.ButtonStyle.green, disabled=True)
42+
async def previous(self, button: discord.ui.Button, interaction: discord.Interaction):
43+
44+
self.current_page -= 1
45+
46+
if self.current_page == 1:
47+
button.disabled = True
48+
49+
if not self.show_disabled:
50+
if len(self.children) == 1:
51+
self.add_item(self.forbutton)
52+
self.forbutton.disabled = False
53+
if button.disabled:
54+
self.remove_item(button)
55+
else:
56+
self.children[1].disabled = False
57+
58+
page = self.pages[self.current_page - 1]
59+
await interaction.response.edit_message(content=page if isinstance(page, str) else None, embed=page if isinstance(page, discord.Embed) else MISSING, view=self)
60+
61+
@discord.ui.button(label='>', style=discord.ButtonStyle.green)
62+
async def forward(self, button: discord.ui.Button, interaction: discord.Interaction):
63+
64+
self.current_page += 1
65+
66+
if self.current_page == self.page_count:
67+
button.disabled = True
68+
69+
if not self.show_disabled:
70+
if len(self.children) == 1:
71+
self.add_item(self.prevbutton)
72+
self.prevbutton.disabled = False
73+
self.children[0], self.children[1] = self.children[1], self.children[0]
74+
if button.disabled:
75+
self.remove_item(button)
76+
else:
77+
self.children[0].disabled = False
78+
79+
page = self.pages[self.current_page - 1]
80+
await interaction.response.edit_message(content=page if isinstance(page, str) else None, embed=page if isinstance(page, discord.Embed) else MISSING, view=self)
81+
82+
async def send(self, messageable: abc.Messageable, ephemeral: bool = False):
83+
"""Sends a message with the paginated items.
84+
85+
Parameters
86+
------------
87+
messageable: :class:`discord.abc.Messageable`
88+
The messageable channel to send to.
89+
ephemeral: :class:`bool`
90+
Choose whether or not the message is ephemeral. Only works with slash commands.
91+
Returns
92+
--------
93+
:class:`~discord.Message`
94+
The message that was sent.
95+
"""
96+
97+
if not isinstance(messageable, abc.Messageable):
98+
raise TypeError("messageable should be a subclass of abc.Messageable")
99+
100+
page = self.pages[0]
101+
102+
if isinstance(messageable, (ApplicationContext, Context)):
103+
self.user = messageable.author
104+
105+
if isinstance(messageable, ApplicationContext):
106+
message = await messageable.respond(content=page if isinstance(page, str) else None, embed=page if isinstance(page, discord.Embed) else MISSING, view=self, ephemeral=ephemeral)
107+
else:
108+
message = await messageable.send(content=page if isinstance(page, str) else None, embed=page if isinstance(page, discord.Embed) else None, view=self)
109+
return message
110+
111+
def forward_button(self, label: str, color: str = "green"):
112+
"""Customize your forward button
113+
114+
Parameters
115+
------------
116+
label: :class:`str`
117+
The text you want on your button
118+
119+
color: :class:`str`
120+
The color of the button.
121+
122+
Raises
123+
--------
124+
AttributeError
125+
The color provided isn't supported
126+
"""
127+
128+
self.forbutton.label = label
129+
color = getattr(discord.ButtonStyle, color.lower())
130+
self.forbutton.style = color
131+
132+
def back_button(self, label: str, color: str = "green"):
133+
"""Customize your back button
134+
135+
Parameters
136+
------------
137+
label: :class:`str`
138+
The text you want on your button
139+
140+
color: :class:`str`
141+
The color of the button.
142+
143+
Raises
144+
--------
145+
AttributeError
146+
The color provided isn't supported
147+
"""
148+
149+
self.prevbutton.label = label
150+
color = getattr(discord.ButtonStyle, color.lower())
151+
self.prevbutton.style = color

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
'discord.commands',
6161
'discord.ext.commands',
6262
'discord.ext.tasks',
63+
'discord.ext.menus',
6364
]
6465

6566
setup(name='py-cord',

0 commit comments

Comments
 (0)