Skip to content

Commit 3ab5161

Browse files
authored
Add (truncated) preview to snippets command (#3342)
* Add (truncated) preview to snippets command * Add old view as option with "compact" * Fix black formatting
1 parent 4212c59 commit 3ab5161

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

cogs/modmail.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ async def snippet(self, ctx, *, name: str.lower = None):
143143
"""
144144

145145
if name is not None:
146+
if name == "compact":
147+
embeds = []
148+
149+
for i, names in enumerate(zip_longest(*(iter(sorted(self.bot.snippets)),) * 15)):
150+
description = format_description(i, names)
151+
embed = discord.Embed(color=self.bot.main_color, description=description)
152+
embed.set_author(
153+
name="Snippets", icon_url=self.bot.get_guild_icon(guild=ctx.guild, size=128)
154+
)
155+
embeds.append(embed)
156+
157+
session = EmbedPaginatorSession(ctx, *embeds)
158+
await session.run()
159+
return
160+
146161
snippet_name = self.bot._resolve_snippet(name)
147162

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

165-
embeds = []
166-
167-
for i, names in enumerate(zip_longest(*(iter(sorted(self.bot.snippets)),) * 15)):
168-
description = format_description(i, names)
169-
embed = discord.Embed(color=self.bot.main_color, description=description)
180+
embeds = [discord.Embed(color=self.bot.main_color) for _ in range((len(self.bot.snippets) // 10) + 1)]
181+
for embed in embeds:
170182
embed.set_author(name="Snippets", icon_url=self.bot.get_guild_icon(guild=ctx.guild, size=128))
171-
embeds.append(embed)
183+
184+
for i, snippet in enumerate(sorted(self.bot.snippets.items())):
185+
embeds[i // 10].add_field(
186+
name=snippet[0], value=return_or_truncate(snippet[1], 350), inline=False
187+
)
172188

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

core/utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"get_top_role",
4040
"get_joint_id",
4141
"extract_block_timestamp",
42+
"return_or_truncate",
4243
"AcceptButton",
4344
"DenyButton",
4445
"ConfirmThreadCreationView",
@@ -573,6 +574,12 @@ def extract_block_timestamp(reason, id_):
573574
return end_time, after
574575

575576

577+
def return_or_truncate(text, max_length):
578+
if len(text) <= max_length:
579+
return text
580+
return text[: max_length - 3] + "..."
581+
582+
576583
class AcceptButton(discord.ui.Button):
577584
def __init__(self, emoji):
578585
super().__init__(style=discord.ButtonStyle.gray, emoji=emoji)

0 commit comments

Comments
 (0)