-1

Twitch randomly disconnects my python bot. I googled a lot and found out that this is a common problem. Only solution seems to be an automated reconnect. Tried this, but my knowledge seems to be way too limited to make it work.

I tried to shut down the socket, close it and then use the same routine to connect that i initially use to connect. Tried a few variations, but nothing worked i Always get the Error-Code: "Winerror 10038" when i try to re-connect

import socket
import sys
import modules.cfg
import time

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

def connect():
    '''
    Connection to Twitch IRC using cfg.py
    '''
    irc.connect((modules.cfg.HOST,modules.cfg.PORT))
    irc.send(str("PASS " + modules.cfg.PASS + "\r\n").encode("utf-8"))
    irc.send(str("NICK " + modules.cfg.NICK + "\r\n").encode("utf-8"))
    irc.send(str("JOIN " + modules.cfg.CHAN + "\r\n").encode("utf-8"))
    irc.send(str("CAP REQ :twitch.tv/commands\r\n").encode("utf-8")) #whisper enable
    irc.send(str("CAP REQ :twitch.tv/membership\r\n").encode("utf-8"))

def read_chat():
    response = irc.recv(4096).decode('utf-8') #receive text
    if response == "PING :tmi.twitch.tv\r\n":
        print("Ping received")
        irc.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) 
    return response


def send(msg):
    try:
        irc.send("PRIVMSG {} : {}\r\n".format(modules.cfg.CHAN, msg).encode("utf-8"))
    except:
        irc.shutdown(socket.SHUT_RDWR)
        irc.close()
        print("\n\nDisconnected\n")
        time.sleep(10)
        connect()
        print("Reconnected\n\n")

I am pretty new to coding and it´s some kind of a hobby of mine. Hopefully someone can help me! Thank you guys

DrMohn
  • 1
  • 2
  • Have you considered looking up Winsock error 10038? 'Socket operation on non-socket'. You've closed the socket and are now trying to call `connect()` on it. You have to create a new socket. – user207421 Oct 10 '19 at 23:46
  • i tried having the line "irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)" in front of "irc.connect...." but got an error too.. the line i quoted is the one to open a new socket, isn´t it? (sry for my low-level knowledge) – DrMohn Oct 11 '19 at 08:42
  • You got *what* error? – user207421 Oct 11 '19 at 09:23
  • Winerror 10057 if i have it outside and inside "connect()" and an "irc is not defined" if only inside within the "read_chat()" (which i actually understand) Edit: while typing this i had the idea to have it inside "connect()" and define t as global. Didn´t come up with this one before, but also works. Thank you for helping me understand it a little bit better. still don´t understand the winerror 10057 though – DrMohn Oct 12 '19 at 11:29

1 Answers1

0

Thx to user207421 i finally found the way.. for me it is a bit strange, but it works.

def re_connect():
    irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    irc.connect((modules.cfg.HOST,modules.cfg.PORT))
    irc.send(str("PASS " + modules.cfg.PASS + "\r\n").encode("utf-8"))
    irc.send(str("NICK " + modules.cfg.NICK + "\r\n").encode("utf-8"))
    irc.send(str("JOIN " + modules.cfg.CHAN + "\r\n").encode("utf-8"))
    irc.send(str("CAP REQ :twitch.tv/commands\r\n").encode("utf-8")) #whisper enable
    irc.send(str("CAP REQ :twitch.tv/membership\r\n").encode("utf-8"))


def send(msg):
    try:
        irc.send("PRIVMSG {} : {}\r\n".format(modules.cfg.CHAN, msg).encode("utf-8"))
    except:
        irc.shutdown(socket.SHUT_RDWR)
        irc.close()
        print("\n\nDisconnected\n")
        time.sleep(10)
        re_connect()
        print("Reconnected\n\n")
DrMohn
  • 1
  • 2
  • 1
    This is exactly wat I told you to do above and you claimed didn't work. Please explain. – user207421 Oct 11 '19 at 09:25
  • the funny thing is when i change the "connect()" so that it looks like the "re_connect" i get an error while connecting in the first place. That´s what i tried. Thx to you i used a spereate one to reconnect and now both is working. connecting and reconnecting. What me confused was that i am getting an error if i define the socket within the "connect()" – DrMohn Oct 12 '19 at 11:11