简体   繁体   English

使用 Twisted Python IRC 框架列出 IRC 频道中的用户

[英]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 the channel using Twisted Python.我正在尝试编写一个 function,它将使用 Twisted Python 将 IRC 通道中的缺口列表打印到通道。 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.我已经阅读了 API 文档,我只在这个网站上看到了一个与我的问题类似的问题,但它并没有真正回答我的问题。 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.如果我知道如何获取用户列表(或 Twisted 识别的任何用户列表),我可以简单地使用 for 循环迭代列表,但我不知道如何获取此列表。

The linked example you seem to think is the same, uses WHO , different command, different purpose.您似乎认为的链接示例是相同的,使用WHO ,不同的命令,不同的目的。 The correct way is to use NAMES .正确的方法是使用NAMES

Extended IRCClient to support a names command.扩展 IRCClient 以支持名称命令。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM