I’m making an IRC bot which will log the hostname of a user that just joined the channel, and then log it to a file. The method I’m trying to do this in, is perform a whois command, and eventually it will seperated the hostname, then resolve it to an IP, and also log that.
I’m fairly new to both Python and Twisted, and this is the part of my code that is supposed to log the hostname of the user who just joined (or just log the whois for now):
def userJoined(self, user, channel):
self.logger.log("%s" % (self.whois(user)))
However, when I check the logs, it writes None. Does anybody know what’s wrong with the code, and how to fix it? Thanks.
The
IRCClient.whoismethod always returnsNone. So what you’re seeing is exactly what I would expect from this code. 🙂IRCClient.whoissends a WHOIS command to the server. When it returns, the result is not known because the server has not sent it yet (it very likely has not yet even received the request).In order to get the data in the response, you need to override a few methods on your
IRCClientsubclass.The way a lot of information from the IRC server is exposed by
IRCClientis viairc_-prefixed callback methods. For example, one of the several responses to a WHOIS IRC command, as documented by the IRC RFC, has the mnemonicRPL_WHOISCHANNELS. To get this response, you would override theirc_RPL_WHOISCHANNELSmethod. When the client receives this response from the server, the method is called with the parameters of the response.See also this related question for more details about the
irc_callbacks.Consult the IRC RFC for the list of all the responses you should expect (though various IRC servers may give you more or less). Then override the necessary methods.
Unfortunately, this is not nearly as convenient as a
whoismethod which simply returns the user data, but it is what is necessary to get the information withIRCClientin its current form.