List of IRC Users Using Twisted Python IRC Infrastructure

I am trying to write a function that will print the lists of nicknames in an IRC channel to a channel using Twisted Python. How can I do it? I read the API documentation and I saw only one question, similar to mine on this site, but actually it does not answer my question. If I knew how to get a list of users (or whatever it is, Twisted would recognize it as), I could just iterate over the list with a for loop, but I don't know how to get this list.

+6
source share
1 answer

A related example that you think is the same is using WHO , another team, another goal. The correct way is to use NAMES .

Advanced IRCClient to support command names.

 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) 
+6
source

Source: https://habr.com/ru/post/892615/


All Articles