From f255973ad5a94f1503041a2f772a5fbc8b2e744e Mon Sep 17 00:00:00 2001 From: Will Date: Mon, 25 Jul 2016 17:20:06 -0400 Subject: [PATCH] Alias case insensitivity (#303) --- cogs/alias.py | 53 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/cogs/alias.py b/cogs/alias.py index ce94d9825..1537266a9 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -4,6 +4,7 @@ from .utils.dataIO import fileIO from .utils import checks from __main__ import user_allowed, send_cmd_help import os +from copy import deepcopy class Alias: @@ -11,19 +12,20 @@ class Alias: self.bot = bot self.aliases = fileIO("data/alias/aliases.json", "load") - @commands.group(pass_context=True) - @checks.mod_or_permissions(administrator=True) + @commands.group(pass_context=True, no_pm=True) async def alias(self, ctx): """Manage per-server aliases for commands""" if ctx.invoked_subcommand is None: await send_cmd_help(ctx) - @alias.command(name="add", pass_context=True) - async def _add_alias(self, ctx, command: str, *, to_execute): + @alias.command(name="add", pass_context=True, no_pm=True) + @checks.mod_or_permissions(manage_server=True) + async def _add_alias(self, ctx, command, *, to_execute): """Add an alias for a command Example: !alias add test flip @Twentysix""" server = ctx.message.server + command = command.lower() if len(command.split(" ")) != 1: await self.bot.say("I can't safely do multi-word aliases because" " of the fact that I allow arguments to" @@ -46,7 +48,7 @@ class Alias: await self.bot.say("Cannot add '{}' because it's a real bot " "command.".format(command)) - @alias.command(name="help", pass_context=True) + @alias.command(name="help", pass_context=True, no_pm=True) async def _help_alias(self, ctx, command): """Tries to execute help for the base command of the alias""" server = ctx.message.server @@ -63,7 +65,7 @@ class Alias: else: await self.bot.say("That alias doesn't exist.") - @alias.command(name="show", pass_context=True) + @alias.command(name="show", pass_context=True, no_pm=True) async def _show_alias(self, ctx, command): """Shows what command the alias executes.""" server = ctx.message.server @@ -74,28 +76,35 @@ class Alias: else: await self.bot.say("That alias doesn't exist.") - @alias.command(name="del", pass_context=True) - async def _del_alias(self, ctx, command: str): + @alias.command(name="del", pass_context=True, no_pm=True) + @checks.mod_or_permissions(manage_server=True) + async def _del_alias(self, ctx, command): """Deletes an alias""" + command = command.lower() server = ctx.message.server if server.id in self.aliases: self.aliases[server.id].pop(command, None) fileIO("data/alias/aliases.json", "save", self.aliases) await self.bot.say("Alias '{}' deleted.".format(command)) - @commands.command(pass_context=True) - async def aliaslist(self, ctx): + @alias.command(name="list", pass_context=True, no_pm=True) + async def _alias_list(self, ctx): + """Lists aliases available on this server + + Responds in DM""" server = ctx.message.server if server.id in self.aliases: message = "```Alias list:\n" for alias in sorted(self.aliases[server.id]): if len(message) + len(alias) + 3 > 2000: - await self.bot.say(message) + await self.bot.whisper(message) message = "```\n" message += "\t{}\n".format(alias) - if len(message) > 4: + if message != "```Alias list:\n": message += "```" - await self.bot.say(message) + await self.bot.whisper(message) + else: + await self.bot.say("There are no aliases on this server.") async def check_aliases(self, message): if not user_allowed(message): @@ -110,12 +119,13 @@ class Alias: prefix = self.get_prefix(msg) if prefix and server.id in self.aliases: - if self.first_word(msg[len(prefix):]) in self.aliases[server.id]: - alias = self.first_word(msg[len(prefix):]) + alias = self.first_word(msg[len(prefix):]).lower() + if alias in self.aliases[server.id]: new_command = self.aliases[server.id][alias] args = message.content[len(prefix + alias):] - message.content = prefix + new_command + args - await self.bot.process_commands(message) + new_message = deepcopy(message) + new_message.content = prefix + new_command + args + await self.bot.process_commands(new_message) def part_of_existing_command(self, alias, server): '''Command or alias''' @@ -127,15 +137,22 @@ class Alias: def remove_old(self): for sid in self.aliases: to_delete = [] + to_add = [] for aliasname, alias in self.aliases[sid].items(): + lower = aliasname.lower() + if aliasname != lower: + to_delete.append(aliasname) + to_add.append((lower, alias)) if aliasname != self.first_word(aliasname): to_delete.append(aliasname) continue prefix = self.get_prefix(alias) if prefix is not None: self.aliases[sid][aliasname] = alias[len(prefix):] - for alias in to_delete: + for alias in to_delete: # Fixes caps and bad prefixes del self.aliases[sid][alias] + for alias, command in to_add: # For fixing caps + self.aliases[sid][alias] = command fileIO("data/alias/aliases.json", "save", self.aliases) def first_word(self, msg):