From bf24bac9c979c4349e35098092a6eda71f5bfb59 Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 10 Jul 2017 21:59:29 -0700 Subject: [PATCH] ServiceBot: replace 'alias' option with 'aliases' & condense multiple add_cmds calls into one --- plugins/automode.py | 19 +++++-------------- plugins/bots.py | 3 +-- plugins/example.py | 8 ++++---- plugins/example_service.py | 2 -- plugins/games.py | 7 ++----- utils.py | 14 ++++++++------ 6 files changed, 20 insertions(+), 33 deletions(-) diff --git a/plugins/automode.py b/plugins/automode.py index 5bf41b5..19e0e57 100644 --- a/plugins/automode.py +++ b/plugins/automode.py @@ -208,9 +208,7 @@ def setacc(irc, source, args): # Join the Automode bot to the channel if not explicitly told to. modebot.join(ircobj, channel) -modebot.add_cmd(setacc, 'setaccess', alias='setacc') -modebot.add_cmd(setacc, 'set', alias='setacc') -modebot.add_cmd(setacc, featured=True) +modebot.add_cmd(setacc, aliases=('setaccess', 'set'), featured=True) def delacc(irc, source, args): """ @@ -243,9 +241,7 @@ def delacc(irc, source, args): log.debug("Automode: purging empty channel pair %s/%s", ircobj.name, channel) del db[ircobj.name+channel] -modebot.add_cmd(delacc, 'delaccess', alias='delacc') -modebot.add_cmd(delacc, 'del', alias='delacc') -modebot.add_cmd(delacc, featured=True) +modebot.add_cmd(delacc, aliases=('delaccess', 'del'), featured=True) def listacc(irc, source, args): """ @@ -273,8 +269,7 @@ def listacc(irc, source, args): reply(irc, "[%s] \x02%s\x02 has modes +\x02%s\x02" % (entrynum, mask, modes), private=True) reply(irc, "End of Automode entries list.", private=True) -modebot.add_cmd(listacc, featured=True) -modebot.add_cmd(listacc, 'listaccess', alias='listacc') +modebot.add_cmd(listacc, featured=True, aliases=('listacc',)) def save(irc, source, args): """takes no arguments. @@ -304,9 +299,7 @@ def syncacc(irc, source, args): reply(irc, 'Done.') -modebot.add_cmd(syncacc, featured=True) -modebot.add_cmd(syncacc, 'sync', alias='syncacc') -modebot.add_cmd(syncacc, 'syncaccess', alias='syncacc') +modebot.add_cmd(syncacc, featured=True, aliases=('sync', 'syncaccess')) def clearacc(irc, source, args): """ @@ -329,6 +322,4 @@ def clearacc(irc, source, args): else: error(irc, "No Automode access entries exist for \x02%s\x02." % channel) -modebot.add_cmd(clearacc, 'clearaccess', alias='clearacc') -modebot.add_cmd(clearacc, 'clear', alias='clearacc') -modebot.add_cmd(clearacc, featured=True) +modebot.add_cmd(clearacc, aliases=('clearaccess', 'clearacc'), featured=True) diff --git a/plugins/bots.py b/plugins/bots.py index b77f415..21d2256 100644 --- a/plugins/bots.py +++ b/plugins/bots.py @@ -193,7 +193,6 @@ def part(irc, source, args): irc.reply("Done.") irc.call_hooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}]) -@utils.add_cmd def msg(irc, source, args): """[] @@ -239,4 +238,4 @@ def msg(irc, source, args): irc.message(sourceuid, real_target, text) irc.reply("Done.") irc.call_hooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}]) -utils.add_cmd(msg, 'say', alias='msg') +utils.add_cmd(msg, aliases=('say',)) diff --git a/plugins/example.py b/plugins/example.py index 5097fff..a5565c8 100644 --- a/plugins/example.py +++ b/plugins/example.py @@ -31,7 +31,8 @@ utils.add_hook(hook_privmsg, 'PRIVMSG') # Example command function. @utils.add_cmd binds it to an IRC command of the same name, # but you can also use a different name by specifying a second 'name' argument (see below). -@utils.add_cmd +#@utils.add_cmd + # irc: The IRC object where the command was called. # source: The UID/numeric of the calling user. # args: A list of command args (excluding the command name) that the command was called with. @@ -64,6 +65,5 @@ def randint(irc, source, args): # it will send replies into the channel instead of in your PM. irc.reply(str(n)) -# You can also bind a command function multiple times, and/or to different command names via a -# second argument. The alias= argument marks it as an alias for the specified primary command. -utils.add_cmd(randint, "random", alias="randint") +# You can bind a command function to multiple names using the 'aliases' option. +utils.add_cmd(randint, "random", aliases=("randint", "getrandint")) diff --git a/plugins/example_service.py b/plugins/example_service.py index 05f7cc8..7782697 100644 --- a/plugins/example_service.py +++ b/plugins/example_service.py @@ -58,6 +58,4 @@ def greet(irc, source, args): # 2) Instead of using utils.add_cmd(function, 'name'), bind functions to your ServiceBot instance. # You can also use the featured=True argument to display the command's syntax directly in 'list'. -# The alias= argument marks the command as an alias for another specified command. servicebot.add_cmd(greet, featured=True) -servicebot.add_cmd(greet, 'g', alias='greet') diff --git a/plugins/games.py b/plugins/games.py index dba0210..bed711a 100644 --- a/plugins/games.py +++ b/plugins/games.py @@ -40,8 +40,7 @@ def dice(irc, source, args): s = 'You rolled %s: %s (total: %s)' % (args[0], ' '.join([str(x) for x in results]), sum(results)) reply(irc, s) -gameclient.add_cmd(dice, 'd', alias='dice') -gameclient.add_cmd(dice, featured=True) +gameclient.add_cmd(dice, aliases=('d'), featured=True) eightball_responses = ["It is certain.", "It is decidedly so.", @@ -69,9 +68,7 @@ def eightball(irc, source, args): Asks the Magic 8-ball a question. """ reply(irc, random.choice(eightball_responses)) -gameclient.add_cmd(eightball, featured=True) -gameclient.add_cmd(eightball, '8ball', alias='eightball') -gameclient.add_cmd(eightball, '8b', alias='eightball') +gameclient.add_cmd(eightball, featured=True, aliases=('8ball', '8b')) def die(irc=None): utils.unregisterService('games') diff --git a/utils.py b/utils.py index a04cfcf..2646ea8 100644 --- a/utils.py +++ b/utils.py @@ -340,7 +340,7 @@ class ServiceBot(): log.exception('Unhandled exception caught in command %r', cmd) self.reply(irc, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e))) - def add_cmd(self, func, name=None, featured=False, alias=None): + def add_cmd(self, func, name=None, featured=False, aliases=None): """Binds an IRC command function to the given command name.""" if name is None: name = func.__name__ @@ -349,10 +349,12 @@ class ServiceBot(): # Mark as a featured command if requested to do so. if featured: self.featured_cmds.add(name) - - # If this is an alias, store primary command - if alias is not None: - self.alias_cmds[name] = alias + + # If this is an alias, store the primary command in the alias_cmds dict + if aliases is not None: + for alias in aliases: + self.add_cmd(func, name=alias) # Bind the alias as well. + self.alias_cmds[alias] = name self.commands[name].append(func) return func @@ -424,7 +426,7 @@ class ServiceBot(): _reply_format(next_line) else: _reply("Error: Command %r doesn't offer any help." % command) - + # Regardless of whether help text is available, mention aliases. if not shortform: if command in self.alias_cmds: