加入数百个 irc 频道,套接字已死抽搐

问题描述

我正在尝试使用我的聊天机器人在 twitch 上加入数百个频道。然而,套接获取连接被远程主机错误强行关闭,我的问题是我不特别知道如何添加多个套接字,所以如果一个套接字死亡,那么我可以重新加入该套接字上的通道并继续,而是关闭客户端和尝试再次加入所有频道。

这是我的代码

import socket
import time
import errno

with open('channels.txt') as f:
    channel_list = f.readlines()

def connect():
    global sock
    sock = socket.socket(socket.AF_INET,socket.soCK_STREAM)
    sock.connect(('irc.chat.twitch.tv',6667))



def sendRaw(data,log:bool=True):
    try:
        if log:
            print(f"sending raw: {data}")
        sock.send(bytes(data + '\r\n','utf-8'))
    except IOError as e:
        if e.errno == errno.EPIPE:
            pass


def joinchannel(channel):
    channel = "#" + channel.lower() if not channel.startswith("#") else channel.lower()
    sendRaw('JOIN ' + channel)


def login_and_join():
    sendRaw('PASS oauth:')  # less likely to leak
    sendRaw('NICK justinfan0987')
    # requests more information
    sendRaw('CAP REQ :twitch.tv/commands')
    sendRaw('CAP REQ :twitch.tv/tags')
    sendRaw('JOIN #turtoise')
    sendRaw('JOIN #nimbot0')
    sendRaw('JOIN #peeandpoob')

    for channel in channel_list:
        joinchannel(channel)

def loop():
    while True:
        time.sleep(0.1)
        try:
            data = sock.recv(4096)
            decoded_data = data.decode("utf-8").split("\r\n")
            if decoded_data is None:
                print("no data")
            print(decoded_data)

        except Exception as e:
            print(e)

def run_channel_joiner():
    connect()
    login_and_join()
    loop()

run_channel_joiner()

这里有一些可以测试的渠道: https://pastebin.com/avn0PVVg

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)