-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator.py
60 lines (52 loc) · 2.28 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
import asyncio
import discord
async def embed_list_paginated(ctx, bot, pre, items, item_fct, base_embed, footer_prefix='', msg=None, start=0, per_page=10):
embed = base_embed
# generate list
embed.title = f'{items.__len__()} entries'
text = '\n'
for i,item in enumerate(items[start:start+per_page]):
j = i+start
text += item_fct(j,item) + '\n'
embed.description = text
# footer text
#footer_text = f'Type {pre}show <label> to show a poll. '
footer_text = footer_prefix
if start > 0:
footer_text += f'React with ⏪ to show the last {per_page} entries. '
if items.__len__() > start+per_page:
footer_text += f'React with ⏩ to show the next {per_page} entries. '
if footer_text.__len__() > 0:
embed.set_footer(text=footer_text)
# post / edit message
if msg is not None:
await msg.edit(embed=embed)
if not isinstance(msg.channel, discord.abc.PrivateChannel):
await msg.clear_reactions()
else:
msg = await ctx.followup.send(embed=embed)
# add reactions
if start > 0:
await msg.add_reaction('⏪')
if items.__len__() > start+per_page:
await msg.add_reaction('⏩')
# wait for reactions (2 minutes)
def check(reaction, user):
return True if user != bot.user and str(reaction.emoji) in ['⏪', '⏩'] and reaction.message.id == msg.id else False
try:
reaction, user = await bot.wait_for('reaction_add', timeout=120, check=check)
except asyncio.TimeoutError:
try:
await msg.delete()
await ctx.message.delete()
except discord.errors.NotFound:
# message already deleted
pass
else:
# redirect on reaction
if reaction is None:
return
elif reaction.emoji == '⏪' and start > 0:
await embed_list_paginated(ctx, bot, pre, items, item_fct, base_embed, footer_prefix=footer_prefix, msg=msg, start=start-per_page, per_page=per_page)
elif reaction.emoji == '⏩' and items.__len__() > start+per_page:
await embed_list_paginated(ctx, bot, pre, items, item_fct, base_embed, footer_prefix=footer_prefix, msg=msg, start=start+per_page, per_page=per_page)