Created
April 10, 2011 04:43
-
-
Save EntityReborn/912050 to your computer and use it in GitHub Desktop.
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
from twisted.words.protocols import irc | |
from twisted.internet import protocol, reactor | |
import traceback | |
class Bot(irc.IRCClient): | |
nickname = "SocPuppet" | |
def try_except(self, func, *args): | |
try: | |
return func(*args) | |
except Exception: | |
traceback.print_exc(5) | |
def connectionMade(self): | |
irc.IRCClient.connectionMade(self) | |
print "Connected" | |
def connectionLost(self, reason): | |
irc.IRCClient.connectionLost(self, reason) | |
print "Disconnected" | |
def signedOn(self): | |
"""Called when bot has succesfully signed on to server.""" | |
self.join(self.factory.channel) | |
def joined(self, channel): | |
"""This will get called when the bot joins the channel.""" | |
print "I have joined %s" % channel | |
def handleCommand(self, command, prefix, params): | |
for func in self.manager.raw["*"]: | |
self.try_except(func, self, command, prefix, params) | |
if command in self.manager.raw: | |
for func in self.manager.raw[command]: | |
cont = self.try_except( | |
func, self, command, prefix, params) | |
if cont == False: | |
return | |
elif cont == "skip": | |
break | |
# implicit: if None, keep going | |
irc.IRCClient.handleCommand(self, command, prefix, params) | |
def privmsg(self, user, channel, msg): | |
"""This will get called when the bot receives a message.""" | |
nick = user.split('!')[0] | |
# Check to see if they're sending me a private message | |
if channel == self.nickname: | |
msg = "I don't take commands by message." | |
self.msg(nick, msg) | |
return | |
# Otherwise check to see if it is a message directed at me | |
if msg.startswith(self.nickname + ":"): | |
self.reply(user,channel,msg[len(self.nickname + ":"):]) | |
return | |
def reply(self, user, channel, msg): | |
msg = msg.strip() | |
reply = "unknown command: %s" % (msg) | |
for trigger, fn in self.manager.triggers.items(): | |
if msg.lower().startswith(trigger.lower()): | |
try: | |
reply = str(fn(self, user, channel, msg)) | |
except Exception, e: | |
reply = "An error occurred. This has been logged." | |
traceback.print_exc(5) | |
self.msg(channel, reply) | |
def say(self, channel, string): | |
self.msg(channel, str(string)) | |
def quit(self, message): | |
irc.IRCClient.quit(self, message) | |
self.factory.quit() | |
class BotFactory(protocol.ReconnectingClientFactory): | |
protocol = Bot | |
def __init__(self, channel, pluginmanager): | |
self.plugman = pluginmanager | |
self.plugman.import_plugins() | |
self.channel = channel | |
self.service = None | |
def buildProtocol(self, addr): | |
p = protocol.ReconnectingClientFactory.buildProtocol(self, addr) | |
p.manager = self.plugman | |
return p | |
def clientConnectionFailed(self, connector, reason): | |
print "connection failed:", reason | |
if self.service: | |
self.service.stopService() | |
reactor.stop() |
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
from bot import BotFactory | |
from twisted.internet import reactor | |
from twisted.application import internet, service | |
from plugin import manager | |
p = manager | |
f = BotFactory("##socialites-bots", p) | |
# this is the important bit | |
application = service.Application("socbot") # create the Application | |
botService = internet.TCPClient("irc.freenode.net", 6667, f) # create the service | |
f.service = botService | |
botService.setServiceParent(application) | |
botService.startService() | |
reactor.addSystemEventTrigger('before', 'shutdown', botService.stopService) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment