pollmaster-docker/utils/paginator.py
2019-02-10 19:53:11 +01:00

44 lines
1.7 KiB
Python

async def embed_list_paginated(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 item in items[start:start+per_page]:
text += item_fct(item) + '\n'
embed.description = text
# footer text
#footer_text = f'Type {pre}show <label> to show a poll. '
if start > 0:
footer_prefix += f'React with ⏪ to show the last {per_page} entries. '
if items.__len__() > start+per_page:
footer_prefix += f'React with ⏩ to show the next {per_page} entries. '
if footer_prefix.__len__() > 0:
embed.set_footer(text=footer_prefix)
# post / edit message
if msg is not None:
await bot.edit_message(msg, embed=embed)
await bot.clear_reactions(msg)
else:
msg = await bot.say(embed=embed)
# add reactions
if start > 0:
await bot.add_reaction(msg, '')
if items.__len__() > start+per_page:
await bot.add_reaction(msg, '')
# wait for reactions (2 minutes)
def check(reaction, user):
return reaction.emoji if user != bot.user else False
res = await bot.wait_for_reaction(emoji=['', ''], message=msg, timeout=120, check=check)
# redirect on reaction
if res is None:
return
elif res.reaction.emoji == '' and start > 0:
await embed_list_paginated(bot, pre, items, item_fct, base_embed, msg=msg, start=start-per_page, per_page=per_page)
elif res.reaction.emoji == '' and items.__len__() > start+per_page:
await embed_list_paginated(bot, pre, items, item_fct, base_embed, msg=msg, start=start+per_page, per_page=per_page)