Created
August 29, 2011 03:44
-
-
Save chrisforbes/1177736 to your computer and use it in GitHub Desktop.
Really simple IRC bot using Twisted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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