Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Module HOWTO

re1jo edited this page Sep 25, 2013 · 4 revisions

Basic modules

Commands

Commands start with a cmdchar, which is . by default (for example try as an admin .ping). You can write a command handle by defining a new function command_mycommand. As a result message .mycommand would call the function.

A normal module listens to a specific trigger and responds to it:

def command_helloworld(bot, user, channel, args):
  nick = getNick(user) # parses nick from nick!user@host
  if channel == "#hellochannel":
    bot.say(channel, "%s: Hello, World!" % nick)

Handlers

Messages

You can write a handler for all irc-input (privmsg). This handler will receive all messages typed by irc users, in both queries and in channels.

import logging

log = logging.getlogger("myprivmsghandler")
def handle_privmsg(bot, user, channel, msg):
  log.info("I received a message: %s" % msg)

URLs

There is also handler for urls:

import logging

log = logging.getLogger("myurlhandler")
def handle_url(bot, user, channel, msg):
  log.info("I received a url: %s" % msg)

Repeated tasks

Call a method or function consecutively:

import logging
from twisted.internet import reactor

log = logging.getlogger("motionmachine")

def init(bot):
    """Called when the bot is loaded and on rehash"""
    perpetual_motion_machine(60)

def perpetual_motion_machine(delay):
    """
    This will execute itself every five seconds
    """
    log.info('Hello World!')
    reactor.callLater(delay, perpetual_motion_machine, delay)


perpetual_motion_machine(5)
Clone this wiki locally