This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplepaginator.py
142 lines (112 loc) · 4.84 KB
/
simplepaginator.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
import discord
import asyncio
async def pager(entries, chunk: int):
for x in range(0, len(entries), chunk):
yield entries[x:x + chunk]
class SimplePaginator:
__slots__ = ('entries', 'extras', 'title', 'description', 'colour', 'footer', 'length', 'prepend', 'append',
'fmt', 'timeout', 'ordered', 'controls', 'controller', 'pages', 'current', 'previous', 'eof', 'base',
'names')
def __init__(self, **kwargs):
self.entries = kwargs.get('entries', None)
self.extras = kwargs.get('extras', None)
self.title = kwargs.get('title', None)
self.description = kwargs.get('description', None)
self.colour = kwargs.get('colour', 0xffd4d4)
self.footer = kwargs.get('footer', None)
self.length = kwargs.get('length', 10)
self.prepend = kwargs.get('prepend', '')
self.append = kwargs.get('append', '')
self.fmt = kwargs.get('fmt', '')
self.timeout = kwargs.get('timeout', 90)
self.ordered = kwargs.get('ordered', False)
self.controller = None
self.pages = []
self.names = []
self.base = None
self.current = 0
self.previous = 0
self.eof = 0
self.controls = {'⏮': 0.0, '◀': -1, '⏹': 'stop',
'▶': +1, '⏭': None, '\u274c': 'delete'}
async def indexer(self, ctx, ctrl):
if str(ctrl) in 'stopdelete':
ctx.bot.loop.create_task(self.stop_controller(self.base,int(ctrl=="stop")))
elif isinstance(ctrl, int):
self.current += ctrl
if self.current > self.eof or self.current < 0:
self.current -= ctrl
else:
self.current = int(ctrl)
async def reaction_controller(self, ctx):
bot = ctx.bot
author = ctx.author
self.base = await ctx.send(embed=self.pages[0])
if len(self.pages) == 1:
await self.base.add_reaction('⏹')
await self.base.add_reaction('\u274c')
else:
for reaction in self.controls:
try:
await self.base.add_reaction(reaction)
except discord.HTTPException:
return
def check(r, u):
if str(r) not in self.controls.keys():
return False
elif u.id == bot.user.id or r.message.id != self.base.id:
return False
elif u.id != author.id:
return False
return True
while True:
try:
react, user = await bot.wait_for('reaction_add', check=check, timeout=self.timeout)
except asyncio.TimeoutError:
return ctx.bot.loop.create_task(self.stop_controller(self.base))
control = self.controls.get(str(react))
try:
await self.base.remove_reaction(react, user)
except discord.HTTPException:
pass
self.previous = self.current
await self.indexer(ctx, control)
if self.previous == self.current:
continue
try:
await self.base.edit(embed=self.pages[self.current])
except KeyError:
pass
async def stop_controller(self, message, status=1):
if status==1:
try:
await message.clear_reactions()
except discord.HTTPException:
pass
elif status==0:
try:
await message.delete()
except discord.HTTPException:
pass
try:
self.controller.cancel()
except Exception:
pass
def formmater(self, chunk):
return '\n'.join(f'{self.prepend}{self.fmt}{value}{self.fmt[::-1]}{self.append}' for value in chunk)
async def paginate(self, ctx):
if self.extras:
self.pages = [p for p in self.extras if isinstance(p, discord.Embed)]
if self.entries:
chunks = [c async for c in pager(self.entries, self.length)]
for index, chunk in enumerate(chunks):
page = discord.Embed(title=f'{self.title} - {index + 1}/{len(chunks)}', color=self.colour)
page.description = self.formmater(chunk)
if self.footer:
page.set_footer(text=self.footer)
self.pages.append(page)
if not self.pages:
self.pages=[discord.Embed(title="Nothing Paginated",description="There is probably a blank output.",colour=discord.Colour.blurple())]
self.eof = float(len(self.pages) - 1)
self.controls['⏭'] = self.eof
self.controller = ctx.bot.loop.create_task(self.reaction_controller(ctx))