From 02892a0978ca9e17a64b547b0a65e60a9c82d868 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Thu, 3 Jan 2019 15:34:50 +0100 Subject: Fixing empty LinesPaginator and ImagePaginator with '(nothing to display)' and '(no images to display)' when called with empty container object --- bot/pagination.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bot/pagination.py b/bot/pagination.py index 0d8e8aaa3..7eaad2a6d 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -151,6 +151,10 @@ class LinePaginator(Paginator): paginator = cls(prefix=prefix, suffix=suffix, max_size=max_size, max_lines=max_lines) current_page = 0 + if not lines: + log.debug("No lines to add to paginator, adding empty line") + lines.append("(nothing to display)") + for line in lines: try: paginator.add_line(line, empty=empty) @@ -361,6 +365,10 @@ class ImagePaginator(Paginator): paginator = cls(prefix=prefix, suffix=suffix) current_page = 0 + if not pages: + log.debug("No images to add to paginator, adding empty line") + pages.append(("(no images to display)", "")) + for text, image_url in pages: paginator.add_line(text) paginator.add_image(image_url) -- cgit v1.2.3 From eb9018699eab385310b64cf2127328c65c38f5aa Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Thu, 3 Jan 2019 16:21:23 +0100 Subject: Clarifying log.debug messages for empty embeds for both Paginators --- bot/pagination.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 7eaad2a6d..97468857c 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -152,7 +152,7 @@ class LinePaginator(Paginator): current_page = 0 if not lines: - log.debug("No lines to add to paginator, adding empty line") + log.debug("No lines to add to paginator, adding '(nothing to display)' message") lines.append("(nothing to display)") for line in lines: @@ -366,7 +366,7 @@ class ImagePaginator(Paginator): current_page = 0 if not pages: - log.debug("No images to add to paginator, adding empty line") + log.debug("No images to add to paginator, adding '(no images to display)' message") pages.append(("(no images to display)", "")) for text, image_url in pages: -- cgit v1.2.3 From 0aa4c1b2690096c1e491446e6a64bed434166abd Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Thu, 3 Jan 2019 16:26:13 +0100 Subject: Adding optional kwarg to raise Exception on empty paginator embed instead of sending empty embed --- bot/pagination.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 97468857c..72cfd83ef 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -17,6 +17,10 @@ PAGINATION_EMOJI = [FIRST_EMOJI, LEFT_EMOJI, RIGHT_EMOJI, LAST_EMOJI, DELETE_EMO log = logging.getLogger(__name__) +class EmptyPaginatorEmbed(Exception): + pass + + class LinePaginator(Paginator): """ A class that aids in paginating code blocks for Discord messages. @@ -96,7 +100,8 @@ class LinePaginator(Paginator): async def paginate(cls, lines: Iterable[str], ctx: Context, embed: Embed, prefix: str = "", suffix: str = "", max_lines: Optional[int] = None, max_size: int = 500, empty: bool = True, restrict_to_user: User = None, timeout: int = 300, - footer_text: str = None): + footer_text: str = None, + exception_on_empty_embed: bool = False): """ Use a paginator and set of reactions to provide pagination over a set of lines. The reactions are used to switch page, or to finish with pagination. @@ -152,6 +157,10 @@ class LinePaginator(Paginator): current_page = 0 if not lines: + if exception_on_empty_embed: + log.exception(f"Pagination asked for empty lines iterable") + raise EmptyPaginatorEmbed("No lines to paginate") + log.debug("No lines to add to paginator, adding '(nothing to display)' message") lines.append("(nothing to display)") @@ -319,7 +328,8 @@ class ImagePaginator(Paginator): @classmethod async def paginate(cls, pages: List[Tuple[str, str]], ctx: Context, embed: Embed, - prefix: str = "", suffix: str = "", timeout: int = 300): + prefix: str = "", suffix: str = "", timeout: int = 300, + exception_on_empty_embed: bool = False): """ Use a paginator and set of reactions to provide pagination over a set of title/image pairs.The reactions are @@ -366,6 +376,10 @@ class ImagePaginator(Paginator): current_page = 0 if not pages: + if exception_on_empty_embed: + log.exception(f"Pagination asked for empty image list") + raise EmptyPaginatorEmbed("No images to paginate") + log.debug("No images to add to paginator, adding '(no images to display)' message") pages.append(("(no images to display)", "")) -- cgit v1.2.3