Add commands for editing aliases (#5108)

* [Alias] Added alias edit and alias global edit

* Comment and whitespace only changes

* Docstring fix

* Remove more whitespace

* Add `the` before some English purists make a PR for this...

Co-authored-by: npc203 <npc203@users.noreply.github.com>
Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Samuel
2021-09-03 07:20:08 +05:30
committed by GitHub
parent 6d40de8da3
commit cad7f400f9
2 changed files with 105 additions and 3 deletions

View File

@@ -187,9 +187,10 @@ class AliasCache:
return None
async def add_alias(
self, ctx: commands.Context, alias_name: str, command: str, global_: bool = False
) -> AliasEntry:
@staticmethod
def format_command_for_alias(command: str) -> str:
# This was present in add_alias previously
# Made this into a separate method so as to reuse the same code in edit_alias
indices = findall(r"{(\d*)}", command)
if indices:
try:
@@ -206,6 +207,12 @@ class AliasCache:
+ ", ".join(str(i + low) for i in gaps)
)
command = command.format(*(f"{{{i}}}" for i in range(-low, high + low + 1)))
return command
async def add_alias(
self, ctx: commands.Context, alias_name: str, command: str, global_: bool = False
) -> AliasEntry:
command = self.format_command_for_alias(command)
if global_:
alias = AliasEntry(alias_name, command, ctx.author.id, None)
@@ -225,6 +232,32 @@ class AliasCache:
return alias
async def edit_alias(
self, ctx: commands.Context, alias_name: str, command: str, global_: bool = False
) -> bool:
command = self.format_command_for_alias(command)
if global_:
settings = self.config
else:
settings = self.config.guild(ctx.guild)
async with settings.entries() as aliases:
for index, alias in enumerate(aliases):
if alias["name"] == alias_name:
alias_edited = AliasEntry.from_json(alias)
alias_edited.command = command
aliases[index] = alias_edited.to_json()
if self._cache_enabled:
if global_:
self._aliases[None][alias_edited.name] = alias_edited
else:
self._aliases[ctx.guild.id][alias_edited.name] = alias_edited
return True
return False
async def delete_alias(
self, ctx: commands.Context, alias_name: str, global_: bool = False
) -> bool: