Twitchbot Timesout

Hey there! I"m trying to create a Twitch Plays Bot using Python.

I connect with this method (I have my oauth setup, don’t worry)

def twitch_connect(self, user, key):
        self.user = user;
        self.oauth= key;
        print("Connecting to twitch.tv");
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
        s.settimeout(0.6);
        connect_host = "irc.twitch.tv";
        connect_port = 6667;
        try:
            s.connect((connect_host, connect_port));
        except:
            print("Failed to connect to twitch");
            sys.exit();
        print("Connected to twitch");
        print("Sending our details to twitch...");
        s.send(('USER %s\r\n' % user +
                'PASS %s\r\n' % key +
                'NICK %s\r\n' % user).encode('utf-8'));
 
        if not self.twitch_login_status(s.recv(1024)):
            print("... and they didn't accept our details");
            sys.exit();
        else:
            print("... they accepted our details");
            print("Connected to twitch.tv!")
            self.s = s;
            s.send(('JOIN #%s\r\n' % user).encode('utf-8'))
            s.recv(1024);

But then receiving a message will always ALWAYS time out, from the begginig

def twitch_recieve_messages(self, amount=1024):
        data = None
        try: data = self.s.recv(1024);
        except:
            print("FUCK!");
            return False;
 
        if not data:
            print("Lost connection to Twitch, attempting to reconnect...");
            self.twitch_connect(self.user, self.oauth);
            return None

What is going on?

Your snippet doesn’t show your PING/PONG event which is a normal cause for timeouts.

Hey!

I watched some more examples on this, and added the PING/PONG line:

def twitch_recieve_messages(self, amount=1024):
        data = None
        try:
            data = self.s.recv(1024);
        except:
            print("FUCK!");
            return False;
        for line in data.split("\r\n"):
            if line == "":
                continue;
            if "PING :tmi.twitch.tv" in line:
                print(line);
                msgg = "PONG :tmi.twitch.tv\r\n".encode();
                irc.send(msgg);
                print(msgg);
                continue;
        if not data:
            print("Lost connection to Twitch, attempting to reconnect...");
            self.twitch_connect(self.user, self.oauth);
            return None;
        #self.ping(data)
        if self.check_has_message(data):
            return [self.parse_message(line) for line in filter(None, data.split('\r\n'))];

It doesn’t work/

The thing is, it just STARTS timing out, it never works, it’s not after a while that it times out, it just NEVER works, times out from the moment I run the script

It looks like you’re using synchronous plain sockets - and the receive function blocks the thread. I assume that this is the underlying issue as you may be receiving less than 1024 bytes and thus never moving on.

I can’t say for sure, though.

While you can make it work, I would recommend to switch to WebSockets for more reliability and easier multithreading.

You can have a look at GitHub - Marenthyu/twitchChatAndOAuthExample: An Example / Reference Implementation of Twitch Authentication and Chat Message Handling. for an example.

Otherwise: catch the error where you’re currently just printing FUCK and check what that says exactly

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.