# Twitch Plays bot or something

**URL:** <https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311>\
**Category:** Chat and IRC\
**Created:** [October 26, 2021, 5:30am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311 "2021-10-26T05:30:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zombieonyt](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/zombieonyt/32/14188_2.png) [@Zombieonyt](https://discuss.dev.twitch.com/u/Zombieonyt)\
**Post date:** [October 26, 2021, 5:30am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/1 "2021-10-26T05:30:18Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![WLG3R](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/wlg3r/32/6985_2.png) [@WLG3R](https://discuss.dev.twitch.com/u/WLG3R)\
**Post date:** [October 26, 2021, 11:57am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/2 "2021-10-26T11:57:03Z")

</div>

Are you responding to ping requests?

---

<div class="post-metadata">

**Author:** ![Zombieonyt](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/zombieonyt/32/14188_2.png) [@Zombieonyt](https://discuss.dev.twitch.com/u/Zombieonyt)\
**Post date:** [October 27, 2021, 1:44am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/3 "2021-10-27T01:44:42Z")

</div>

How do I check that ? or add that ?

---

<div class="post-metadata">

**Author:** ![WLG3R](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/wlg3r/32/6985_2.png) [@WLG3R](https://discuss.dev.twitch.com/u/WLG3R)\
**Post date:** [October 27, 2021, 1:56am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/4 "2021-10-27T01:56:37Z")

</div>

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.

> **[Chat & Chatbots](https://dev.twitch.tv/docs/irc/)**
>
> Twitch Chat & Chatbots

---

<div class="post-metadata">

**Author:** ![Zombieonyt](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/zombieonyt/32/14188_2.png) [@Zombieonyt](https://discuss.dev.twitch.com/u/Zombieonyt)\
**Post date:** [October 27, 2021, 2:00am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/5 "2021-10-27T02:00:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![WLG3R](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/wlg3r/32/6985_2.png) [@WLG3R](https://discuss.dev.twitch.com/u/WLG3R)\
**Post date:** [October 27, 2021, 5:01am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/6 "2021-10-27T05:01:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zombieonyt](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/zombieonyt/32/14188_2.png) [@Zombieonyt](https://discuss.dev.twitch.com/u/Zombieonyt)\
**Post date:** [October 27, 2021, 10:49am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/9 "2021-10-27T10:49:24Z")

</div>

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](http://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](http://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()

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [November 26, 2021, 10:50am UTC](https://discuss.dev.twitch.com/t/twitch-plays-bot-or-something/34311/10 "2021-11-26T10:50:04Z")

</div>

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