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

发布于 2024-11-19 21:01:36 字数 182 浏览 2 评论 0原文

我正在尝试编写一个函数,使用 Twisted Python 将 IRC 频道中的昵称列表打印到该频道。我该怎么做?我已经阅读了 API 文档,并且只在此网站上看到了一个与我类似的问题,但它并没有真正回答我的问题。如果我知道如何获取用户列表(或者 Twisted 识别它的任何内容),我可以简单地使用 for 循环迭代该列表,但我不知道如何获取该列表。

I am trying to write a function that will print the lists of nicks in an IRC channel to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-11-26 21:01:36

您似乎认为链接的示例是相同的,使用 WHO,不同的命令,不同的目的。正确的方法是使用NAMES

扩展 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]

例子:

def got_names(nicklist):
    log.msg(nicklist)
self.names("#some channel").addCallback(got_names)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文