mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 21:19:31 +01:00
fantasy: support nick triggers (close #134)
This commit is contained in:
parent
db59134ae7
commit
0d402af17e
@ -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
|
||||||
|
@ -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.
|
||||||
|
# 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 \
|
if utils.isChannel(channel) and text.startswith(prefix) and \
|
||||||
irc.pseudoclient.uid in irc.channels[channel].users and not \
|
irc.pseudoclient.uid in irc.channels[channel].users and not \
|
||||||
utils.isInternalClient(irc, source):
|
utils.isInternalClient(irc, source):
|
||||||
|
|
||||||
# Cut off the length of the prefix from the text.
|
# Cut off the length of the prefix from the text.
|
||||||
text = text[len(prefix):]
|
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
|
irc.called_by = channel
|
||||||
|
|
||||||
|
# Finally, call the bot command and break.
|
||||||
irc.callCommand(source, text)
|
irc.callCommand(source, text)
|
||||||
|
break
|
||||||
|
|
||||||
utils.add_hook(handle_fantasy, 'PRIVMSG')
|
utils.add_hook(handle_fantasy, 'PRIVMSG')
|
||||||
|
Loading…
Reference in New Issue
Block a user