Twitch Plays bot or something

Hello I dunno if this is the right section or not but I don’t have a clue what I am doing but I have set up a python program it connects to my twitch all fine but after so long it keeps losing connecting and then close itself down or says connection was shut by a remote location. Dunno what I am doing wrong but would like some help/advice.
This file here is main . Py

#Define the imports
import twitch
import keypresser
import keyholder
t = twitch.Twitch();
k = keypresser.Keypresser();

#Enter your twitch username and oauth-key below, and the app connects to twitch with the details.
#Your oauth-key can be generated at http://.com/tmi/
username = “abc”;
key = “abc”;
t.twitch_connect(username, key);

#The main loop
while True:
#Check for new mesasages
new_messages = t.twitch_recieve_messages();

if not new_messages:
    #No new messages...
    continue
else:
    for message in new_messages:
        #Wuhu we got a message. Let's extract some details from it
        msg = message['message'].lower()
        username = message['username'].lower()
        print(username + ": " + msg.encode('utf-8'));

        #This is where you change the keys that shall be pressed and listened to.
        #The code below will simulate the key q if "q" is typed into twitch by someone
        #.. the same thing with "w"
        #Change this to make Twitch fit to your game!
        if msg == "w": keyholder.holdForSeconds(msg, 0.3);
        if msg == "b": keyholder.holdForSeconds(msg, 0.3);
        if msg == "a": keyholder.holdForSeconds(msg, 0.1);
        if msg == "d": keyholder.holdForSeconds(msg, 0.1);
        if msg == "x": keyholder.holdForSeconds(msg, 0.5);
        if msg == "q": keyholder.holdForSeconds(msg, 0.1);
        if msg == "r": keyholder.holdForSeconds(msg, 0.5);
        if msg == "y": keyholder.holdForSeconds(msg, 0.1);
        if msg == "u": keyholder.holdForSeconds(msg, 0.1);
        if msg == "f": keyholder.holdForSeconds(msg, 0.1);
        if msg == "e": keyholder.holdForSeconds(msg, 0.1);
        if msg == "l": keyholder.holdForSeconds(msg, 0.1);

Are you responding to ping requests?

How do I check that ? or add that ?

I don’t know python specifically. But the guide tells you how to respond to ping requests. Just respond with the ping message as the docs indicate.

I see it what do I have to add with " About once every five minutes, the server will send you a PING :tmi.twitch.tv . To ensure that your connection to the server is not prematurely terminated, reply with PONG :tmi.twitch.tv ." Dose it have to be in a specific place or can it be placed anywhere and what file dose it have to be placed in main. py twitch ect.

Where in your code you decide as the developer. You just have to listen for the ping and respond when it comes in. How you code that is up to you.

So I have set up all the code like the tutorial says but it connects to twitch even the message pops up on twitch joined the chat but after that nothing. Can anyone help/tell me what has gone wrong thanks

:tmi.twitch.tv 001 zombieonyt_on_twitch :Welcome, GLHF!

:tmi.twitch.tv 002 zombieonyt_on_twitch :Your host is tmi.twitch.tv

:tmi.twitch.tv 003 zombieonyt_on_twitch :This server is rather new

:tmi.twitch.tv 004 zombieonyt_on_twitch :-

:tmi.twitch.tv 375 zombieonyt_on_twitch :-

:tmi.twitch.tv 372 zombieonyt_on_twitch :You are in a maze of twisty passages, all alike.

:tmi.twitch.tv 376 zombieonyt_on_twitch :>

:zombieonyt_on_twitch!zombieonyt_on_twitch@zombieonyt_on_twitch.tmi.twitch.tv JOIN #zombieonyt_on_twitch

:zombieonyt_on_twitch.tmi.twitch.tv 353 zombieonyt_on_twitch = #zombieonyt_on_twitch :zombieonyt_on_twitch

:zombieonyt_on_twitch.tmi.twitch.tv 366 zombieonyt_on_twitch #zombieonyt_on_twitch :End of /NAMES list

Bot has joined zombieonyt_on_twitch’s Channel!

import socket
import pyautogui
import threading
SERVER = “irc.twitch.tv
PORT = 6667
PASS = “”
BOT = “Zombiebot”
CHANNEL = “”
OWNER = “”
message = “”
irc = socket.socket()
irc.connect((SERVER, PORT))
irc.send(( "PASS " + PASS + “\n” +
"NICK " + BOT + “\n” +
“JOIN #” + CHANNEL + “\n”).encode())

def gamecontrol():
global message
while True:
if “w” in message.lower():
pyautogui.keydown(‘w’)
message = “”
pyautogui.keyup(‘w’)
elif “b” in message.lower():
pyautogui.keydown(‘b’)
message = “”
pyautogui.keyup(‘b’)
elif “a” in message.lower():
pyautogui.keydown(‘a’)
message = “”
pyautogui.keyup(‘a’)
elif “d” in message.lower():
pyautogui.keydown(‘d’)
message = “”
pyautogui.keyup(‘d’)
elif “x” in message.lower():
pyautogui.keydown(‘x’)
message = “”
pyautogui.keyup(‘x’)
elif “q” in message.lower():
pyautogui.keydown(‘q’)
message = “”
pyautogui.keyup(‘q’)
elif “r” in message.lower():
pyautogui.keydown(‘r’)
message = “”
pyautogui.keyup(‘r’)
elif “l” in message.lower():
pyautogui.keydown(‘l’)
message = “”
pyautogui.keyup(‘l’)
elif “f” in message.lower():
pyautogui.keydown(‘f’)
message = “”
pyautogui.keyup(‘f’)
elif “y” in message.lower():
pyautogui.keydown(‘y’)
message = “”
pyautogui.keyup(‘y’)
elif “e” in message.lower():
pyautogui.keydown(‘e’)
message = “”
pyautogui.keyup(‘e’)
else:
pass

def twitch():
def joinchat():
Loading = True
while Loading:
readbuffer_join = irc.recv(1024)
readbuffer_join = readbuffer_join.decode()
for line in readbuffer_join.split("\n")[0:-1]:
print(line)
Loading = loadingComplete(line)
def loadingComplete(line):
if (“End of /NAMES list” in line):
print(“Bot has joined " + CHANNEL + “'s Channel!”)
sendMessage(irc, “Chat Room Joined”)
return False
else:
return True
def sendMessage(irc, message):
messageTemp = “PRIVMSG #” + CHANNEL + " :” + message
irc.send((messageTemp + “\n”).encode())
def getUser(line):
separate = line.split(":", 2)
user = separate[1].split("!", 1)[0]
return user
def getMessage(line):
global message
try:
message = (line.split(":",2))[2]
except:
message = “”
return message
def Console(line):
if “PRIVMSG” in line:
return False
else:
return True

joinchat()
	
while True:
	try:
		readbuffer = irc.recv(1024).decode()
	except:	
		readbuffer = ""
		for line in readbuffer.split("\r\n"):
			if line == "":
				continue
			elif "PING" in line and Console(line):
				msgg = "PONG tmi.twitch.tv\r\n".encode()
				irc.send(msgg)
				print(msgg)
				continue
			else:
				print(line)
				user = getUser(line)
				message = getMessage(line)
				print(user + " : " + message)

if name ==‘main’:
t1 = threading.Thread(target = twitch)
t1. start()
t2 = threading.Thread(target = gamecontrol)
t2.start()

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