Skip to content

Commit

Permalink
Fixed some issues with alias from last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Taaku18 committed Nov 7, 2019
1 parent 1453108 commit 060c5b0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
36 changes: 12 additions & 24 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from core import checks
from core.clients import ApiClient, PluginDatabaseClient
from core.config import ConfigManager
from core.utils import human_join, parse_alias
from core.utils import human_join, parse_alias, normalize_alias
from core.models import PermissionLevel, SafeFormatter, getLogger, configure_logging
from core.thread import ThreadManager
from core.time import human_timedelta
Expand Down Expand Up @@ -723,32 +723,20 @@ async def get_contexts(self, message, *, cls=commands.Context):
# Check if there is any aliases being called.
alias = self.aliases.get(invoker)
if alias is not None:
aliases = parse_alias(alias)
ctxs = []
aliases = normalize_alias(alias, message.content[len(f"{invoked_prefix}{invoker}") :])
if not aliases:
logger.warning("Alias %s is invalid, removing.", invoker)
self.aliases.pop(invoker)
else:
len_ = len(f"{invoked_prefix}{invoker}")
contents = parse_alias(message.content[len_:])
if not contents:
contents = [message.content[len_:]]

ctxs = []
for alias, content in zip_longest(aliases, contents):
if alias is None:
break
ctx = cls(prefix=self.prefix, view=view, bot=self, message=message)
ctx.thread = await self.threads.find(channel=ctx.channel)

if content is not None:
view = StringView(f"{alias} {content.strip()}")
else:
view = StringView(alias)
ctx.view = view
ctx.invoked_with = view.get_word()
ctx.command = self.all_commands.get(ctx.invoked_with)
ctxs += [ctx]
return ctxs
for alias in aliases:
view = StringView(alias)
ctx = cls(prefix=self.prefix, view=view, bot=self, message=message)
ctx.thread = await self.threads.find(channel=ctx.channel)
ctx.view = view
ctx.invoked_with = view.get_word()
ctx.command = self.all_commands.get(ctx.invoked_with)
ctxs += [ctx]
return ctxs

ctx.invoked_with = invoker
ctx.command = self.all_commands.get(invoker)
Expand Down
50 changes: 26 additions & 24 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,42 +1016,43 @@ async def alias_add(self, ctx, name: str.lower, *, value):

multiple_alias = len(values) > 1

embed = discord.Embed(
title="Added alias",
color=self.bot.main_color
)
embed = discord.Embed(title="Added alias", color=self.bot.main_color)

if multiple_alias:
if not multiple_alias:
embed.description = f'`{name}` points to: "{values[0]}".'
else:
embed.description = f"`{name}` now points to the following steps:"

for i, val in enumerate(values, start=1):
view = StringView(val)
linked_command = view.get_word()
linked_command = view.get_word().lower()
message = view.read_rest()

if not self.bot.get_command(linked_command):
alias_command = self.bot.aliases.get(linked_command)
if alias_command is not None:
save_aliases.append(f"{alias_command} {message}".strip())
save_aliases.extend(utils.normalize_alias(alias_command, message))
else:
embed = discord.Embed(title="Error", color=self.bot.error_color)

if multiple_alias:
embed.description = ("The command you are attempting to point "
f"to does not exist: `{linked_command}`.")
embed.description = (
"The command you are attempting to point "
f"to does not exist: `{linked_command}`."
)
else:
embed.description = ("The command you are attempting to point "
f"to n step {i} does not exist: `{linked_command}`.")
embed.description = (
"The command you are attempting to point "
f"to n step {i} does not exist: `{linked_command}`."
)

return await ctx.send(embed=embed)
else:
save_aliases.append(val)

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

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

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

multiple_alias = len(values) > 1

embed = discord.Embed(
title="Edited alias",
color=self.bot.main_color
)
embed = discord.Embed(title="Edited alias", color=self.bot.main_color)

if multiple_alias:
if not multiple_alias:
embed.description = f'`{name}` points to: "{values[0]}".'
else:
embed.description = f"`{name}` now points to the following steps:"

for i, val in enumerate(values, start=1):
view = StringView(val)
linked_command = view.get_word()
linked_command = view.get_word().lower()
message = view.read_rest()

if not self.bot.get_command(linked_command):
alias_command = self.bot.aliases.get(linked_command)
if alias_command is not None:
save_aliases.append(f"{alias_command} {message}".strip())
save_aliases.extend(utils.normalize_alias(alias_command, message))
else:
embed = discord.Embed(title="Error", color=self.bot.error_color)

if multiple_alias:
embed.description = ("The command you are attempting to point "
f"to does not exist: `{linked_command}`.")
embed.description = (
"The command you are attempting to point "
f"to does not exist: `{linked_command}`."
)
else:
embed.description = ("The command you are attempting to point "
f"to n step {i} does not exist: `{linked_command}`.")
embed.description = (
"The command you are attempting to point "
f"to n step {i} does not exist: `{linked_command}`."
)

return await ctx.send(embed=embed)
else:
save_aliases.append(val)

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

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

Expand Down
28 changes: 24 additions & 4 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
from difflib import get_close_matches
from distutils.util import strtobool as _stb # pylint: disable=import-error
from itertools import takewhile
from itertools import takewhile, zip_longest
from urllib import parse

import discord
Expand Down Expand Up @@ -221,9 +221,12 @@ def encode_alias(m):
def decode_alias(m):
return base64.b64decode(m.group(1).encode()).decode()

alias = re.sub(r"(?:(?<=^)(?:\s*(?<!\\)(?:\")\s*)|(?<=&&)(?:\s*(?<!\\)(?:\")\s*))(.+?)"
r"(?:(?:\s*(?<!\\)(?:\")\s*)(?=&&)|(?:\s*(?<!\\)(?:\")\s*)(?=$))",
encode_alias, alias)
alias = re.sub(
r"(?:(?<=^)(?:\s*(?<!\\)(?:\")\s*)|(?<=&&)(?:\s*(?<!\\)(?:\")\s*))(.+?)"
r"(?:(?:\s*(?<!\\)(?:\")\s*)(?=&&)|(?:\s*(?<!\\)(?:\")\s*)(?=$))",
encode_alias,
alias,
)

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


def normalize_alias(alias, message):
aliases = parse_alias(alias)
contents = parse_alias(message)

final_aliases = []
for alias, content in zip_longest(aliases, contents):
if alias is None:
break

if content:
final_aliases.append(f"{alias} {content}")
else:
final_aliases.append(alias)

return final_aliases


def format_description(i, names):
return "\n".join(
": ".join((str(a + i * 15), b))
Expand Down

0 comments on commit 060c5b0

Please sign in to comment.