From 08c861ef02277ef7f358eff848ac3d4610c7b05f Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Thu, 14 Apr 2016 00:40:15 -0400 Subject: [PATCH 1/3] add in aliaslist command - not putting it in group so anyone can use it --- cogs/alias.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cogs/alias.py b/cogs/alias.py index 40339db88..26332b79b 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -78,6 +78,20 @@ class Alias: 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): + 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) + message = "```\n" + message += "\t{}\n".format(alias) + if len(message) > 4: + message += "```" + await self.bot.say(message) + async def check_aliases(self, message): if message.author.id == self.bot.user.id or \ len(message.content) < 2 or message.channel.is_private: @@ -98,12 +112,14 @@ class Alias: def part_of_existing_command(self, alias, server): '''Command or alias''' for command in self.bot.commands: - if alias.startswith(command): + if alias.lower() == command: return True if server not in self.aliases: return False - if alias.split(" ")[0] in self.aliases[server]: - return True + for curr_alias in self.aliases[server]: + if alias.split(" ")[0] == curr_alias \ + and alias != curr_alias: + return True return False def remove_old(self): From f03b44d57a8a2ca0dd70f56cfbc4b39cdbdd4c8e Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Thu, 14 Apr 2016 01:12:13 -0400 Subject: [PATCH 2/3] Stop doing multi word, more hassle than it's worth and its pretty much indeterminant. Added better recognition and delete all old multi word aliases --- cogs/alias.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/cogs/alias.py b/cogs/alias.py index 26332b79b..5b8f6210d 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -24,6 +24,11 @@ class Alias: Example: !alias add test flip @Twentysix""" server = ctx.message.server + 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" + " aliases. It sucks, I know, deal with it.") + return if self.part_of_existing_command(command, server.id): await self.bot.say('I can\'t safely add an alias that starts with ' 'an existing command or alias. Sry <3') @@ -102,32 +107,32 @@ class Alias: prefix = self.get_prefix(msg) if prefix and server.id in self.aliases: - for alias in self.aliases[server.id]: - if msg[len(prefix):].startswith(alias): - 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) + if self.first_word(msg[len(prefix):]) in self.aliases[server.id]: + alias = self.first_word(msg[len(prefix):]) + 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) def part_of_existing_command(self, alias, server): '''Command or alias''' for command in self.bot.commands: if alias.lower() == command: return True - if server not in self.aliases: - return False - for curr_alias in self.aliases[server]: - if alias.split(" ")[0] == curr_alias \ - and alias != curr_alias: - return True return False def remove_old(self): for sid in self.aliases: + to_delete = [] for aliasname, alias in self.aliases[sid].items(): + 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: + del self.aliases[sid][alias] fileIO("data/alias/aliases.json", "save", self.aliases) def first_word(self, msg): From 4ad66d82d0307609a9ee2cef463ecfd38b0513c7 Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Thu, 14 Apr 2016 01:19:40 -0400 Subject: [PATCH 3/3] higher chance of equality comparison with existing commands --- cogs/alias.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/alias.py b/cogs/alias.py index 5b8f6210d..cd00f9126 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -117,7 +117,7 @@ class Alias: def part_of_existing_command(self, alias, server): '''Command or alias''' for command in self.bot.commands: - if alias.lower() == command: + if alias.lower() == command.lower(): return True return False