mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +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,18 +25,17 @@ 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]:
|
||||||
func(irc, source, cmd_args)
|
try:
|
||||||
except Exception as e:
|
func(irc, source, cmd_args)
|
||||||
log.exception('Unhandled exception caught in command %r', cmd)
|
except Exception as e:
|
||||||
utils.msg(irc, source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
|
log.exception('Unhandled exception caught in command %r', cmd)
|
||||||
return
|
utils.msg(irc, source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
|
||||||
|
return
|
||||||
utils.add_hook(handle_commands, 'PRIVMSG')
|
utils.add_hook(handle_commands, 'PRIVMSG')
|
||||||
|
|
||||||
# Handle WHOIS queries, for IRCds that send them across servers (charybdis, UnrealIRCd; NOT InspIRCd).
|
# Handle WHOIS queries, for IRCds that send them across servers (charybdis, UnrealIRCd; NOT InspIRCd).
|
||||||
|
@ -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:
|
||||||
doc = func.__doc__
|
funcs = world.bot_commands[command]
|
||||||
if doc:
|
if len(funcs) > 1:
|
||||||
lines = doc.split('\n')
|
utils.msg(irc, source, 'The following \x02%s\x02 plugins bind to the \x02%s\x02 command: %s'
|
||||||
# Bold the first line, which usually just tells you what
|
% (len(funcs), command, ', '.join([func.__module__ for func in funcs])))
|
||||||
# arguments the command takes.
|
for func in funcs:
|
||||||
lines[0] = '\x02%s %s\x02' % (command, lines[0])
|
doc = func.__doc__
|
||||||
for line in lines:
|
mod = func.__module__
|
||||||
utils.msg(irc, source, line.strip())
|
if doc:
|
||||||
else:
|
lines = doc.split('\n')
|
||||||
utils.msg(irc, source, 'Error: Command %r doesn\'t offer any help.' % command)
|
# Bold the first line, which usually just tells you what
|
||||||
return
|
# arguments the command takes.
|
||||||
|
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 (from plugin %r) "
|
||||||
|
"doesn't offer any help." % (command, mod))
|
||||||
|
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