My favorites | Sign in
Project Home Downloads Issues Source
Project Information
Members

Module providing an easy interface to create IRC bots in python.

Uses regexes in combination with an event-like system to handle messages, and abstracts many IRC commands. Includes flood control and easy parallelism, enabled by default.

Although pythonircbot is designed for IRC bots, it is (in most cases) flexible enough to serve as a general IRC library.

Documentation

Released under GNU Affero General Public License.

Example code of a simple echo bot:

import pythonircbot

myBot = pythonircbot.Bot("MyBot")
myBot.connect('irc.freenode.net')

def echo(msg, channel, nick, client, msgMatch):
    myBot.sendMsg(channel, msg)

myBot.addMsgHandler(echo)

myBot.waitForDisconnect()

Because all handlers run in seperate threads, creating an echo bot that would echo after 2 seconds is as easy as adding time.sleep(2) at the start of the echo-function. Also the main thread won't terminate with an exception in a handler thread. So if something goes wrong, your bot won't crash.

Constraints can be added to make a handler only react if it matches a certain regex. Constraints can be defined for the message, channel, nick and client. The Match-object of the message-constraint matched on the message will be returned as the fifth argument to the handler.

In the next example we create a bot that reacts to "!google <searchterm>", and returns the Google url for this search query to the same channel, using a message-constraint and the Match-object.

import pythonircbot
import urllib

myBot = pythonircbot.Bot("MyBot")
myBot.connect('irc.freenode.net')

def search(msg, channel, nick, client, msgMatch):
    searchterm = msgMatch.group(1) # Gets what matched the first parenthesized group.
    searchurl = "https://encrypted.google.com/search?%s" % urllib.urlencode({'q': searchterm })
    myBot.sendMsg(channel, "Google search for \"%s\": %s" % (searchterm, searchurl))

myBot.addMsgHandler(search, '^!google (.*)$')

myBot.waitForDisconnect()
Powered by Google Project Hosting