From 03f2827ff641246bb548c24a4b1a34f43ffcb80b Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sun, 30 Aug 2020 01:43:25 +0100 Subject: [PATCH] Updating the paginator to allow to be modular --- bot/libs/paginator.py | 74 ++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/bot/libs/paginator.py b/bot/libs/paginator.py index b3f012cf..8e3553d7 100644 --- a/bot/libs/paginator.py +++ b/bot/libs/paginator.py @@ -13,28 +13,50 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - +from discord import Embed from discord.ext import menus -class AllPermissions(menus.Menu): - def __init__(self, i, item, perms, embeds, bot, guild_bot): +class SimpleMenu(menus.Menu): + def __init__(self, i, item, perms, embeds, bot): super().__init__(timeout=125.0, clear_reactions_after=True) self.i = i self.perms = perms self.dicts = embeds self.type = item self.bot = bot - self.guild_bot = guild_bot + + async def remove_reaction(self, reaction): + """Remove the reaction given""" + + if self.perms.manage_messages: + await self.message.remove_reaction(reaction, self.ctx.author) + + @staticmethod + def check(m, payload): + """Simple check to make sure that the reaction is performed by the user""" + + return m.author == payload.member and m.channel.id == payload.channel_id + + @staticmethod + def get_page(self): + """ + Return the current page index + """ + + cur_page = self.i + 1 + pages = len(self.dicts) + + return cur_page, pages async def send_initial_message(self, ctx, channel): """Set the first embed to the first element in the pages[]""" initial = self.dicts[self.i] - cur_page = self.i + 1 + cur_page, pages = self.get_page(self) pages = len(self.dicts) - initial.set_author(name=f"{self.type} Permissions | Page {cur_page}/{pages}") + initial.set_author(name=f"{self.type} | Page {cur_page}/{pages}") # Send embed return await channel.send(embed=initial) @@ -43,58 +65,44 @@ class AllPermissions(menus.Menu): async def on_left_arrow(self, payload): """Reaction to allow user to go to the previous page in the embed""" - # Simple check to make sure that the reaction is performed by the user - def check(m): - return m.author == payload.member - # Do nothing if the check does not return true - if check(self.ctx): + if self.check(self.ctx, payload): # Set self.i to (i - 1) remainder length of the array self.i = (self.i - 1) % len(self.dicts) prev_page = self.dicts[self.i] - cur_page = self.i + 1 - pages = len(self.dicts) - prev_page.set_author(name=f"{self.type} Permissions | Page {cur_page}/{pages}") + cur_page, pages = self.get_page(self) + prev_page.set_author(name=f"{self.type} | Page {cur_page}/{pages}") # Send the embed and remove the reaction of the user await self.message.edit(embed=prev_page) - if self.perms.manage_messages: - await self.message.remove_reaction("⬅", self.ctx.author) + await self.remove_reaction("⬅") @menus.button('\N{BLACK RIGHTWARDS ARROW}') async def on_right_arrow(self, payload): """Reaction to allow user to go to the next page in the embed""" - # Simple check to make sure that the reaction is performed by the user - def check(m): - return m.author == payload.member - # Do nothing if the check does not return true - if check(self.ctx): + if self.check(self.ctx, payload): # Set self.i to (i + 1) remainder length of the array self.i = (self.i + 1) % len(self.dicts) next_page = self.dicts[self.i] - cur_page = self.i + 1 - pages = len(self.dicts) - next_page.set_author(name=f"{self.type} Permissions | Page {cur_page}/{pages}") + cur_page, pages = self.get_page(self) + next_page.set_author(name=f"{self.type} | Page {cur_page}/{pages}") # Send the embed and remove the reaction of the user await self.message.edit(embed=next_page) - if self.perms.manage_messages: - await self.message.remove_reaction("➡", self.ctx.author) + await self.remove_reaction("➡") @menus.button('\N{BLACK SQUARE FOR STOP}\ufe0f') async def on_stop(self, payload): """Reaction to allow user to make the embed disappear""" - # Simple check to make sure that the reaction is performed by the user - def check(m): - return m.author == payload.member - # Do nothing if the check does not return true - if check(self.ctx): - # Delete the embed and stop the function from running - await self.message.delete() + if self.check(self.ctx, payload): + # Edit the embed and tell the member that the session has been closed + embed = Embed(description="**Pagination Session Has Been Closed**", + colour=self.bot.admin_colour) + await self.message.edit(embed=embed) self.stop()