mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 09:19:23 +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:
parent
afd6d8c20d
commit
e1e31f64ad
@ -25,13 +25,12 @@ def handle_commands(irc, source, command, args):
|
|||||||
cmd_args = text.split(' ')
|
cmd_args = text.split(' ')
|
||||||
cmd = cmd_args[0].lower()
|
cmd = cmd_args[0].lower()
|
||||||
cmd_args = cmd_args[1:]
|
cmd_args = cmd_args[1:]
|
||||||
try:
|
if cmd not in world.bot_commands:
|
||||||
func = world.bot_commands[cmd]
|
|
||||||
except KeyError:
|
|
||||||
utils.msg(irc, source, 'Error: Unknown command %r.' % cmd)
|
utils.msg(irc, source, 'Error: Unknown command %r.' % cmd)
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
log.info('(%s) Calling command %r for %s', irc.name, cmd, utils.getHostmask(irc, source))
|
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)
|
func(irc, source, cmd_args)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception('Unhandled exception caught in command %r', cmd)
|
log.exception('Unhandled exception caught in command %r', cmd)
|
||||||
|
@ -63,20 +63,26 @@ def help(irc, source, args):
|
|||||||
except IndexError: # No argument given, just return 'list' output
|
except IndexError: # No argument given, just return 'list' output
|
||||||
listcommands(irc, source, args)
|
listcommands(irc, source, args)
|
||||||
return
|
return
|
||||||
try:
|
if command not in world.bot_commands:
|
||||||
func = world.bot_commands[command]
|
|
||||||
except KeyError:
|
|
||||||
utils.msg(irc, source, 'Error: Unknown command %r.' % command)
|
utils.msg(irc, source, 'Error: Unknown command %r.' % command)
|
||||||
return
|
return
|
||||||
else:
|
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__
|
doc = func.__doc__
|
||||||
|
mod = func.__module__
|
||||||
if doc:
|
if doc:
|
||||||
lines = doc.split('\n')
|
lines = doc.split('\n')
|
||||||
# Bold the first line, which usually just tells you what
|
# Bold the first line, which usually just tells you what
|
||||||
# arguments the command takes.
|
# 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:
|
for line in lines:
|
||||||
utils.msg(irc, source, line.strip())
|
utils.msg(irc, source, line.strip())
|
||||||
else:
|
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
|
return
|
||||||
|
|
||||||
|
2
utils.py
2
utils.py
@ -108,7 +108,7 @@ def add_cmd(func, name=None):
|
|||||||
if name is None:
|
if name is None:
|
||||||
name = func.__name__
|
name = func.__name__
|
||||||
name = name.lower()
|
name = name.lower()
|
||||||
world.bot_commands[name] = func
|
world.bot_commands[name].append(func)
|
||||||
|
|
||||||
def add_hook(func, command):
|
def add_hook(func, command):
|
||||||
"""Add a hook <func> for command <command>."""
|
"""Add a hook <func> for command <command>."""
|
||||||
|
2
world.py
2
world.py
@ -9,7 +9,7 @@ testing = True
|
|||||||
|
|
||||||
global bot_commands, command_hooks
|
global bot_commands, command_hooks
|
||||||
# This should be a mapping of command names to functions
|
# This should be a mapping of command names to functions
|
||||||
bot_commands = {}
|
bot_commands = defaultdict(list)
|
||||||
command_hooks = defaultdict(list)
|
command_hooks = defaultdict(list)
|
||||||
networkobjects = {}
|
networkobjects = {}
|
||||||
schedulers = {}
|
schedulers = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user