List users in IRC channel using Twisted Python IRC framework
I am trying to write a function that will print the lists of nicks in an IRC channel to开发者_C百科 the channel using Twisted Python. How do I do this? I have read the API documentation and I have only seen one question similar to mine on this site, but it doesn't really answer my question. If I knew how to get the userlist (or whatever it is Twisted recognizes it as), I could simply iterate the list using a for loop, but I don't know how to get this list.
The linked example you seem to think is the same, uses WHO, different command, different purpose. The correct way is to use NAMES.
Extended IRCClient to support a names command.
from twisted.words.protocols import irc
from twisted.internet import defer
class NamesIRCClient(irc.IRCClient):
def __init__(self, *args, **kwargs):
self._namescallback = {}
def names(self, channel):
channel = channel.lower()
d = defer.Deferred()
if channel not in self._namescallback:
self._namescallback[channel] = ([], [])
self._namescallback[channel][0].append(d)
self.sendLine("NAMES %s" % channel)
return d
def irc_RPL_NAMREPLY(self, prefix, params):
channel = params[2].lower()
nicklist = params[3].split(' ')
if channel not in self._namescallback:
return
n = self._namescallback[channel][1]
n += nicklist
def irc_RPL_ENDOFNAMES(self, prefix, params):
channel = params[1].lower()
if channel not in self._namescallback:
return
callbacks, namelist = self._namescallback[channel]
for cb in callbacks:
cb.callback(namelist)
del self._namescallback[channel]
Example:
def got_names(nicklist):
log.msg(nicklist)
self.names("#some channel").addCallback(got_names)
加载中,请稍侯......
精彩评论