-
Notifications
You must be signed in to change notification settings - Fork 396
/
paginator.py
72 lines (66 loc) · 2.93 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
import asyncio
import discord
class Paginator:
def __init__(self, message, base, embeds, obj):
self.message = message
self.base = base
self.pointers = ['👈','👉','❌']
self.embeds = embeds
self.cursor = 0
self.obj = obj
async def _add_handler(self):
def reaction_check(reaction,user):
return user == self.message.author and reaction.message.id == self.base.id and reaction.emoji in self.pointers
while True:
reaction, user = await discord.Client.wait_for(self.obj, event='reaction_add', check=reaction_check)
op = self.pointers.index(reaction.emoji)
if op == 1 and self.cursor < len(self.embeds) - 1:
self.cursor += 1
await self.base.edit(embed=self.embeds[self.cursor])
elif op == 0 and self.cursor > 0:
self.cursor -= 1
await self.base.edit(embed=self.embeds[self.cursor])
elif op == 2:
await self.base.delete()
break
else:
pass
async def _remove_handler(self):
def reaction_check(reaction,user):
return user == self.message.author and reaction.message.id == self.base.id and reaction.emoji in self.pointers
while True:
reaction, user = await discord.Client.wait_for(self.obj, event='reaction_remove', check=reaction_check)
op = self.pointers.index(reaction.emoji)
if op == 1 and self.cursor < len(self.embeds) - 1:
self.cursor += 1
await self.base.edit(embed=self.embeds[self.cursor])
elif op == 0 and self.cursor > 0:
self.cursor -= 1
await self.base.edit(embed=self.embeds[self.cursor])
else:
pass
async def run(self):
await self.base.edit(content=self.message.author.mention,embed=self.embeds[0])
for pointer in self.pointers:
await self.base.add_reaction(pointer)
asyncio.ensure_future(self._add_handler())
asyncio.ensure_future(self._remove_handler())
class Embedder:
def __init__(self, image):
self.image = image
def generate(self, title, fields, current, total):
def _len_check(embed, field_name, description):
i = 5
content = description
while i > 0:
if len(content) <= 1024:
embed.add_field(name=field_name, value=content, inline=False)
break
else:
content = '\n'.join(content.split('\n\n')[:i])
i -= 1
hub_embed = discord.Embed(title=title,description="\u200B",color=15728640)
for name, content in fields.items():
_len_check(hub_embed, name, content)
hub_embed.set_footer(text="{}/{}".format(current, total),icon_url=self.image)
return hub_embed