Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/Twitch Bot
#1
Hello Everyone,
I am working on a project for my twitch stream that will need to involve twitch chat commands going to a python script then those commands being passed on to the Arduino serial monitor. For the most part, I have it working however, of course there is a bug in the code that I have not been able to squish and was hoping for some advice on where to start.

Problem: The python bot works for 11 minutes then repeats the same lines of code and becomes unresponsive. The bot will not send or receive any information until it is closed and opened again.
Here is the debug output picture [Image: rk96boxjr]

At first I thought it was something wrong with the Ping Pong, but Twitch IRC sends pings roughly every 5 minutes as stated by https://dev.twitch.tv/docs/irc So I thought it would be odd if it succeeded through the first Ping, but not the second, then I launched the code and debug mode and got the above picture.

Like I said, the system works for about 11 minutes then it stops working, and I am at a loss. This code originally did not work at all and I made some changes to get it to connect and then tried to update it to python 3, which is why all the encode/decodes are there (which I'm not used to doing being more accustomed to python 2)/


I appreciate anyone giving their thoughts on what I could do to help troubleshoot this problem.
Thank you everyone!


Code:
 
#using python version 3.6.5
#converted from python version 2.x
import socket, string
import serial
arduinoSerialData = serial.Serial('com7',9600)  #this is to allow for communication between the python script and the Arduino device.  

# Set all the variables necessary to connect to Twitch IRC
HOST = "IRC goes here"  #Chat IRC goes here, for me it is irc.twitch.tv
NICK = "Nickname Here"      #Nickname for the bot goes here
PORT = portNumber Here             #port number to connect to
PASS = "authentication token here"   #authentication token to log in
readbuffer = ""
MODT = False

# Connecting to Twitch IRC by passing credentials and joining a certain channel
s = socket.socket()
s.connect((HOST, PORT))

outputPassMsg = "PASS " + PASS + "\r\n"
s.send(outputPassMsg.encode('utf-8'))


outputNickMsg = "NICK " + NICK + "\r\n"
s.send(outputNickMsg.encode('utf-8'))


outputChanMsg = "JOIN #ChannelNameHere \r\n"  #Change this to your channel name
s.send(outputChanMsg.encode('utf-8'))


# Method for sending a message
def Send_message(message):
    s.send(("PRIVMSG #ChannelNameHere :" + "From PyBot: " + message + "\r\n").encode('utf-8'))    #Change this to your channel name

#This is the problem-------------------------------------------------------------Happens around 11 minutes in lines 36, 37, 40, 35, 36, 37, repeating
while True:
    readbuffer = readbuffer.encode('utf-8') + s.recv(1024)
    
    temp = str.split(readbuffer.decode('utf-8'), "\n")
    
    readbuffer = temp.pop()
#This is the problem-------------------------------------------------------------

    for line in temp:
        # Checks whether the message is PING because its a method of Twitch to check if you're afk
        #maybe replace with s.send(bytes("PONG :tmi.twitch.tv\r\n", "UTF-8"))
        if (line[0] == "PING"):
            #PingReply = "PONG %s\r\n" % line[1]
            #s.send (PingReply.encode('utf-8'))
            
        else:
            # Splits the given string so we can work with it better
            parts = str.split(line, ":")

            if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
                try:
                    # Sets the message variable to the actual message sent
                    message = parts[2][:len(parts[2]) - 1]
                except:
                    message = ""
                # Sets the username variable to the actual username
                usernamesplit = str.split(parts[1], "!")
                username = usernamesplit[0]
                
                # Only works after twitch is done announcing stuff (MODT = Message of the day)
                if MODT:
                    print (username + ": " + message)
                    
                    # You can add all your plain commands here
                    if message == "Hey":
                        Send_message("Welcome to my stream: " + username)
                    
                    if message == ("fog") or ("rust") or ("party"):
                        arduinoSerialData.write(message.encode())  #send these commands to the Arduino, the Arduino code will handle the rest.

                for l in parts:
                    if "End of /NAMES list" in l:
                        MODT = True
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020