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

fantasy: support nick triggers (close #134)

This commit is contained in:
James Lu 2015-11-28 20:46:53 -08:00
parent db59134ae7
commit 0d402af17e
2 changed files with 45 additions and 18 deletions

View File

@ -15,9 +15,14 @@ bot:
# Console log verbosity: see https://docs.python.org/3/library/logging.html#logging-levels # Console log verbosity: see https://docs.python.org/3/library/logging.html#logging-levels
loglevel: DEBUG loglevel: DEBUG
# Fantasy command prefix, if the fantasy plugin is loaded. # Sets the fantasy command prefix for calling commands inside channels
# (requires fantasy plugin).
prefix: "." prefix: "."
# Determines whether the bot will reply to commands prefixed with its nick
# (case sensitive and requires the fantasy plugin).
respondtonick: true
login: login:
# PyLink administrative login - Change this, or the service will not start! # PyLink administrative login - Change this, or the service will not start!
user: admin user: admin

View File

@ -8,26 +8,48 @@ from log import log
def handle_fantasy(irc, source, command, args): def handle_fantasy(irc, source, command, args):
"""Fantasy command handler.""" """Fantasy command handler."""
try: try: # First, try to fetch the config-defined prefix.
prefix = irc.botdata["prefix"] prefixes = [irc.botdata["prefix"]]
except KeyError: 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: "<nick>," and "<nick>:"
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 - " log.warning("(%s) Fantasy prefix was not set in configuration - "
"fantasy commands will not work!", irc.name) "fantasy commands will not work!", irc.name)
return return
channel = args['target'] channel = args['target']
text = args['text'] text = args['text']
# Conditions: for prefix in prefixes: # Cycle through the prefixes list we finished with.
# 1) Message target is a channel, # The following conditions must be met for an incoming message for
# 2) Message starts with our fantasy prefix, # fantasy to trigger:
# 3) The main PyLink client is in the channel. # 1) The message target is a channel.
# 4) The sender is NOT a PyLink client (prevents message loops). # 2) The message starts with one of our fantasy prefixes.
if utils.isChannel(channel) and text.startswith(prefix) and \ # 3) The main PyLink client is in the channel where the command was
irc.pseudoclient.uid in irc.channels[channel].users and not \ # called.
utils.isInternalClient(irc, source): # 4) The sender is NOT a PyLink client (this prevents infinite
# Cut off the length of the prefix from the text. # message loops).
text = text[len(prefix):] if utils.isChannel(channel) and text.startswith(prefix) and \
# Set the last called in variable to the channel, so replies (from irc.pseudoclient.uid in irc.channels[channel].users and not \
# supporting plugins) get forwarded to it. utils.isInternalClient(irc, source):
irc.called_by = channel
irc.callCommand(source, text) # Cut off the length of the prefix from the text.
text = text[len(prefix):]
# Set the "place last command was called in" variable to the
# channel in question, so that replies from fantasy-supporting
# plugins get forwarded to it.
irc.called_by = channel
# Finally, call the bot command and break.
irc.callCommand(source, text)
break
utils.add_hook(handle_fantasy, 'PRIVMSG') utils.add_hook(handle_fantasy, 'PRIVMSG')