3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

Merge branch 'hide-aliases' into devel

This commit is contained in:
James Lu 2017-07-10 22:00:29 -07:00
commit 6437721ec9
6 changed files with 32 additions and 30 deletions

View File

@ -208,9 +208,7 @@ def setacc(irc, source, args):
# Join the Automode bot to the channel if not explicitly told to. # Join the Automode bot to the channel if not explicitly told to.
modebot.join(ircobj, channel) modebot.join(ircobj, channel)
modebot.add_cmd(setacc, 'setaccess') modebot.add_cmd(setacc, aliases=('setaccess', 'set'), featured=True)
modebot.add_cmd(setacc, 'set')
modebot.add_cmd(setacc, featured=True)
def delacc(irc, source, args): def delacc(irc, source, args):
"""<channel/chanpair> <mask> """<channel/chanpair> <mask>
@ -243,9 +241,7 @@ def delacc(irc, source, args):
log.debug("Automode: purging empty channel pair %s/%s", ircobj.name, channel) log.debug("Automode: purging empty channel pair %s/%s", ircobj.name, channel)
del db[ircobj.name+channel] del db[ircobj.name+channel]
modebot.add_cmd(delacc, 'delaccess') modebot.add_cmd(delacc, aliases=('delaccess', 'del'), featured=True)
modebot.add_cmd(delacc, 'del')
modebot.add_cmd(delacc, featured=True)
def listacc(irc, source, args): def listacc(irc, source, args):
"""<channel/chanpair> """<channel/chanpair>
@ -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, "[%s] \x02%s\x02 has modes +\x02%s\x02" % (entrynum, mask, modes), private=True)
reply(irc, "End of Automode entries list.", private=True) reply(irc, "End of Automode entries list.", private=True)
modebot.add_cmd(listacc, featured=True) modebot.add_cmd(listacc, featured=True, aliases=('listacc',))
modebot.add_cmd(listacc, 'listaccess')
def save(irc, source, args): def save(irc, source, args):
"""takes no arguments. """takes no arguments.
@ -304,9 +299,7 @@ def syncacc(irc, source, args):
reply(irc, 'Done.') reply(irc, 'Done.')
modebot.add_cmd(syncacc, featured=True) modebot.add_cmd(syncacc, featured=True, aliases=('sync', 'syncaccess'))
modebot.add_cmd(syncacc, 'sync')
modebot.add_cmd(syncacc, 'syncaccess')
def clearacc(irc, source, args): def clearacc(irc, source, args):
"""<channel> """<channel>
@ -329,6 +322,4 @@ def clearacc(irc, source, args):
else: else:
error(irc, "No Automode access entries exist for \x02%s\x02." % channel) error(irc, "No Automode access entries exist for \x02%s\x02." % channel)
modebot.add_cmd(clearacc, 'clearaccess') modebot.add_cmd(clearacc, aliases=('clearaccess', 'clearacc'), featured=True)
modebot.add_cmd(clearacc, 'clear')
modebot.add_cmd(clearacc, featured=True)

View File

@ -193,7 +193,6 @@ def part(irc, source, args):
irc.reply("Done.") irc.reply("Done.")
irc.call_hooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}]) irc.call_hooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}])
@utils.add_cmd
def msg(irc, source, args): def msg(irc, source, args):
"""[<source>] <target> <text> """[<source>] <target> <text>
@ -239,4 +238,4 @@ def msg(irc, source, args):
irc.message(sourceuid, real_target, text) irc.message(sourceuid, real_target, text)
irc.reply("Done.") irc.reply("Done.")
irc.call_hooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}]) irc.call_hooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}])
utils.add_cmd(msg, 'say') utils.add_cmd(msg, aliases=('say',))

View File

@ -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, # 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). # 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. # irc: The IRC object where the command was called.
# source: The UID/numeric of the calling user. # source: The UID/numeric of the calling user.
# args: A list of command args (excluding the command name) that the command was called with. # 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. # it will send replies into the channel instead of in your PM.
irc.reply(str(n)) irc.reply(str(n))
# You can also bind a command function multiple times, and/or to different command names via a # You can bind a command function to multiple names using the 'aliases' option.
# second argument. utils.add_cmd(randint, "random", aliases=("randint", "getrandint"))
utils.add_cmd(randint, "random")

View File

@ -59,4 +59,3 @@ def greet(irc, source, args):
# 2) Instead of using utils.add_cmd(function, 'name'), bind functions to your ServiceBot instance. # 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'. # You can also use the featured=True argument to display the command's syntax directly in 'list'.
servicebot.add_cmd(greet, featured=True) servicebot.add_cmd(greet, featured=True)
servicebot.add_cmd(greet, 'g')

View File

@ -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)) s = 'You rolled %s: %s (total: %s)' % (args[0], ' '.join([str(x) for x in results]), sum(results))
reply(irc, s) reply(irc, s)
gameclient.add_cmd(dice, 'd') gameclient.add_cmd(dice, aliases=('d'), featured=True)
gameclient.add_cmd(dice, featured=True)
eightball_responses = ["It is certain.", eightball_responses = ["It is certain.",
"It is decidedly so.", "It is decidedly so.",
@ -69,9 +68,7 @@ def eightball(irc, source, args):
Asks the Magic 8-ball a question. Asks the Magic 8-ball a question.
""" """
reply(irc, random.choice(eightball_responses)) reply(irc, random.choice(eightball_responses))
gameclient.add_cmd(eightball, featured=True) gameclient.add_cmd(eightball, featured=True, aliases=('8ball', '8b'))
gameclient.add_cmd(eightball, '8ball')
gameclient.add_cmd(eightball, '8b')
def die(irc=None): def die(irc=None):
utils.unregisterService('games') utils.unregisterService('games')

View File

@ -217,6 +217,9 @@ class ServiceBot():
# List of command names to "feature" # List of command names to "feature"
self.featured_cmds = set() self.featured_cmds = set()
# Maps command aliases to the respective primary commands
self.alias_cmds = {}
if default_help: if default_help:
self.add_cmd(self.help) self.add_cmd(self.help)
@ -337,7 +340,7 @@ class ServiceBot():
log.exception('Unhandled exception caught in command %r', cmd) 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))) 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): def add_cmd(self, func, name=None, featured=False, aliases=None):
"""Binds an IRC command function to the given command name.""" """Binds an IRC command function to the given command name."""
if name is None: if name is None:
name = func.__name__ name = func.__name__
@ -347,6 +350,12 @@ class ServiceBot():
if featured: if featured:
self.featured_cmds.add(name) self.featured_cmds.add(name)
# 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) self.commands[name].append(func)
return func return func
@ -417,7 +426,14 @@ class ServiceBot():
_reply_format(next_line) _reply_format(next_line)
else: else:
_reply("Error: Command %r doesn't offer any help." % command) _reply("Error: Command %r doesn't offer any help." % command)
return
# Regardless of whether help text is available, mention aliases.
if not shortform:
if command in self.alias_cmds:
_reply('Alias for %s.' % self.alias_cmds[command])
aliases = set(alias for alias, primary in self.alias_cmds.items() if primary == command)
if aliases:
_reply('Available aliases: %s' % ', '.join(aliases))
def help(self, irc, source, args): def help(self, irc, source, args):
"""<command> """<command>
@ -449,8 +465,8 @@ class ServiceBot():
except IndexError: except IndexError:
plugin_filter = None plugin_filter = None
# Don't show CTCP handlers in the public command list. # Don't show CTCP handlers or aliases in the public command list.
cmds = sorted(cmd for cmd in self.commands.keys() if '\x01' not in cmd) cmds = sorted(cmd for cmd in self.commands.keys() if '\x01' not in cmd and cmd not in self.alias_cmds)
if plugin_filter is not None: if plugin_filter is not None:
# Filter by plugin, if the option was given. # Filter by plugin, if the option was given.