Skip to content

Commit 060c5b0

Browse files
committed
Fixed some issues with alias from last commit
1 parent 1453108 commit 060c5b0

File tree

3 files changed

+62
-52
lines changed

3 files changed

+62
-52
lines changed

bot.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from core import checks
3535
from core.clients import ApiClient, PluginDatabaseClient
3636
from core.config import ConfigManager
37-
from core.utils import human_join, parse_alias
37+
from core.utils import human_join, parse_alias, normalize_alias
3838
from core.models import PermissionLevel, SafeFormatter, getLogger, configure_logging
3939
from core.thread import ThreadManager
4040
from core.time import human_timedelta
@@ -723,32 +723,20 @@ async def get_contexts(self, message, *, cls=commands.Context):
723723
# Check if there is any aliases being called.
724724
alias = self.aliases.get(invoker)
725725
if alias is not None:
726-
aliases = parse_alias(alias)
726+
ctxs = []
727+
aliases = normalize_alias(alias, message.content[len(f"{invoked_prefix}{invoker}") :])
727728
if not aliases:
728729
logger.warning("Alias %s is invalid, removing.", invoker)
729730
self.aliases.pop(invoker)
730-
else:
731-
len_ = len(f"{invoked_prefix}{invoker}")
732-
contents = parse_alias(message.content[len_:])
733-
if not contents:
734-
contents = [message.content[len_:]]
735-
736-
ctxs = []
737-
for alias, content in zip_longest(aliases, contents):
738-
if alias is None:
739-
break
740-
ctx = cls(prefix=self.prefix, view=view, bot=self, message=message)
741-
ctx.thread = await self.threads.find(channel=ctx.channel)
742-
743-
if content is not None:
744-
view = StringView(f"{alias} {content.strip()}")
745-
else:
746-
view = StringView(alias)
747-
ctx.view = view
748-
ctx.invoked_with = view.get_word()
749-
ctx.command = self.all_commands.get(ctx.invoked_with)
750-
ctxs += [ctx]
751-
return ctxs
731+
for alias in aliases:
732+
view = StringView(alias)
733+
ctx = cls(prefix=self.prefix, view=view, bot=self, message=message)
734+
ctx.thread = await self.threads.find(channel=ctx.channel)
735+
ctx.view = view
736+
ctx.invoked_with = view.get_word()
737+
ctx.command = self.all_commands.get(ctx.invoked_with)
738+
ctxs += [ctx]
739+
return ctxs
752740

753741
ctx.invoked_with = invoker
754742
ctx.command = self.all_commands.get(invoker)

cogs/utility.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,42 +1016,43 @@ async def alias_add(self, ctx, name: str.lower, *, value):
10161016

10171017
multiple_alias = len(values) > 1
10181018

1019-
embed = discord.Embed(
1020-
title="Added alias",
1021-
color=self.bot.main_color
1022-
)
1019+
embed = discord.Embed(title="Added alias", color=self.bot.main_color)
10231020

1024-
if multiple_alias:
1021+
if not multiple_alias:
10251022
embed.description = f'`{name}` points to: "{values[0]}".'
10261023
else:
10271024
embed.description = f"`{name}` now points to the following steps:"
10281025

10291026
for i, val in enumerate(values, start=1):
10301027
view = StringView(val)
1031-
linked_command = view.get_word()
1028+
linked_command = view.get_word().lower()
10321029
message = view.read_rest()
10331030

10341031
if not self.bot.get_command(linked_command):
10351032
alias_command = self.bot.aliases.get(linked_command)
10361033
if alias_command is not None:
1037-
save_aliases.append(f"{alias_command} {message}".strip())
1034+
save_aliases.extend(utils.normalize_alias(alias_command, message))
10381035
else:
10391036
embed = discord.Embed(title="Error", color=self.bot.error_color)
10401037

10411038
if multiple_alias:
1042-
embed.description = ("The command you are attempting to point "
1043-
f"to does not exist: `{linked_command}`.")
1039+
embed.description = (
1040+
"The command you are attempting to point "
1041+
f"to does not exist: `{linked_command}`."
1042+
)
10441043
else:
1045-
embed.description = ("The command you are attempting to point "
1046-
f"to n step {i} does not exist: `{linked_command}`.")
1044+
embed.description = (
1045+
"The command you are attempting to point "
1046+
f"to n step {i} does not exist: `{linked_command}`."
1047+
)
10471048

10481049
return await ctx.send(embed=embed)
10491050
else:
10501051
save_aliases.append(val)
10511052

10521053
embed.description += f"\n{i}: {val}"
10531054

