Skip to content

Commit 8ab3706

Browse files
committed
Feat: Renaming of snippets and aliases
This adds two commands for renaming snippets and aliases for easier name editing.
1 parent 4212c59 commit 8ab3706

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

cogs/modmail.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,61 @@ async def snippet_edit(self, ctx, name: str.lower, *, value):
375375
embed = create_not_found_embed(name, self.bot.snippets.keys(), "Snippet")
376376
await ctx.send(embed=embed)
377377

378+
@snippet.command(name="rename")
379+
@checks.has_permissions(PermissionLevel.SUPPORTER)
380+
async def snippet_rename(self, ctx, name: str.lower, *, value):
381+
"""
382+
Rename a snippet.
383+
384+
To rename a multi-word snippet name, use quotes: ```
385+
{prefix}snippet rename "two word" this is a new two word snippet.
386+
```
387+
"""
388+
if name in self.bot.snippets:
389+
if self.bot.get_command(value):
390+
embed = discord.Embed(
391+
title="Error",
392+
color=self.bot.error_color,
393+
description=f"A command with the same name already exists: `{value}`.",
394+
)
395+
return await ctx.send(embed=embed)
396+
elif value in self.bot.snippets:
397+
embed = discord.Embed(
398+
title="Error",
399+
color=self.bot.error_color,
400+
description=f"Snippet `{value}` already exists.",
401+
)
402+
return await ctx.send(embed=embed)
403+
404+
if value in self.bot.aliases:
405+
embed = discord.Embed(
406+
title="Error",
407+
color=self.bot.error_color,
408+
description=f"An alias that shares the same name exists: `{value}`.",
409+
)
410+
return await ctx.send(embed=embed)
411+
412+
if len(value) > 120:
413+
embed = discord.Embed(
414+
title="Error",
415+
color=self.bot.error_color,
416+
description="Snippet names cannot be longer than 120 characters.",
417+
)
418+
return await ctx.send(embed=embed)
419+
old_snippet_value = self.bot.snippets[name]
420+
self.bot.snippets.pop(name)
421+
self.bot.snippets[value] = old_snippet_value
422+
await self.bot.config.update()
423+
424+
embed = discord.Embed(
425+
title="Renamed snippet",
426+
color=self.bot.main_color,
427+
description=f'`{name}` has been renamed to "{value}".',
428+
)
429+
else:
430+
embed = create_not_found_embed(name, self.bot.snippets.keys(), "Snippet")
431+
await ctx.send(embed=embed)
432+
378433
@commands.command(usage="<category> [options]")
379434
@checks.has_permissions(PermissionLevel.MODERATOR)
380435
@checks.thread_only()

cogs/utility.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,58 @@ async def alias_edit(self, ctx, name: str.lower, *, value):
11881188
embed = await self.make_alias(name, value, "Edited")
11891189
return await ctx.send(embed=embed)
11901190

1191+
@alias.command(name="rename")
1192+
@checks.has_permissions(PermissionLevel.MODERATOR)
1193+
async def alias_rename(self, ctx, name: str.lower, *, value):
1194+
"""
1195+
Rename an alias.
1196+
"""
1197+
if name not in self.bot.aliases:
1198+
embed = utils.create_not_found_embed(name, self.bot.aliases.keys(), "Alias")
1199+
return await ctx.send(embed=embed)
1200+
1201+
embed = None
1202+
if self.bot.get_command(value):
1203+
embed = discord.Embed(
1204+
title="Error",
1205+
color=self.bot.error_color,
1206+
description=f"A command with the same name already exists: `{value}`.",
1207+
)
1208+
1209+
elif value in self.bot.aliases:
1210+
embed = discord.Embed(
1211+
title="Error",
1212+
color=self.bot.error_color,
1213+
description=f"Another alias with the same name already exists: `{value}`.",
1214+
)
1215+
1216+
elif value in self.bot.snippets:
1217+
embed = discord.Embed(
1218+
title="Error",
1219+
color=self.bot.error_color,
1220+
description=f"A snippet with the same name already exists: `{value}`.",
1221+
)
1222+
1223+
elif len(value) > 120:
1224+
embed = discord.Embed(
1225+
title="Error",
1226+
color=self.bot.error_color,
1227+
description="Alias names cannot be longer than 120 characters.",
1228+
)
1229+
1230+
if embed is None:
1231+
old_alias_value = self.bot.aliases[name]
1232+
self.bot.aliases.pop(name)
1233+
self.bot.aliases[value] = old_alias_value
1234+
await self.bot.config.update()
1235+
1236+
embed = discord.Embed(
1237+
title="Alias renamed",
1238+
color=self.bot.main_color,
1239+
description=f'`{name}` has been renamed to "{value}".',
1240+
)
1241+
return await ctx.send(embed=embed)
1242+
11911243
@commands.group(aliases=["perms"], invoke_without_command=True)
11921244
@checks.has_permissions(PermissionLevel.OWNER)
11931245
async def permissions(self, ctx):

0 commit comments

Comments
 (0)