You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Twitch IRC API获取直播观众列表时on_names未触发的问题

Twitch IRC机器人无法触发353/366事件获取观众列表

我用Twitch IRC API开发机器人,想要在运行时获取当前直播的所有观众,但on_names函数始终无法执行。按预期,机器人连接后应该收到JOIN、353和366消息,但只有on_join被触发(pubmsg、welcome、join事件均正常工作)。

现有代码

import irc.client
import logging  # 补充缺失的导入

def on_connect(connection, event):
    logging.info(f"Connected to {connection.server}")
    connection.join(channel)


def on_join(connection, event):
    logging.info(f"Joined {channel}")
    logging.info(f"event: {event}")


def on_names(connection, event):
    logging.info(f"Received raw message: {event} - Type: {event.type}")

    # Check for NAMES response
    if event.type == "353":  # NAMES reply
        # Format: :tmi.twitch.tv 353 <bot_nick> = <channel> :<names>
        channel = event.arguments[1]
        names = event.arguments[2].split()
        global viewers
        viewers = names
        logging.info(f"Updated list of viewers in {channel}: {viewers}")

    elif event.type == "366":  # End of NAMES reply
        logging.info(f"End of NAMES list for {event.arguments[1]}.")


def run_irc_bot(server, port, nickname, token):
    reactor = irc.client.Reactor()
    factory = irc.connection.Factory(wrapper=ssl.wrap_socket)

    try:
        logging.info("Connecting to chat...")
        connection = reactor.server().connect(server, port, nickname, token, connect_factory=factory)
        logging.info(f"Connected to {server} as {nickname}")
    except irc.client.ServerConnectionError as e:
        logging.error(f"Could not connect to server: {e}")
        return

    connection.add_global_handler('welcome', on_connect)
    connection.add_global_handler('join', on_join)
    connection.add_global_handler('pubmsg', on_pubmsg)  # 确保on_pubmsg已定义
    connection.add_global_handler(353, on_names)
    connection.add_global_handler(366, on_names)

    try:
        reactor.process_forever()
    except KeyboardInterrupt:
        logging.info("Shutting down...")
    finally:
        connection.close()
        logging.info("Bot disconnected.")

已尝试的绑定方式

我试过以下几种事件绑定写法,但都未能触发on_names:

connection.add_global_handler('names', on_names)

connection.add_global_handler('353', on_names)
connection.add_global_handler('366', on_names)

问题原因与修复方案

  1. 事件类型格式错误
    irc.client库要求数字IRC命令(如353、366)必须以字符串形式绑定处理器,而你当前使用的是整数353/366,库无法识别该事件类型,导致处理器不会被触发。

  2. 修正事件绑定代码
    将整数类型的事件名改为字符串:

    # 替换原来的整数绑定
    connection.add_global_handler('353', on_names)
    connection.add_global_handler('366', on_names)
    
  3. 主动触发NAMES请求
    部分情况下Twitch不会自动推送NAMES列表,可在on_join中主动发送NAMES命令强制获取:

    def on_join(connection, event):
        logging.info(f"Joined {channel}")
        logging.info(f"event: {event}")
        # 主动发送NAMES命令请求观众列表
        connection.names(channel)
    
  4. 基础检查

    • 确认频道名称格式正确:必须以#开头(如#my_live_channel)
    • 验证机器人的token拥有频道聊天的访问权限
    • 确保on_pubmsg函数已正确定义(代码中引用了该函数但未给出实现)

内容的提问来源于stack exchange,提问作者Oliver Conway

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:27:25