[Twisted-Python] irc bot, reacting to: irc_ERR_NOSUCHNICK. after the code where the reactor was started

Glyph Lefkowitz glyph at twistedmatrix.com
Mon Oct 5 15:12:09 MDT 2015


> On Oct 5, 2015, at 2:33 AM, peter <commercials24 at yahoo.de> wrote:
> 
> my bot inherits from irc.IRCClient, when it sends a message to another user who isnt online irc_ERR_NOSUCHNICK(self, prefix, command) is called.
> how can i react to that message, my code is called with:
> 
> 
> class mybot(irc.IRCClient):
> ...
> if __name__ == "__main__":
> 
>    factory = XdccBotFactory( channel, nickname, xdccbot, xdccrequests )
>    reactor.connectTCP( server, 6667, factory )
> 
> 
> 
> in irc_ERR_NOSUCHNICK... i got:
>    error_message = "%s bot is not connected to server" % now.strftime("%Y-%m-%d %H:%M:%S")
>    log.warning(error_message)
> 
> 
> which works.
> but what i really would like to have is:
> 
> 
> if __name__ == "__main__":
> 
>    factory = XdccBotFactory( channel, nickname, xdccbot, xdccrequests )
>    reactor.connectTCP( server, 6667, factory )
> 
>    if "bot not online":
>        "send message to antoher user"
> 
> 
> so basically i need the information that irc_ERR_... was called after the reactor was started.
> how do you do that?

Use Endpoints <https://twistedmatrix.com/documents/15.4.0/core/howto/endpoints.html> so you get a nice `Deferred´ back from connectTCP.
Add a callback to that Deferred.
Add a method to `XdccBotFactory´ that returns a `Deferred´ that fires when its message is successfully sent and acknowledged, and then add a callback to that `Deferred´.

If you were to use inlineCallbacks to inline the callbacks you're adding into one function, it might look something like this:

> @inlineCallbacks
> def main(reactor):
>     factory = XdccBotFactory(channel, nickname, xdccbot, xdccrequests)
>     endpoint = clientFromString(reactor, "...")
>     bot = yield endpoint.connect(factory)
>     online = yield bot.isBotOnline()
>     if online:
>         "send message to another user"
>     else:
>         "ok"


Does this make sense?

-glyph

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20151005/f6666613/attachment-0002.html>


More information about the Twisted-Python mailing list