From f00f0fd4c0e0cc53be85900154f1303b9607b3f6 Mon Sep 17 00:00:00 2001
From: Romain J <romain.ordi@gmail.com>
Date: Tue, 19 Jan 2021 16:05:20 +0100
Subject: [PATCH] feat(commands|Custom>alias): finish alias interpretation

---
 README.rst                                 |  2 +-
 tuxbot/cogs/Custom/custom.py               | 15 -------
 tuxbot/cogs/Custom/functions/converters.py |  2 +-
 tuxbot/cogs/Custom/locales/en-US.po        | 30 ++++++++++++--
 tuxbot/cogs/Custom/locales/fr-FR.po        |  5 ---
 tuxbot/cogs/Custom/locales/messages.pot    |  5 ---
 tuxbot/core/bot.py                         | 48 +++++++++++++++-------
 7 files changed, 63 insertions(+), 44 deletions(-)

diff --git a/README.rst b/README.rst
index aa1b513..aec380e 100644
--- a/README.rst
+++ b/README.rst
@@ -61,7 +61,7 @@ two commands:
     $ make install
 
 Now, switch your environment to the virtual one by run this single
-command: :bash:`source ~/tuxvenv/bin/activate`
+command: :bash:`source ~/venv/bin/activate`
 
 Configuration
 -------------
diff --git a/tuxbot/cogs/Custom/custom.py b/tuxbot/cogs/Custom/custom.py
index fea5b0c..d3ce57b 100644
--- a/tuxbot/cogs/Custom/custom.py
+++ b/tuxbot/cogs/Custom/custom.py
@@ -99,21 +99,6 @@ class Custom(commands.Cog, name="Custom"):
                 ).format(alias=alias, command=user_aliases.get(alias))
             )
 
-        if command in user_aliases.values():
-            return await ctx.send(
-                _(
-                    "There is already an alias for `{command}` "
-                    "which is `{alias}`",
-                    ctx,
-                    self.bot.config,
-                ).format(
-                    command=command,
-                    alias=list(user_aliases.keys())[
-                        list(user_aliases.values()).index(command)
-                    ],
-                )
-            )
-
         user_aliases[alias] = command
 
         await self._save_alias(ctx, user_aliases)
diff --git a/tuxbot/cogs/Custom/functions/converters.py b/tuxbot/cogs/Custom/functions/converters.py
index 05895c0..6e1cd86 100644
--- a/tuxbot/cogs/Custom/functions/converters.py
+++ b/tuxbot/cogs/Custom/functions/converters.py
@@ -22,7 +22,7 @@ class AliasConvertor(commands.Converter):
         if command_ctx.command is None:
             raise commands.BadArgument(_("Unknown command"))
 
-        if alias_ctx.command is not None:
+        if args[0] != args[1] and alias_ctx.command is not None:
             raise commands.BadArgument(_("Command already exists"))
 
         return argument
diff --git a/tuxbot/cogs/Custom/locales/en-US.po b/tuxbot/cogs/Custom/locales/en-US.po
index 0d5a557..bd85a4a 100644
--- a/tuxbot/cogs/Custom/locales/en-US.po
+++ b/tuxbot/cogs/Custom/locales/en-US.po
@@ -17,11 +17,35 @@ msgstr ""
 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
-#: tuxbot/cogs/Admin/admin.py:50
+#: tuxbot/cogs/Custom/custom.py:69
 #, python-brace-format
-msgid "Locale changed to {lang} successfully"
+msgid "Locale changed for you to {lang} successfully"
 msgstr ""
 
-#: tuxbot/cogs/Admin/admin.py:65
+#: tuxbot/cogs/Custom/custom.py:76
 msgid "List of available locales: "
 msgstr ""
