From e4d42bf551148d9829322b388d790028a6b4e677 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 15 May 2016 11:45:32 -0700 Subject: [PATCH] fantasy: fixes to respondtonick, allow configuring custom per-bot prefixes --- example-conf.yml | 11 ++++++++--- plugins/fantasy.py | 42 ++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/example-conf.yml b/example-conf.yml index af5cd09..0e57b73 100644 --- a/example-conf.yml +++ b/example-conf.yml @@ -13,14 +13,19 @@ bot: # Server description (shown in /links, /whois, etc.) serverdesc: PyLink Server - # Sets the fantasy command prefix for calling commands inside channels - # (requires fantasy plugin). - prefix: "." + # Options below this require the fantasy plugin to be loaded. + + # Sets the default fantasy command prefix for calling commands inside channels + prefix: "&" # Determines whether the bot will reply to commands prefixed with its nick # (case sensitive and requires the fantasy plugin). respondtonick: true + # Custom fantasy command prefixes for other service bots if they are loaded. + prefixes: + games: "@" + login: # PyLink administrative login - Change this, or the service will not start! user: admin diff --git a/plugins/fantasy.py b/plugins/fantasy.py index b849aee..0b3d3bf 100644 --- a/plugins/fantasy.py +++ b/plugins/fantasy.py @@ -14,22 +14,7 @@ def handle_fantasy(irc, source, command, args): # Break if the IRC network isn't ready. return - try: # First, try to fetch the config-defined prefix. - prefixes = [irc.botdata["prefix"]] - except KeyError: # Config option is missing. - prefixes = [] - - if irc.botdata.get("respondtonick"): - # If responding to nick is enabled, add variations of the current nick - # to the prefix list: "," and ":" - nick = irc.pseudoclient.nick - prefixes += [nick+',', nick+':'] - - if not prefixes: - # We finished with an empty prefixes list, meaning fantasy is misconfigured! - log.warning("(%s) Fantasy prefix was not set in configuration - " - "fantasy commands will not work!", irc.name) - return + respondtonick = irc.botdata.get("respondtonick") channel = args['target'] orig_text = args['text'] @@ -44,9 +29,30 @@ def handle_fantasy(irc, source, command, args): # message loops). for botname, sbot in world.services.items(): log.debug('(%s) fantasy: checking bot %s', irc.name, botname) - if sbot.uids.get(irc.name) in irc.channels[channel].users: + servuid = sbot.uids.get(irc.name) + if servuid in irc.channels[channel].users: + + # Try to look up a prefix specific for this bot in + # bot: prefixes: , falling back to the default prefix if not + # specified. + prefixes = [irc.botdata.get('prefixes', {}).get(botname) or + irc.botdata.get('prefix')] + + # If responding to nick is enabled, add variations of the current nick + # to the prefix list: "," and ":" + nick = irc.users[servuid].nick + + if respondtonick: + prefixes += [nick+',', nick+':'] + + if not any(prefixes): + # We finished with an empty prefixes list, meaning fantasy is misconfigured! + log.warning("(%s) Fantasy prefix for bot %s was not set in configuration - " + "fantasy commands will not work!", irc.name, botname) + continue + for prefix in prefixes: # Cycle through the prefixes list we finished with. - if orig_text.startswith(prefix): + if prefix and orig_text.startswith(prefix): # Cut off the length of the prefix from the text. text = orig_text[len(prefix):]