我已经使用此处的信息作为指导线编写了一些代码,
使用 Twisted Python IRC 框架列出 IRC 频道中的用户
我可以成功地将 NAMES 列表记录到控制台,但是到目前为止我无法检索它以进一步使用它。以下是与此问题相关的代码摘录:
class Alfred(irc.IRCClient):
""" An IRC bot """
# Voodoo magic for names
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]
# End of voodoo magic
def get_names(self, nicklist):
# Log the output to the log
log.msg(nicklist)
def has_op(self, channel, user):
self.names('#' + channel).addCallback(self.get_names)
def change_mode(self, channel, user, msg):
target = msg[5:-3]
params = [channel, '+o']
self.has_op(channel, user)
self.mode(channel, True, '+o', user=target)
我想要实现的是在 has_op 函数中获取所谓的昵称列表。到目前为止,我一直无法使用试错法来做到这一点,我尝试在合理的地方使用 print 和 return 语句,但这并没有产生任何输出或实例/属性错误。显然,我很茫然,我真的需要一些指导。