3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 19:52:53 +01:00

Allow multiple plugins to bind to one command name!

They will get called in a list, one after the other. The "help" command is able to distinguish with this too.
This commit is contained in:
James Lu 2015-08-29 19:24:32 -07:00
parent afd6d8c20d
commit e1e31f64ad
4 changed files with 31 additions and 26 deletions

View File

@ -25,13 +25,12 @@ def handle_commands(irc, source, command, args):
cmd_args = text.split(' ')
cmd = cmd_args[0].lower()
cmd_args = cmd_args[1:]
try:
func = world.bot_commands[cmd]
except KeyError:
if cmd not in world.bot_commands:
utils.msg(irc, source, 'Error: Unknown command %r.' % cmd)
return
try:
log.info('(%s) Calling command %r for %s', irc.name, cmd, utils.getHostmask(irc, source))
for func in world.bot_commands[cmd]:
try:
func(irc, source, cmd_args)
except Exception as e:
log.exception('Unhandled exception caught in command %r', cmd)

View File

@ -63,20 +63,26 @@ def help(irc, source, args):
except IndexError: # No argument given, just return 'list' output
listcommands(irc, source, args)
return
try:
func = world.bot_commands[command]
except KeyError:
if command not in world.bot_commands:
utils.msg(irc, source, 'Error: Unknown command %r.' % command)
return
else:
funcs = world.bot_commands[command]
if len(funcs) > 1:
utils.msg(irc, source, 'The following \x02%s\x02 plugins bind to the \x02%s\x02 command: %s'
% (len(funcs), command, ', '.join([func.__module__ for func in funcs])))
for func in funcs:
doc = func.__doc__
mod = func.__module__
if doc:
lines = doc.split('\n')
# Bold the first line, which usually just tells you what
# arguments the command takes.
lines[0] = '\x02%s %s\x02' % (command, lines[0])
lines[0] = '\x02%s %s\x02 (plugin: %r)' % (command, lines[0], mod)
for line in lines:
utils.msg(irc, source, line.strip())
else:
utils.msg(irc, source, 'Error: Command %r doesn\'t offer any help.' % command)
utils.msg(irc, source, "Error: Command %r (from plugin %r) "
"doesn't offer any help." % (command, mod))
return

View File

@ -108,7 +108,7 @@ def add_cmd(func, name=None):
if name is None:
name = func.__name__
name = name.lower()
world.bot_commands[name] = func
world.bot_commands[name].append(func)
def add_hook(func, command):
"""Add a hook <func> for command <command>."""

View File

@ -9,7 +9,7 @@ testing = True
global bot_commands, command_hooks
# This should be a mapping of command names to functions
bot_commands = {}
bot_commands = defaultdict(list)
command_hooks = defaultdict(list)
networkobjects = {}
schedulers = {}