I'm creating a twitch chat bot to read the chat on my stream. But when I try to .split() the incoming string into separate strings to isolate the username and message, it displays an extra ' and ["'"]. when I try to print the strings separately by index I get an index error.
Following is the code which connects the the twitch chat fine, and the result when I type "test" into the chat.
from settings import *
import socket
import threading
class twitch:
def __init__(self, host, port, nick, pwd, channel):
self.s = socket.socket()
self.s.connect((host, port))
self.s.send(bytes("PASS " + pwd + "\r\n", "UTF-8"))
self.s.send(bytes("NICK " + nick + "\r\n", "UTF-8"))
self.s.send(bytes("JOIN #" + channel + " \r\n", "UTF-8"))
self.s.send(bytes("PRIVMSG #" + channel + " :" + "Connected " + "\r\n", "UTF-8"))
self.alive = True
readerthread = threading.Thread(target=self.read_chat)
readerthread.start()
def read_chat(self):
while self.alive:
for line in str(self.s.recv(1024)).split('\\r\\n'):
if "PING :tmi.twitch.tv" in line:
print(time.strftime("%H:%M:%S"), "PONG :tmi.twitch.tv")
s.send(bytes("PONG :tmi.twitch.tv\r\n", "UTF-8"))
else:
print(line)
parts = line.split(":")
print(parts)
def main():
tc = twitch(HOST, PORT, NICK, PASS, CHANNEL)
Printing the string (line) to the console produces: b':username!username@username.tmi.twitch.tv PRIVMSG #username :test
However when I split the string and print the list of strings (parts) it produces this: ["b'", 'username!username@username.tmi.twitch.tv PRIVMSG #username ', 'test'] ' ["'"]