views:

122

answers:

2

Hi, I'm trying to build a very simple irc bot for now but it seems like my bot won't join the channel. Can someone point out what's wrong with the following code:

from twisted.internet import reactor, protocol
from twisted.words.protocols import irc

class IRCProtocol(irc.IRCClient):
    nickname = "botnick"

    def connectionMade(self):
        print 'connectionMade!'

    def signedOn(self):
        print 'Signed On to server'
        self.join(self.factory.channels)
        print 'Joined channel'
        self.say(self.factory.channels, "hello", 1024)


class IRCFactory(protocol.ClientFactory):
    protocol = IRCProtocol
    channels = "#testingircbot"

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed because of %s" % reason
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost: %s" % reason
        connector.connect()

if __name__ == "__main__":
    host, port = "irc.freenode.net", 6667
    fact = IRCFactory()
    reactor.connectTCP(host, port, fact)
    reactor.run()

This is the output when I run the script:

connectionMade!
Connection lost: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]
connectionMade!
Connection lost: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]
connectionMade!
Connection lost: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]
+1  A: 

Try enabling logging. One of your methods is probably raising an exception. The exception will be logged, but without logging enabled you'll never see it:

from sys import stdout
from twisted.python.log import startLogging
startLogging(stdout)
Jean-Paul Calderone
+1  A: 

One of the problems with your code is that you are overriding connectionMade without calling the IRCClient implementation. IRCClient.connectionMade performs several important tasks, such as "registering" with the server (providing a nickname etc.) which is necessary before the server will accept any further commands. In fact, signedOn will never be called under these circumstances; the server is probably waiting for your bot to register, and then disconnecting it after a configured timeout when the registration never occurs.

(Note that the "registration" I refer to above is not NickServ-style registration; this refers to the process of sending USER and NICK commands upon connection to the IRC server.(

Also, your code calls self.join() to join a channel, and then immediately attempts to send a message to that channel. While this may work, it is not guaranteed to do so; instead, you should override joined in order to have your code run once the bot has actually joined the channel. You can then send your "hello" message (or do something else).

mithrandi