Skip to content

Commit

Permalink
fix: paginator can now not use an embed
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Nov 17, 2021
1 parent b63eb07 commit ba84673
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
48 changes: 37 additions & 11 deletions modmail/utils/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

logger: ModmailLogger = logging.getLogger(__name__)

_AUTOGENERATE = object()


class ButtonPaginator(ui.View, DpyPaginator):
"""
Expand All @@ -57,7 +59,7 @@ def __init__(
contents: Union[List[str], str],
/,
source_message: Optional[discord.Message] = None,
embed: Embed = None,
embed: Optional[Embed] = _AUTOGENERATE,
timeout: float = 180,
*,
footer_text: str = None,
Expand All @@ -82,7 +84,13 @@ def __init__(
self.suffix = suffix
self.max_size = max_size
self.linesep = linesep
self.embed = embed or Embed()
if embed is _AUTOGENERATE:
self.embed = Embed()
else:
self.embed = embed

# used if embed is None
self.content = ""

# temporary to support strings as contents. This will be changed when we added wrapping.
if isinstance(contents, str):
Expand Down Expand Up @@ -116,8 +124,8 @@ def __init__(

# set footer to embed.footer if embed is set
# this is because we will be modifying the footer of this embed
if embed is not None:
if not isinstance(embed.footer, EmbedProxy) and footer_text is None:
if self.embed is not None:
if not isinstance(self.embed.footer, EmbedProxy) and footer_text is None:
footer_text = embed.footer
self.footer_text = footer_text
self.clear()
Expand All @@ -140,7 +148,7 @@ async def paginate(
source_message: discord.Message = None,
/,
timeout: float = 180,
embed: Embed = None,
embed: Embed = _AUTOGENERATE,
*,
footer_text: str = None,
only: Optional[discord.abc.User] = None,
Expand Down Expand Up @@ -178,18 +186,28 @@ async def paginate(
channel = source_message.channel

paginator.update_states()
paginator.embed.description = paginator.pages[paginator.index]
if paginator.embed:
paginator.embed.description = paginator.pages[paginator.index]
else:
paginator.content = paginator.pages[paginator.index]
# if there's only one page, don't send the view
if len(paginator.pages) < 2:
await channel.send(embeds=[paginator.embed])
if paginator.embed:
await channel.send(embeds=[paginator.embed])
else:
await channel.send(content=paginator.content)

return

if len(paginator.pages) < (show_jump_buttons_min_pages or 3):
for item in paginator.children:
if getattr(item, "custom_id", None) in ["pag_jump_first", "pag_jump_last"]:
paginator.remove_item(item)

msg: discord.Message = await channel.send(embeds=[paginator.embed], view=paginator)
if paginator.embed is None:
msg: discord.Message = await channel.send(content=paginator.content, view=paginator)
else:
msg: discord.Message = await channel.send(embeds=[paginator.embed], view=paginator)

await paginator.wait()
await msg.edit(view=None)
Expand All @@ -212,8 +230,11 @@ async def interaction_check(self, interaction: Interaction) -> bool:
)
return False

def get_footer(self) -> str:
def get_footer(self) -> Optional[str]:
"""Returns the footer text."""
if self.embed is None:
self.content = self._pages[self.index]
return None
self.embed.description = self._pages[self.index]
page_indicator = f"Page {self.index+1}/{len(self._pages)}"
footer_txt = (
Expand All @@ -230,7 +251,9 @@ def update_states(self) -> None:
if the paginator is on the last page, the jump last/move forward buttons will be disabled.
"""
# update the footer
self.embed.set_footer(text=self.get_footer())
text = self.get_footer()
if self.embed:
self.embed.set_footer(text=text)

# determine if the jump buttons should be enabled
more_than_two_pages = len(self._pages) > 2
Expand Down Expand Up @@ -264,7 +287,10 @@ async def send_page(self, interaction: Interaction) -> None:
"""Send new page to discord, after updating the view to have properly disabled buttons."""
self.update_states()

await interaction.message.edit(embed=self.embed, view=self)
if self.embed:
await interaction.message.edit(embed=self.embed, view=self)
else:
await interaction.message.edit(content=self.content, view=self)

@ui.button(label=JUMP_FIRST_LABEL, custom_id="pag_jump_first", style=ButtonStyle.primary)
async def go_first(self, _: Button, interaction: Interaction) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/modmail/utils/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ async def test_paginator_footer(content: Union[str, List[str]], footer_text: str
pag = ButtonPaginator(content, footer_text=footer_text)
print("index:", pag.index)
print("page len: ", len(pag.pages))
assert pag.footer_text == footer_text
assert footer_text == pag.footer_text
if isinstance(content, str):
content = [content]

print(pag.get_footer())
if footer_text is not None:
assert pag.get_footer().endswith(f"{len(content)})")
assert pag.get_footer().startswith(footer_text)
Expand Down

0 comments on commit ba84673

Please sign in to comment.