+
+#: tuxbot/cogs/Custom/custom.py:95
+#, python-brace-format
+msgid "The alias `{alias}` is already defined for the command `{command}`"
+msgstr ""
+
+#: tuxbot/cogs/Custom/custom.py:123
+#, python-brace-format
+msgid "The alias `{alias}` for the command `{command}` was successfully created"
+msgstr ""
+
+#: tuxbot/cogs/Custom/functions/converters.py:14
+msgid "Alias must be like `[command] | [alias]`"
+msgstr ""
+
+#: tuxbot/cogs/Custom/functions/converters.py:23
+#, python-brace-format
+msgid "Unknown command"
+msgstr ""
+
+#: tuxbot/cogs/Custom/functions/converters.py:26
+#, python-brace-format
+msgid "Command already exists"
+msgstr ""
diff --git a/tuxbot/cogs/Custom/locales/fr-FR.po b/tuxbot/cogs/Custom/locales/fr-FR.po
index 6f98cde..c30aa77 100644
--- a/tuxbot/cogs/Custom/locales/fr-FR.po
+++ b/tuxbot/cogs/Custom/locales/fr-FR.po
@@ -32,11 +32,6 @@ msgstr "Liste des langues disponibles: "
 msgid "The alias `{alias}` is already defined for the command `{command}`"
 msgstr "L'alias `{alias}` est déjà défini pour la commande `{command}`"
 
-#: tuxbot/cogs/Custom/custom.py:105
-#, python-brace-format
-msgid "There is already an alias for `{command}` which is `{alias}`"
-msgstr "Il existe déjà un alias pour `{command}`, qui est `{alias}`"
-
 #: tuxbot/cogs/Custom/custom.py:123
 #, python-brace-format
 msgid "The alias `{alias}` for the command `{command}` was successfully created"
diff --git a/tuxbot/cogs/Custom/locales/messages.pot b/tuxbot/cogs/Custom/locales/messages.pot
index b914f61..4dc8022 100644
--- a/tuxbot/cogs/Custom/locales/messages.pot
+++ b/tuxbot/cogs/Custom/locales/messages.pot
@@ -31,11 +31,6 @@ msgstr ""
 msgid "The alias `{alias}` is already defined for the command `{command}`"
 msgstr ""
 
-#: tuxbot/cogs/Custom/custom.py:105
-#, python-brace-format
-msgid "There is already an alias for `{command}` which is `{alias}`"
-msgstr ""
-
 #: tuxbot/cogs/Custom/custom.py:123
 #, python-brace-format
 msgid "The alias `{alias}` for the command `{command}` was successfully created"
diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py
index 50b3437..df720bf 100644
--- a/tuxbot/core/bot.py
+++ b/tuxbot/core/bot.py
@@ -104,6 +104,22 @@ class Tux(commands.AutoShardedBot):
         )
         self.session = aiohttp.ClientSession(loop=self.loop)
 
+    async def _is_blacklister(self, message: discord.Message) -> bool:
+        """Check for blacklists."""
+        if message.author.bot:
+            return True
+
+        if (
+            search_for(self.config.Servers, message.guild.id, "blacklisted")
+            or search_for(
+                self.config.Channels, message.channel.id, "blacklisted"
+            )
+            or search_for(self.config.Users, message.author.id, "blacklisted")
+        ):
+            return True
+
+        return False
+
     async def load_packages(self):
         if packages:
             with Progress() as progress:
@@ -216,24 +232,28 @@ class Tux(commands.AutoShardedBot):
         return await super().get_context(message, cls=ContextPlus)
 
     async def process_commands(self, message: discord.Message):
-        """Check for blacklists."""
-        if message.author.bot:
-            return
-
-        if (
-            search_for(self.config.Servers, message.guild.id, "blacklisted")
-            or search_for(
-                self.config.Channels, message.channel.id, "blacklisted"
-            )
-            or search_for(self.config.Users, message.author.id, "blacklisted")
-        ):
-            return
-
         ctx: ContextPlus = await self.get_context(message)
 
         if ctx is None or not ctx.valid:
+            if user_aliases := search_for(
+                self.config.Users, message.author.id, "aliases"
+            ):
+                for alias, command in user_aliases.items():
+                    back_content = message.content
+                    message.content = message.content.replace(
+                        alias, command, 1
+                    )
+
+                    if (
+                        ctx := await self.get_context(message)
+                    ) is None or not ctx.valid:
+                        message.content = back_content
+                    else:
+                        break
+
             self.dispatch("message_without_command", message)
-        else:
+
+        if ctx is not None and ctx.valid:
             if ctx.command in search_for(
                 self.config.Servers, message.guild.id, "disabled_command", []
             ):