1054-
self.bot.aliases[name] = " && ".join(f"\"{a}\"" for a in save_aliases)
1055+
self.bot.aliases[name] = " && ".join(f'"{a}"' for a in save_aliases)
10551056
await self.bot.config.update()
10561057
return await ctx.send(embed=embed)
10571058

@@ -1098,42 +1099,43 @@ async def alias_edit(self, ctx, name: str.lower, *, value):
10981099

10991100
multiple_alias = len(values) > 1
11001101

1101-
embed = discord.Embed(
1102-
title="Edited alias",
1103-
color=self.bot.main_color
1104-
)
1102+
embed = discord.Embed(title="Edited alias", color=self.bot.main_color)
11051103

1106-
if multiple_alias:
1104+
if not multiple_alias:
11071105
embed.description = f'`{name}` points to: "{values[0]}".'
11081106
else:
11091107
embed.description = f"`{name}` now points to the following steps:"
11101108

11111109
for i, val in enumerate(values, start=1):
11121110
view = StringView(val)
1113-
linked_command = view.get_word()
1111+
linked_command = view.get_word().lower()
11141112
message = view.read_rest()
11151113

11161114
if not self.bot.get_command(linked_command):
11171115
alias_command = self.bot.aliases.get(linked_command)
11181116
if alias_command is not None:
1119-
save_aliases.append(f"{alias_command} {message}".strip())
1117+
save_aliases.extend(utils.normalize_alias(alias_command, message))
11201118
else:
11211119
embed = discord.Embed(title="Error", color=self.bot.error_color)
11221120

11231121
if multiple_alias:
1124-
embed.description = ("The command you are attempting to point "
1125-
f"to does not exist: `{linked_command}`.")
1122+
embed.description = (
1123+
"The command you are attempting to point "
1124+
f"to does not exist: `{linked_command}`."
1125+
)
11261126
else:
1127-
embed.description = ("The command you are attempting to point "
1128-
f"to n step {i} does not exist: `{linked_command}`.")
1127+
embed.description = (
1128+
"The command you are attempting to point "
1129+
f"to n step {i} does not exist: `{linked_command}`."
1130+
)
11291131

11301132
return await ctx.send(embed=embed)
11311133
else:
11321134
save_aliases.append(val)
11331135

11341136
embed.description += f"\n{i}: {val}"
11351137

1136-
self.bot.aliases[name] = " && ".join(f"\"{a}\"" for a in save_aliases)
1138+
self.bot.aliases[name] = " && ".join(f'"{a}"' for a in save_aliases)
11371139
await self.bot.config.update()
11381140
return await ctx.send(embed=embed)
11391141

core/utils.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import typing
55
from difflib import get_close_matches
66
from distutils.util import strtobool as _stb # pylint: disable=import-error
7-
from itertools import takewhile
7+
from itertools import takewhile, zip_longest
88
from urllib import parse
99

1010
import discord
@@ -221,9 +221,12 @@ def encode_alias(m):
221221
def decode_alias(m):
222222
return base64.b64decode(m.group(1).encode()).decode()
223223

224-
alias = re.sub(r"(?:(?<=^)(?:\s*(?<!\\)(?:\")\s*)|(?<=&&)(?:\s*(?<!\\)(?:\")\s*))(.+?)"
225-
r"(?:(?:\s*(?<!\\)(?:\")\s*)(?=&&)|(?:\s*(?<!\\)(?:\")\s*)(?=$))",
226-
encode_alias, alias)
224+
alias = re.sub(
225+
r"(?:(?<=^)(?:\s*(?<!\\)(?:\")\s*)|(?<=&&)(?:\s*(?<!\\)(?:\")\s*))(.+?)"
226+
r"(?:(?:\s*(?<!\\)(?:\")\s*)(?=&&)|(?:\s*(?<!\\)(?:\")\s*)(?=$))",
227+
encode_alias,
228+
alias,
229+
)
227230

228231
aliases = []
229232
for alias in re.split(r"\s*&&\s*", alias):
@@ -232,6 +235,23 @@ def decode_alias(m):
232235
return aliases
233236

234237

238+
def normalize_alias(alias, message):
239+
aliases = parse_alias(alias)
240+
contents = parse_alias(message)
241+
242+
final_aliases = []
243+
for alias, content in zip_longest(aliases, contents):
244+
if alias is None:
245+
break
246+
247+
if content:
248+
final_aliases.append(f"{alias} {content}")
249+
else:
250+
final_aliases.append(alias)
251+
252+
return final_aliases
253+
254+
235255
def format_description(i, names):
236256
return "\n".join(
237257
": ".join((str(a + i * 15), b))

0 commit comments

Comments
 (0)