Skip to content

Commit 79616fe

Browse files
committed
Add a delete button for paginators
1 parent a712667 commit 79616fe

File tree

1 file changed

+44
-52
lines changed

1 file changed

+44
-52
lines changed

core/paginator.py

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,26 @@ class PaginatorSession:
4343

4444
def __init__(self, ctx: commands.Context, *embeds, **options):
4545
self.ctx = ctx
46-
self.timeout: int = options.get('timeout', 210)
46+
self.timeout: int = options.get("timeout", 210)
4747
self.embeds: typing.List[Embed] = list(embeds)
4848
self.running = False
4949
self.base: Message = None
5050
self.current = 0
51-
self.destination = options.get('destination', ctx)
51+
self.destination = options.get("destination", ctx)
5252
self.reaction_map = {
53-
'⏮': self.first_page,
54-
'◀': self.previous_page,
55-
'▶': self.next_page,
56-
'⏭': self.last_page,
57-
# '⏹': self.close
53+
"⏮": self.first_page,
54+
"◀": self.previous_page,
55+
"▶": self.next_page,
56+
"⏭": self.last_page,
57+
"🛑": self.close,
5858
}
5959

60-
if options.get('edit_footer', True) and len(self.embeds) > 1:
60+
if options.get("edit_footer", True) and len(self.embeds) > 1:
6161
for i, embed in enumerate(self.embeds):
62-
footer_text = f'Page {i + 1} of {len(self.embeds)}'
62+
footer_text = f"Page {i + 1} of {len(self.embeds)}"
6363
if embed.footer.text:
64-
footer_text = footer_text + ' • ' + embed.footer.text
65-
embed.set_footer(text=footer_text,
66-
icon_url=embed.footer.icon_url)
64+
footer_text = footer_text + " • " + embed.footer.text
65+
embed.set_footer(text=footer_text, icon_url=embed.footer.icon_url)
6766

6867
def add_page(self, embed: Embed) -> None:
6968
"""
@@ -77,7 +76,7 @@ def add_page(self, embed: Embed) -> None:
7776
if isinstance(embed, Embed):
7877
self.embeds.append(embed)
7978
else:
80-
raise TypeError('Page must be an Embed object.')
79+
raise TypeError("Page must be an Embed object.")
8180

8281
async def create_base(self, embed: Embed) -> None:
8382
"""
@@ -96,7 +95,7 @@ async def create_base(self, embed: Embed) -> None:
9695

9796
self.running = True
9897
for reaction in self.reaction_map:
99-
if len(self.embeds) == 2 and reaction in '⏮⏭':
98+
if len(self.embeds) == 2 and reaction in "⏮⏭":
10099
continue
101100
await self.base.add_reaction(reaction)
102101

@@ -134,9 +133,11 @@ def react_check(self, reaction: Reaction, user: User) -> bool:
134133
-------
135134
bool
136135
"""
137-
return (reaction.message.id == self.base.id and
138-
user.id == self.ctx.author.id and
139-
reaction.emoji in self.reaction_map.keys())
136+
return (
137+
reaction.message.id == self.base.id
138+
and user.id == self.ctx.author.id
139+
and reaction.emoji in self.reaction_map.keys()
140+
)
140141

141142
async def run(self) -> typing.Optional[Message]:
142143
"""
@@ -152,9 +153,7 @@ async def run(self) -> typing.Optional[Message]:
152153
while self.running:
153154
try:
154155
reaction, user = await self.ctx.bot.wait_for(
155-
'reaction_add',
156-
check=self.react_check,
157-
timeout=self.timeout
156+
"reaction_add", check=self.react_check, timeout=self.timeout
158157
)
159158
except TimeoutError:
160159
return await self.close(delete=False)
@@ -195,10 +194,7 @@ async def close(self, delete: bool = True) -> typing.Optional[Message]:
195194
"""
196195
self.running = False
197196

198-
try:
199-
await self.ctx.message.add_reaction('✅')
200-
except (HTTPException, InvalidArgument):
201-
pass
197+
self.bot.loop.create
202198

203199
if delete:
204200
return await self.base.delete()
@@ -222,10 +218,11 @@ async def last_page(self) -> None:
222218

223219

224220
class MessagePaginatorSession:
225-
def __init__(self, ctx: commands.Context, *messages,
226-
embed: Embed = None, **options):
221+
def __init__(
222+
self, ctx: commands.Context, *messages, embed: Embed = None, **options
223+
):
227224
self.ctx = ctx
228-
self.timeout: int = options.get('timeout', 180)
225+
self.timeout: int = options.get("timeout", 180)
229226
self.messages: typing.List[str] = list(messages)
230227

231228
self.running = False
@@ -238,11 +235,11 @@ def __init__(self, ctx: commands.Context, *messages,
238235

239236
self.current = 0
240237
self.reaction_map = {
241-
'⏮': self.first_page,
242-
'◀': self.previous_page,
243-
'▶': self.next_page,
244-
'⏭': self.last_page,
245-
# '⏹': self.close
238+
"⏮": self.first_page,
239+
"◀": self.previous_page,
240+
"▶": self.next_page,
241+
"⏭": self.last_page,
242+
"🛑": self.close,
246243
}
247244

248245
def add_page(self, msg: str) -> None:
@@ -257,7 +254,7 @@ def add_page(self, msg: str) -> None:
257254
if isinstance(msg, str):
258255
self.messages.append(msg)
259256
else:
260-
raise TypeError('Page must be a str object.')
257+
raise TypeError("Page must be a str object.")
261258

262259
async def create_base(self, msg: str) -> None:
263260
"""
@@ -269,11 +266,10 @@ async def create_base(self, msg: str) -> None:
269266
The message content to fill the base `Message`.
270267
"""
271268
if self.embed is not None:
272-
footer_text = f'Page {self.current+1} of {len(self.messages)}'
269+
footer_text = f"Page {self.current+1} of {len(self.messages)}"
273270
if self.footer_text:
274-
footer_text = footer_text + ' • ' + self.footer_text
275-
self.embed.set_footer(text=footer_text,
276-
icon_url=self.embed.footer.icon_url)
271+
footer_text = footer_text + " • " + self.footer_text
272+
self.embed.set_footer(text=footer_text, icon_url=self.embed.footer.icon_url)
277273

278274
self.base = await self.ctx.send(content=msg, embed=self.embed)
279275

@@ -283,7 +279,7 @@ async def create_base(self, msg: str) -> None:
283279

284280
self.running = True
285281
for reaction in self.reaction_map:
286-
if len(self.messages) == 2 and reaction in '⏮⏭':
282+
if len(self.messages) == 2 and reaction in "⏮⏭":
287283
continue
288284
await self.base.add_reaction(reaction)
289285

@@ -303,11 +299,10 @@ async def show_page(self, index: int) -> None:
303299
page = self.messages[index]
304300

305301
if self.embed is not None:
306-
footer_text = f'Page {self.current + 1} of {len(self.messages)}'
302+
footer_text = f"Page {self.current + 1} of {len(self.messages)}"
307303
if self.footer_text:
308-
footer_text = footer_text + ' • ' + self.footer_text
309-
self.embed.set_footer(text=footer_text,
310-
icon_url=self.embed.footer.icon_url)
304+
footer_text = footer_text + " • " + self.footer_text
305+
self.embed.set_footer(text=footer_text, icon_url=self.embed.footer.icon_url)
311306

312307
if self.running:
313308
await self.base.edit(content=page, embed=self.embed)
@@ -328,9 +323,11 @@ def react_check(self, reaction: Reaction, user: User) -> bool:
328323
-------
329324
bool
330325
"""
331-
return (reaction.message.id == self.base.id and
332-
user.id == self.ctx.author.id and
333-
reaction.emoji in self.reaction_map.keys())
326+
return (
327+
reaction.message.id == self.base.id
328+
and user.id == self.ctx.author.id
329+
and reaction.emoji in self.reaction_map.keys()
330+
)
334331

335332
async def run(self) -> typing.Optional[Message]:
336333
"""
@@ -346,9 +343,7 @@ async def run(self) -> typing.Optional[Message]:
346343
while self.running:
347344
try:
348345
reaction, user = await self.ctx.bot.wait_for(
349-
'reaction_add',
350-
check=self.react_check,
351-
timeout=self.timeout
346+
"reaction_add", check=self.react_check, timeout=self.timeout
352347
)
353348
except TimeoutError:
354349
return await self.close(delete=False)
@@ -389,10 +384,7 @@ async def close(self, delete: bool = True) -> typing.Optional[Message]:
389384
"""
390385
self.running = False
391386

392-
try:
393-
await self.ctx.message.add_reaction('✅')
394-
except (HTTPException, InvalidArgument):
395-
pass
387+
self.ctx.bot.loop.create_task(self.ctx.message.add_reaction("✅"))
396388

397389
if delete:
398390
return await self.base.delete()

0 commit comments

Comments
 (0)