This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
forked from nielsvv08/enchanted-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaginator.py
246 lines (211 loc) · 7.16 KB
/
Paginator.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
import typing
import asyncio
from discord import User, Reaction, Message, Embed
from discord import HTTPException, InvalidArgument, NotFound
from discord.ext import commands
class PaginatorSession:
"""
Class that interactively paginates something.
Parameters
----------
ctx : Context
The context of the command.
timeout : float
How long to wait for before the session closes.
pages : List[Any]
A list of entries to paginate.
Attributes
----------
ctx : Context
The context of the command.
timeout : float
How long to wait for before the session closes.
pages : List[Any]
A list of entries to paginate.
running : bool
Whether the paginate session is running.
base : Message
The `Message` of the `Embed`.
current : int
The current page number.
reaction_map : Dict[str, method]
A mapping for reaction to method.
"""
def __init__(self, ctx: commands.Context, *pages, **options):
self.ctx = ctx
self.timeout: int = options.get("timeout", 210)
self.running = False
self.base: Message = None
self.current = 0
self.pages = list(pages)
self.destination = options.get("destination", ctx)
self.reaction_map = {
"⏮": self.first_page,
"◀": self.previous_page,
"▶": self.next_page,
"⏭": self.last_page,
"🛑": self.close,
}
def add_page(self, item) -> None:
"""
Add a page.
"""
raise NotImplementedError
async def create_base(self, item) -> None:
"""
Create a base `Message`.
"""
await self._create_base(item)
if len(self.pages) == 1:
self.running = False
return
self.running = True
for reaction in self.reaction_map:
if len(self.pages) == 2 and reaction in "⏮⏭":
continue
await self.base.add_reaction(reaction)
async def _create_base(self, item) -> None:
raise NotImplementedError
async def show_page(self, index: int) -> None:
"""
Show a page by page number.
Parameters
----------
index : int
The index of the page.
"""
if not 0 <= index < len(self.pages):
return
self.current = index
page = self.pages[index]
if self.running:
await self._show_page(page)
else:
await self.create_base(page)
async def _show_page(self, page):
raise NotImplementedError
def react_check(self, reaction: Reaction, user: User) -> bool:
"""
Parameters
----------
reaction : Reaction
The `Reaction` object of the reaction.
user : User
The `User` or `Member` object of who sent the reaction.
Returns
-------
bool
"""
return (
reaction.message.id == self.base.id
and user.id == self.ctx.author.id
and reaction.emoji in self.reaction_map.keys()
)
async def run(self) -> typing.Optional[Message]:
"""
Starts the pagination session.
Returns
-------
Optional[Message]
If it's closed before running ends.
"""
if not self.running:
await self.show_page(self.current)
while self.running:
try:
reaction, user = await self.ctx.bot.wait_for(
"reaction_add", check=self.react_check, timeout=self.timeout
)
except asyncio.TimeoutError:
return await self.close(delete=False)
else:
action = self.reaction_map.get(reaction.emoji)
await action()
try:
await self.base.remove_reaction(reaction, user)
except (HTTPException, InvalidArgument):
pass
async def previous_page(self) -> None:
"""
Go to the previous page.
"""
await self.show_page(self.current - 1)
async def next_page(self) -> None:
"""
Go to the next page.
"""
await self.show_page(self.current + 1)
async def close(self, delete: bool = True) -> typing.Optional[Message]:
"""
Closes the pagination session.
Parameters
----------
delete : bool, optional
Whether or delete the message upon closure.
Defaults to `True`.
Returns
-------
Optional[Message]
If `delete` is `True`.
"""
self.running = False
try:
await self.ctx.message.add_reaction("✅")
except NotFound:
pass
if delete:
return await self.base.delete()
try:
await self.base.clear_reactions()
except HTTPException:
pass
async def first_page(self) -> None:
"""
Go to the first page.
"""
await self.show_page(0)
async def last_page(self) -> None:
"""
Go to the last page.
"""
await self.show_page(len(self.pages) - 1)
class EmbedPaginatorSession(PaginatorSession):
def __init__(self, ctx: commands.Context, *embeds, **options):
super().__init__(ctx, *embeds, **options)
if len(self.pages) > 1:
for i, embed in enumerate(self.pages):
footer_text = f"Page {i + 1} of {len(self.pages)}"
if embed.footer.text:
footer_text = footer_text + " • " + embed.footer.text
embed.set_footer(text=footer_text, icon_url=embed.footer.icon_url)
def add_page(self, item: Embed) -> None:
if isinstance(item, Embed):
self.pages.append(item)
else:
raise TypeError("Page must be an Embed object.")
async def _create_base(self, item: Embed) -> None:
self.base = await self.destination.send(embed=item)
async def _show_page(self, page):
await self.base.edit(embed=page)
class MessagePaginatorSession(PaginatorSession):
def __init__(self, ctx: commands.Context, *messages, embed: Embed = None, **options):
self.embed = embed
self.footer_text = self.embed.footer.text if embed is not None else None
super().__init__(ctx, *messages, **options)
def add_page(self, item: str) -> None:
if isinstance(item, str):
self.pages.append(item)
else:
raise TypeError("Page must be a str object.")
def _set_footer(self):
if self.embed is not None:
footer_text = f"Page {self.current+1} of {len(self.pages)}"
if self.footer_text:
footer_text = footer_text + " • " + self.footer_text
self.embed.set_footer(text=footer_text, icon_url=self.embed.footer.icon_url)
async def _create_base(self, item: str) -> None:
self._set_footer()
self.base = await self.ctx.send(content=item, embed=self.embed)
async def _show_page(self, page) -> None:
self._set_footer()
await self.base.edit(content=page, embed=self.embed)