From 8760fb92d50d8822838fa1a187ef127ec7c924fa Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Tue, 12 Apr 2016 00:18:42 -0400 Subject: [PATCH 1/3] alias PEP8 --- cogs/alias.py | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/cogs/alias.py b/cogs/alias.py index 5211aed07..32269006e 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -1,4 +1,3 @@ -import discord from discord.ext import commands from .utils.chat_formatting import * from .utils.dataIO import fileIO @@ -6,38 +5,39 @@ from .utils import checks from __main__ import send_cmd_help import os + class Alias: - def __init__(self,bot): + def __init__(self, bot): self.bot = bot - self.aliases = fileIO("data/alias/aliases.json","load") + self.aliases = fileIO("data/alias/aliases.json", "load") @commands.group(pass_context=True) @checks.mod_or_permissions(manage_server=True) - async def alias(self,ctx): + 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) + async def _add_alias(self, ctx, command: str, *, to_execute): """Add an alias for a command Example: !alias add test flip @Twentysix""" server = ctx.message.server - if self.get_prefix(to_execute) == False: + if self.get_prefix(to_execute) is False: to_execute = self.bot.command_prefix[0] + to_execute if server.id not in self.aliases: self.aliases[server.id] = {} - #curr_aliases = self.aliases[server.id] if command not in self.bot.commands: self.aliases[server.id][command] = to_execute - fileIO("data/alias/aliases.json","save",self.aliases) + fileIO("data/alias/aliases.json", "save", self.aliases) await self.bot.say("Alias '{}' added.".format(command)) else: - await self.bot.say("Cannot add '{}' because it's a real bot command.".format(command)) + await self.bot.say("Cannot add '{}' because it's a real bot " + "command.".format(command)) - @alias.command(name="help",pass_context=True) - async def _help_alias(self,ctx,command): + @alias.command(name="help", pass_context=True) + async def _help_alias(self, ctx, command): """Tries to execute help for the base command of the alias""" server = ctx.message.server if server.id in self.aliases: @@ -53,8 +53,8 @@ class Alias: else: await self.bot.say("That alias doesn't exist.") - @alias.command(name="show",pass_context=True) - async def _show_alias(self,ctx,command): + @alias.command(name="show", pass_context=True) + async def _show_alias(self, ctx, command): """Shows what command the alias executes.""" server = ctx.message.server if server.id in self.aliases: @@ -64,17 +64,18 @@ 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) + async def _del_alias(self, ctx, command: str): """Deletes an alias""" 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) + self.aliases[server.id].pop(command, None) + fileIO("data/alias/aliases.json", "save", self.aliases) await self.bot.say("Alias '{}' deleted.".format(command)) - async def check_aliases(self,message): - if message.author.id == self.bot.user.id or len(message.content) < 2 or message.channel.is_private: + async def check_aliases(self, message): + if message.author.id == self.bot.user.id or \ + len(message.content) < 2 or message.channel.is_private: return msg = message.content @@ -91,7 +92,7 @@ class Alias: new_message.content = content await self.bot.process_commands(new_message) - def first_word(self,msg): + def first_word(self, msg): return msg.split(" ")[0] def get_prefix(self, msg): @@ -100,11 +101,13 @@ class Alias: return p return False + def check_folder(): if not os.path.exists("data/alias"): print("Creating data/alias folder...") os.makedirs("data/alias") + def check_file(): aliases = {} @@ -113,9 +116,10 @@ def check_file(): print("Creating default alias's aliases.json...") fileIO(f, "save", aliases) + def setup(bot): check_folder() check_file() n = Alias(bot) bot.add_listener(n.check_aliases, "on_message") - bot.add_cog(n) \ No newline at end of file + bot.add_cog(n) From 74332e2cda238a6f2b1757b793a0522da5d37d30 Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Tue, 12 Apr 2016 01:33:12 -0400 Subject: [PATCH 2/3] Aliases do more, better now --- cogs/alias.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/cogs/alias.py b/cogs/alias.py index 32269006e..2f80c9f5b 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -24,8 +24,13 @@ class Alias: Example: !alias add test flip @Twentysix""" server = ctx.message.server - if self.get_prefix(to_execute) is False: - to_execute = self.bot.command_prefix[0] + to_execute + 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') + return + prefix = self.get_prefix(to_execute) + if prefix is not None: + to_execute = to_execute[len(prefix):] if server.id not in self.aliases: self.aliases[server.id] = {} if command not in self.bot.commands: @@ -83,14 +88,32 @@ class Alias: prefix = self.get_prefix(msg) if prefix and server.id in self.aliases: - aliaslist = self.aliases[server.id] - alias = msg[len(prefix):].split(" ")[0] - args = msg[len(self.first_word(message.content)):] - if alias in aliaslist.keys(): - content = aliaslist[alias] + args - new_message = message - new_message.content = content - await self.bot.process_commands(new_message) + 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) + + def part_of_existing_command(self, alias, server): + '''Command or alias''' + for command in self.bot.commands: + if alias.startswith(command): + return True + if server not in self.aliases: + return False + for aname in self.aliases[server]: + if aname.startswith(alias): + return True + return False + + def remove_old(self): + for sid in self.aliases: + for aliasname, alias in self.aliases[sid].items(): + prefix = self.get_prefix(alias) + if prefix is not None: + self.aliases[sid][aliasname] = alias[len(prefix):] + fileIO("data/alias/aliases.json", "save", self.aliases) def first_word(self, msg): return msg.split(" ")[0] @@ -99,7 +122,7 @@ class Alias: for p in self.bot.command_prefix: if msg.startswith(p): return p - return False + return None def check_folder(): @@ -121,5 +144,6 @@ def setup(bot): check_folder() check_file() n = Alias(bot) + n.remove_old() bot.add_listener(n.check_aliases, "on_message") bot.add_cog(n) From 072788a1cca7d290f57abc849c196df8d4b3e327 Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Tue, 12 Apr 2016 13:57:09 -0400 Subject: [PATCH 3/3] only stop adding alias if the first WORD is already an alias in multi-word alias names --- cogs/alias.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cogs/alias.py b/cogs/alias.py index 2f80c9f5b..40339db88 100644 --- a/cogs/alias.py +++ b/cogs/alias.py @@ -102,9 +102,8 @@ class Alias: return True if server not in self.aliases: return False - for aname in self.aliases[server]: - if aname.startswith(alias): - return True + if alias.split(" ")[0] in self.aliases[server]: + return True return False def remove_old(self):