Skip to content

Instantly share code, notes, and snippets.

@chrisforbes
Created August 29, 2011 03:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisforbes/1177736 to your computer and use it in GitHub Desktop.
Save chrisforbes/1177736 to your computer and use it in GitHub Desktop.
Really simple IRC bot using Twisted
#!/usr/bin/env python2
"""A really simple IRC bot."""
import sys
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
class Bot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
self.join(self.factory.channel)
print "Signed on as %s." % self.nickname
def joined(self, channel):
print "Joined %s." % channel
def privmsg(self, user, channel, msg):
print msg
class BotFactory(protocol.ClientFactory):
protocol = Bot
def __init__(self, channel, nickname='twistedbot'):
self.channel = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
print "Connection lost. Reason: %s" % reason
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Connection failed. Reason: %s" % reason
if __name__ == "__main__":
chan = sys.argv[1]
reactor.connectTCP('irc.freenode.net', 6667, BotFactory('#' + chan))
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment