diff --git a/buttons/embed.py b/buttons/embed.py index d56e13661..d12e9a598 100644 --- a/buttons/embed.py +++ b/buttons/embed.py @@ -6,12 +6,6 @@ from features.leaderboard import LeaderboardPageSource -class ViewRowFull(Exception): - """Adding a Item to already filled row""" - - pass - - class PaginationView(BaseView): message: disnake.Message @@ -41,49 +35,50 @@ def __init__( :param int page: Starting page. :param bool show_page: Show the page number at the bottom of the embed, e.g.: 2/4. """ - self.page = page + super().__init__(timeout=timeout) self.author = author - self.locked = False - self.page_source = page_source - self.roll_around = roll_around + self.embeds = embeds + self.row = row self.perma_lock = perma_lock - if not page_source: + self.roll_around = roll_around + self.page_source = page_source + self.page = page + self.dynam_lock = False + + if not self.page_source: self.max_page = len(embeds) else: - self.max_page = page_source.get_max_pages() + self.max_page = self.page_source.get_max_pages() end_arrow = False - self.embeds = embeds - super().__init__(timeout=timeout) + if self.max_page <= 1: - return + return # No need for pagination + if show_page: self.add_page_numbers() - self.add_item( - disnake.ui.Button( - emoji="⏪", custom_id="embed:start_page", row=row, style=disnake.ButtonStyle.primary - ) - ) - self.add_item( - disnake.ui.Button( - emoji="◀", custom_id="embed:prev_page", row=row, style=disnake.ButtonStyle.primary - ) - ) - self.add_item( - disnake.ui.Button( - emoji="▶", custom_id="embed:next_page", row=row, style=disnake.ButtonStyle.primary - ) - ) + + # Add all buttons to the view and set their callbacks + self.start_button = disnake.ui.Button(emoji="⏪", row=row, style=disnake.ButtonStyle.primary) + self.start_button.callback = self.start_callback + self.add_item(self.start_button) + + self.prev_button = disnake.ui.Button(emoji="◀", row=row, style=disnake.ButtonStyle.primary) + self.prev_button.callback = self.prev_callback + self.add_item(self.prev_button) + + self.next_button = disnake.ui.Button(emoji="▶", row=row, style=disnake.ButtonStyle.primary) + self.next_button.callback = self.next_callback + self.add_item(self.next_button) + if end_arrow: - self.add_item( - disnake.ui.Button( - emoji="⏩", custom_id="embed:end_page", row=row, style=disnake.ButtonStyle.primary - ) - ) - if not perma_lock: - # if permanent lock is applied, dynamic lock is removed from buttons - self.lock_button = disnake.ui.Button( - emoji="🔓", custom_id="embed:lock", row=0, style=disnake.ButtonStyle.success - ) + self.end_button = disnake.ui.Button(emoji="⏩", row=row, style=disnake.ButtonStyle.primary) + self.end_button.callback = self.end_callback + self.add_item(self.end_button) + + if not self.perma_lock: + # if permanent lock is not applied, dynamic lock is added + self.lock_button = disnake.ui.Button(emoji="🔓", row=0, style=disnake.ButtonStyle.success) + self.lock_button.callback = self.lock_callback self.add_item(self.lock_button) @property @@ -98,19 +93,6 @@ def embed(self): def embed(self, value): self.embeds[self.page - 1] = value - def add_item(self, item: disnake.ui.Item) -> None: - row_cnt = 0 - for child in self.children: - if item.emoji == child.emoji: - child.disabled = False - return - if item.row == child.row: - row_cnt += 1 - if row_cnt >= 5: - # we are trying to add new button to already filled row - raise ViewRowFull - super().add_item(item) - def add_page_numbers(self): """Set footers with page numbers for each embed in list""" for page, embed in enumerate(self.embeds): @@ -118,31 +100,37 @@ def add_page_numbers(self): embed, self.author, additional_text=[f"Page {page+1}/{self.max_page}"] ) + async def lock_callback(self, interaction: disnake.MessageInteraction): + self.dynam_lock = not self.dynam_lock + if self.dynam_lock: + self.lock_button.style = disnake.ButtonStyle.danger + self.lock_button.emoji = "🔒" + else: + self.lock_button.style = disnake.ButtonStyle.success + self.lock_button.emoji = "🔓" + await interaction.response.edit_message(view=self) + + async def start_callback(self, interaction: disnake.MessageInteraction): + await self.pagination_callback(interaction, "start") + + async def prev_callback(self, interaction: disnake.MessageInteraction): + await self.pagination_callback(interaction, "prev") + + async def next_callback(self, interaction: disnake.MessageInteraction): + await self.pagination_callback(interaction, "next") + + async def end_callback(self, interaction: disnake.MessageInteraction): + await self.pagination_callback(interaction, "end") + + async def pagination_callback(self, interaction: disnake.MessageInteraction, id: str): + self.page = utils.embed.pagination_next(id, self.page, self.max_page, self.roll_around) + await interaction.response.edit_message(embed=self.embed, view=self) + async def interaction_check(self, interaction: disnake.MessageInteraction) -> bool: - if interaction.data.custom_id == "embed:lock": - if interaction.author.id == self.author.id: - self.locked = not self.locked - if self.locked: - self.lock_button.style = disnake.ButtonStyle.danger - self.lock_button.emoji = "🔒" - else: - self.lock_button.style = disnake.ButtonStyle.success - self.lock_button.emoji = "🔓" - await interaction.response.edit_message(view=self) - else: - await interaction.send(Messages.embed_not_author, ephemeral=True) - return False - ids = ["embed:start_page", "embed:prev_page", "embed:next_page", "embed:end_page"] - if interaction.data.custom_id not in ids or self.max_page <= 1: - return False - if (self.perma_lock or self.locked) and interaction.author.id != self.author.id: + if (self.perma_lock or self.dynam_lock) and interaction.author.id != self.author.id: + """Message has permanent lock or dynamic lock enabled""" await interaction.send(Messages.embed_not_author, ephemeral=True) return False - - self.page = utils.embed.pagination_next( - interaction.data.custom_id, self.page, self.max_page, self.roll_around - ) - await interaction.response.edit_message(embed=self.embed) return True async def on_timeout(self):