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
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: "."
# Determines whether the bot will reply to commands prefixed with its nick
# (case sensitive and requires the fantasy plugin).
respondtonick: true
login:
# PyLink administrative login - Change this, or the service will not start!
user: admin

View File

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