Last active
March 23, 2016 17:25
-
-
Save danilomac/f6ad4b4b3b9ee2726934 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from twisted.words.protocols import irc | |
from twisted.internet import reactor, protocol, task | |
def decode(txt): | |
try: | |
txt = txt.decode('utf-8') | |
except: | |
txt = txt.decode('latin1') | |
return txt | |
class Bot(irc.IRCClient): | |
nickname = 'basicBot' # <--- Nick do robô | |
adminChannel = '##basicbot' # <--- Coloque aqui o canal onde o aviso deve ser reportado | |
def signedOn(self): | |
"""Called when bot has succesfully signed on to server.""" | |
self.join('##basicbot2,##basicbot') # <--- Coloque aqui os canais em que o robô deve entrar | |
def privmsg(self, user, channel, msg): | |
"""This will get called when the bot receives a message.""" | |
msg = decode(msg) | |
if msg.startswith(u'!admin') and channel != self.adminChannel: | |
resp = user.split('!')[0] + u' chama administradores em ' + channel | |
self.msg(self.adminChannel, resp.encode('utf-8')) | |
class BotFactory(protocol.ClientFactory): | |
"""A new protocol instance will be created each time we connect to the server.""" | |
def buildProtocol(self, addr): | |
p = Bot() | |
p.factory = self | |
return p | |
def clientConnectionLost(self, connector, reason): | |
"""If we get disconnected, reconnect to server.""" | |
connector.connect() | |
def clientConnectionFailed(self, connector, reason): | |
print "connection failed:", reason | |
reactor.stop() | |
if __name__ == '__main__': | |
# create factory protocol and application | |
f = BotFactory() | |
# connect factory to this host and port | |
reactor.connectTCP("irc.freenode.net", 6667, f) | |
# run bot | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment