-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator.py
105 lines (93 loc) · 3.3 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
from diskord.ext import commands
import diskord
from typing import *
from contextlib import suppress
class Paginator:
def __init__(self, ctx: commands.Context, *, embeds: List[diskord.Embed]):
self.ctx = ctx
self.embeds = embeds
self.bot: commands.Bot = ctx.bot
self.index = 0
self.current = 1
self.timeout = 100.0
self.compact: bool = False
if len(self.embeds) == 2 or len(self.embeds) < 2:
self.compact = True
self._buttons: Dict[str, str] = {
"⏪": "stop",
"◀️": "plus",
"▶️": "last",
"⏩": "first",
"⏹️": "minus",
}
if self.compact:
keys = (
"⏩",
"⏪",
)
for key in keys:
del self._buttons[key]
async def send_initial_message(self):
self.message: diskord.Message = await self.ctx.send(
embed=self.embeds[0]
)
# print(self.message)
return self.message
async def start(self):
await self._paginate()
async def _paginate(self):
"""
Start the pagination session.
"""
message = await self.send_initial_message()
# print(message)
for b in self._buttons:
await message.add_reaction(b)
def check(
reaction: diskord.Reaction,
user: Union[diskord.Member, diskord.User],
):
return (
str(reaction.emoji) in self._buttons
and user == self.ctx.author
)
while True:
try:
reaction, user = await self.ctx.bot.wait_for(
"reaction_add", check=check, timeout=self.timeout
)
await self.try_delete_reaction(message, reaction)
embed = self.get_embed(reaction)
if not embed:
await message.delete()
break
else:
await message.edit(embed=embed)
except Exception as e:
with suppress(diskord.Forbidden, diskord.HTTPException):
for b in self._buttons:
await message.remove_reaction(b, self.ctx.bot.user)
def get_embed(self, reaction: diskord.Reaction):
if str(reaction.emoji) == "⏹️":
return None
if str(reaction.emoji) == "▶️" and self.current != len(
self.embeds
):
self.current += 1
return self.embeds[self.current - 1]
if str(reaction.emoji) == "◀️" and self.current > 1:
self.current -= 1
return self.embeds[self.current - 1]
if str(reaction.emoji) == "⏩":
self.current = len(self.embeds)
return self.embeds[self.current - 1]
if str(reaction.emoji) == "⏪":
self.current = 1
return self.embeds[self.current - 1]
async def try_delete_reaction(self, message: diskord.Message, emoji: diskord.Emoji):
if not message.channel.permissions_for(self.ctx.me).manage_messages:
return False
try:
await message.remove_reaction(emoji, self.ctx.author)
except Exception: # idgaf lel
return False