From 51d88850986b5a8ddf8405d8b18854ff8b440521 Mon Sep 17 00:00:00 2001 From: Sebastian Kuipers Date: Thu, 23 May 2024 21:45:32 +0200 Subject: [PATCH 1/3] Add (truncated) preview to snippets command --- cogs/modmail.py | 11 +++++------ core/utils.py | 6 ++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cogs/modmail.py b/cogs/modmail.py index ee27806177..62dde83daf 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -162,13 +162,12 @@ 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() diff --git a/core/utils.py b/core/utils.py index 9f9f572f5a..82290f1c6e 100644 --- a/core/utils.py +++ b/core/utils.py @@ -39,6 +39,7 @@ "get_top_role", "get_joint_id", "extract_block_timestamp", + "return_or_truncate", "AcceptButton", "DenyButton", "ConfirmThreadCreationView", @@ -573,6 +574,11 @@ 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) From 16a4eccfc9ff5881c3b4029034e28573240bae14 Mon Sep 17 00:00:00 2001 From: Sebastian Kuipers Date: Thu, 23 May 2024 22:21:22 +0200 Subject: [PATCH 2/3] Add old view as option with "compact" --- cogs/modmail.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cogs/modmail.py b/cogs/modmail.py index 62dde83daf..879a931335 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -143,6 +143,19 @@ 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: From 6833fa1c19f4fdc6fdd4b1432b5b446e9b067261 Mon Sep 17 00:00:00 2001 From: Sebastian Kuipers Date: Thu, 23 May 2024 22:23:26 +0200 Subject: [PATCH 3/3] Fix black formatting --- cogs/modmail.py | 8 ++++++-- core/utils.py | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cogs/modmail.py b/cogs/modmail.py index 879a931335..17bf07b514 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -149,7 +149,9 @@ async def snippet(self, ctx, *, name: str.lower = None): 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)) + embed.set_author( + name="Snippets", icon_url=self.bot.get_guild_icon(guild=ctx.guild, size=128) + ) embeds.append(embed) session = EmbedPaginatorSession(ctx, *embeds) @@ -180,7 +182,9 @@ 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)) 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) + embeds[i // 10].add_field( + name=snippet[0], value=return_or_truncate(snippet[1], 350), inline=False + ) session = EmbedPaginatorSession(ctx, *embeds) await session.run() diff --git a/core/utils.py b/core/utils.py index 82290f1c6e..149240dc08 100644 --- a/core/utils.py +++ b/core/utils.py @@ -579,6 +579,7 @@ def return_or_truncate(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)