Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (truncated) preview to snippets command #3342

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ async def snippet(self, ctx, *, name: str.lower = None):
"""

if name is not None:
if name == "compact":
embeds = []

for i, names in enumerate(zip_longest(*(iter(sorted(self.bot.snippets)),) * 15)):
description = format_description(i, names)
embed = discord.Embed(color=self.bot.main_color, description=description)
embed.set_author(
name="Snippets", icon_url=self.bot.get_guild_icon(guild=ctx.guild, size=128)
)
embeds.append(embed)

session = EmbedPaginatorSession(ctx, *embeds)
await session.run()
return

snippet_name = self.bot._resolve_snippet(name)

if snippet_name is None:
Expand All @@ -162,13 +177,14 @@ async def snippet(self, ctx, *, name: str.lower = None):
embed.set_author(name="Snippets", icon_url=self.bot.get_guild_icon(guild=ctx.guild, size=128))
return await ctx.send(embed=embed)

embeds = []

for i, names in enumerate(zip_longest(*(iter(sorted(self.bot.snippets)),) * 15)):
description = format_description(i, names)
embed = discord.Embed(color=self.bot.main_color, description=description)
embeds = [discord.Embed(color=self.bot.main_color) for _ in range((len(self.bot.snippets) // 10) + 1)]
for embed in embeds:
embed.set_author(name="Snippets", icon_url=self.bot.get_guild_icon(guild=ctx.guild, size=128))
embeds.append(embed)

for i, snippet in enumerate(sorted(self.bot.snippets.items())):
embeds[i // 10].add_field(
name=snippet[0], value=return_or_truncate(snippet[1], 350), inline=False
)

session = EmbedPaginatorSession(ctx, *embeds)
await session.run()
Expand Down
7 changes: 7 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"get_top_role",
"get_joint_id",
"extract_block_timestamp",
"return_or_truncate",
"AcceptButton",
"DenyButton",
"ConfirmThreadCreationView",
Expand Down Expand Up @@ -573,6 +574,12 @@ def extract_block_timestamp(reason, id_):
return end_time, after


def return_or_truncate(text, max_length):
if len(text) <= max_length:
return text
return text[: max_length - 3] + "..."


class AcceptButton(discord.ui.Button):
def __init__(self, emoji):
super().__init__(style=discord.ButtonStyle.gray, emoji=emoji)
Expand Down
Loading