2016-05-22 19:59:57 +02:00
|
|
|
# example.py: An example PyLink plugin.
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc import utils
|
|
|
|
from pylinkirc.log import log
|
2015-08-09 09:26:40 +02:00
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
# Example PRIVMSG hook that returns "hi there!" when PyLink's nick is mentioned
|
|
|
|
# in a channel.
|
2015-08-09 10:06:00 +02:00
|
|
|
|
|
|
|
# irc: The IRC object where the hook was called.
|
|
|
|
# source: The UID/numeric of the sender.
|
|
|
|
# command: The true command name where the hook originates. This may or may not be
|
|
|
|
# the same as the name of the hook, depending on context.
|
|
|
|
# args: The hook data (a dict) associated with the command. The available data
|
|
|
|
# keys differ by hook name (see the hooks reference for a list of which can
|
|
|
|
# be used).
|
2015-08-09 09:26:40 +02:00
|
|
|
def hook_privmsg(irc, source, command, args):
|
|
|
|
channel = args['target']
|
|
|
|
text = args['text']
|
2016-05-22 19:59:57 +02:00
|
|
|
|
2015-08-24 21:10:14 +02:00
|
|
|
# irc.pseudoclient stores the IrcUser object of the main PyLink client.
|
|
|
|
# (i.e. the user defined in the bot: section of the config)
|
2015-08-09 09:26:40 +02:00
|
|
|
if utils.isChannel(channel) and irc.pseudoclient.nick in text:
|
2015-09-07 07:40:10 +02:00
|
|
|
irc.msg(channel, 'hi there!')
|
2016-05-22 19:59:57 +02:00
|
|
|
# log.debug, log.info, log.warning, log.error, log.exception (within except: clauses)
|
|
|
|
# and log.critical are supported here.
|
2015-08-09 09:26:40 +02:00
|
|
|
log.info('%s said my name on channel %s (PRIVMSG hook caught)' % (source, channel))
|
2016-05-22 19:59:57 +02:00
|
|
|
|
2015-08-09 09:26:40 +02:00
|
|
|
utils.add_hook(hook_privmsg, 'PRIVMSG')
|
|
|
|
|
2015-08-09 10:06:00 +02:00
|
|
|
|
2015-08-09 09:26:40 +02:00
|
|
|
# Example command function. @utils.add_cmd binds it to an IRC command of the same name,
|
|
|
|
# but you can also use a different name by specifying a second 'name' argument (see below).
|
|
|
|
@utils.add_cmd
|
2015-08-09 10:06:00 +02:00
|
|
|
# irc: The IRC object where the command was called.
|
|
|
|
# source: The UID/numeric of the calling user.
|
|
|
|
# args: A list of command args (excluding the command name) that the command was called with.
|
2015-08-09 09:26:40 +02:00
|
|
|
def randint(irc, source, args):
|
2017-01-21 20:26:39 +01:00
|
|
|
# The 'help' command uses command functions' docstrings as help text, and formats them
|
|
|
|
# in the following manner:
|
|
|
|
# - Any newlines immediately adjacent to text on both sides are replaced with a space. This
|
|
|
|
# means that the first descriptive paragraph ("Returns a random...given.") shows up as one
|
|
|
|
# line, even though it is physically written on two.
|
|
|
|
# - Double line breaks are treated as breaks between two paragraphs, and will be shown
|
|
|
|
# as distinct lines in IRC.
|
2016-05-22 19:59:57 +02:00
|
|
|
|
2017-01-21 20:26:39 +01:00
|
|
|
# Note: you shouldn't make any one paragraph too long, since they may get cut off. Automatic
|
|
|
|
# word-wrap may be added in the future; see https://github.com/GLolol/PyLink/issues/153
|
2016-05-22 19:59:57 +02:00
|
|
|
"""[<min> <max>]
|
2017-01-21 20:26:39 +01:00
|
|
|
|
|
|
|
Returns a random number between <min> and <max>. <min> and <max> default to 1 and 10
|
|
|
|
respectively, if both aren't given.
|
|
|
|
|
|
|
|
Example second paragraph here."""
|
2015-08-09 09:26:40 +02:00
|
|
|
try:
|
|
|
|
rmin = args[0]
|
|
|
|
rmax = args[1]
|
|
|
|
except IndexError:
|
|
|
|
rmin, rmax = 1, 10
|
|
|
|
n = random.randint(rmin, rmax)
|
2015-12-27 00:02:23 +01:00
|
|
|
|
|
|
|
# irc.reply() is intelligent and will automatically reply to the caller in
|
|
|
|
# right context. If fantasy is loaded and you call the command via it,
|
|
|
|
# it will send replies into the channel instead of in your PM.
|
|
|
|
irc.reply(str(n))
|
|
|
|
|
2015-08-24 21:10:14 +02:00
|
|
|
# You can also bind a command function multiple times, and/or to different command names via a
|
|
|
|
# second argument.
|
2015-08-09 09:26:40 +02:00
|
|
|
utils.add_cmd(randint, "random")
|