Created
May 10, 2013 17:12
-
-
Save ryonsherman/5555844 to your computer and use it in GitHub Desktop.
Basic IRC bot boilerplate.
This file contains 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
import time | |
from twisted.words.protocols import irc | |
from twisted.internet import reactor, protocol, ssl | |
NETWORK = 'irc.network.com' | |
PORT = 6697 | |
CHANNEL = 'channel' | |
NICKNAME = 'bot' | |
class IRCClient(irc.IRCClient): | |
nickname = NICKNAME | |
def connectionMade(self): | |
irc.IRCClient.connectionMade(self) | |
print("[connected at %s]" % time.asctime(time.localtime(time.time()))) | |
def connectionLost(self, reason): | |
irc.IRCClient.connectionLost(self, reason) | |
print("[disconnected at %s]" % time.asctime(time.localtime(time.time()))) | |
def signedOn(self): | |
self.join(self.factory.channel) | |
def joined(self, channel): | |
print("[Joined %s Channel]" % channel) | |
def privmsg(self, user, channel, msg): | |
user = user.split('!', 1)[0] | |
print("<%s> %s" % (user, msg)) | |
# Check to see if they're sending me a private message | |
if channel == self.nickname: | |
msg = "It isn't nice to whisper! Play nice with the group." | |
self.msg(user, msg) | |
return | |
# Otherwise check to see if it is a message directed at me | |
if msg.startswith(self.nickname + ":"): | |
msg = "%s: I am a spam bot" % user | |
self.msg(channel, msg) | |
print("<%s> %s" % (self.nickname, msg)) | |
class ClientFactory(protocol.ClientFactory): | |
channel = CHANNEL | |
def buildProtocol(self, addr): | |
protocol = IRCClient() | |
protocol.factory = self | |
return protocol | |
def clientConnectionLost(self, connector, reason): | |
print "connection lost, reconnecting" | |
connector.connect() | |
def clientConnectionFailed(self, connector, reason): | |
print "connection failed:", reason | |
reactor.stop() | |
if __name__ == '__main__': | |
reactor.connectSSL(NETWORK, PORT, ClientFactory(), ssl.ClientContextFactory()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment