Create a gist now

Instantly share code, notes, and snippets.

anonymous / Secret
Created Feb 21, 2013

from twisted.words.protocols import irc
from twisted.internet import protocol, reactor
from twisted.protocols.basic import LineReceiver
from twisted.internet.endpoints import clientFromString, serverFromString
from twisted.protocols import basic
messages = []
class Echo(protocol.Protocol):
def dataReceived(self, data):
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
class IRCBot(irc.IRCClient):
def dataReceived(self, data):
global messages
basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
def nickname(self):
return self.factory.nickname
def signedOn(self):
print "Signed on as %s." % (self.nickname,)
def joined(self, channel):
print "Joined %s." % (channel,)
def privmsg(self, user, channel, msg):
"""This function is called when a message is sent to the bot or to a
channel the bot is in"""
print user, msg
def connectionLost(self, reason):
def say(self, msg):
self.msg(, msg)
class IRCBotFactory(protocol.ClientFactory):
protocol = IRCBot
def __init__(self, channel, nickname): = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s). Reconnecting" % (reason,)
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment