Choosing a Python IRC library
30Jul14
irc
Looks complex to begin with but ended up being effective enough to not consider other libraries.
Connecting is as simple as the following:
import irc.client client = irc.client.IRC() server = client.server() server.connect('localhost', 6667, 'test')
Then because it’s event driven you call:
client.process_once()
in your event loop to keep communication flowing.
Whenever you wish to perform an action on IRC you use the server object to trigger it, for example:
server.join('#test') server.privmsg('nickserv', 'identify password1')
To respond to events, set event handling functions with server.add_global_handler e.g.
server.add_global_handler("welcome", on_connect)
There’s a list of the possible events in the source for events.py. Here are some common ones:
"error", "join", "kick", "mode", "part", "ping", "privmsg", "privnotice", "pubmsg", # privmsg to channel "pubnotice", # notice to channel "quit", "invite", "pong", "action", "topic", "nick",
An event handler looks like this:
def on_connect(server_connection, event): # event has the following properties: event.arguments event.source event.target event.type server_connection.join('#test')
One unexpected behaviour to note is the library uses ‘pubmsg’ to represent a privmsg to a channel.
Additionally, there are some example scripts that can help you get started.
Filed under: solutions | Leave a Comment
Tags: guide, IRC, python, tutorial