List users in IRC channel using Twisted Python IRC framework
Asked Answered
P

1

6

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.

Plagio answered 12/7, 2011 at 21:50 Comment(5)
dupe #5305550Heedless
This isn't a dupe, I even mentioned that question you linked in question, because it IS NOT what I am trying to do and is NOT helpful.Plagio
It is in fact a dup of that other question; I'm curious why you think it isn't.Mundy
It isn't a dupe because the other question is not the same and the answer doesn't work. I am talking about an irc BOT that use the IRCClient protocol, so it is different.Plagio
But it is the same. That answer does work for your question, exactly as written. You need to be more specific in your question: what happens when you try that solution? Why doesn't it solve your problem?Mundy
Y
6

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)
Yvonneyvonner answered 15/7, 2011 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.