Where have I gone wrong in writing this python script? I am very new to it, so it may be an obvious oversight to someone with experience in this language. I followed a tutorial at https://www.youtube.com/watch?v=T8DLwACpe3o and some information appears it’s outdated, which I thought I updated by reading over the API documentation at https://dev.twitch.tv/docs/irc
I think I’m following the examples just fine, but they aren’t working.
Run.Py (main file)
import string from Read import getUser, getMessage from Socket import openSocket, sendMessage from Init import joinRoom from Settings import CHANNEL readbuffer = "" s = openSocket() joinRoom(s) while True: readbuffer = readbuffer + s.recv(1024) temp = string.split(readbuffer, "\n") readbuffer = temp.pop() for line in temp: print(line) if "Bot Go Away" in line: s.send("PART #" + CHANNEL) # This seems to fail, as I am still shown on viewers in chat window and my script displays messages typed in chat print("attempted to leave") if "PING" in line: s.send(line.replace("PING","PONG")) #send msg to the server, not to chat. This is different from sendMessage custom definition print("I just sent a PONG") user = getUser(line) message = getMessage(line) print user + " typed :" + message if "You Suck" in message: sendMessage(s, "No, you suck!")
import socket from Settings import HOST, PORT, PASS, IDENT, CHANNEL def openSocket(): # I think this is a colon, can't tell on TV for sure s = socket.socket() s.connect((HOST,PORT)) s.send("PASS " + PASS + "\r\n") # "PASS oauthkey\r\n" s.send("NICK " + IDENT + "\r\n") # this gets us signed into IRC s.send("JOIN #" + CHANNEL + "\r\n") s.send(":" + IDENT + "!" + IDENT + "@" + IDENT + ".tmi.twitch.tv JOIN #" + CHANNEL) print("checkpoint") return s def sendMessage(s, message): messageTemp = "PRIVMSG #" + CHANNEL + " :" + message #PRIVMSG #bad_nidalee :Hello ... no idea why it's PRIVMSG, but that is a public msg. s.send(messageTemp) print("Sent: " + messageTemp)
HOST = "irc.chat.twitch.tv" PORT = 6667 PASS = "oauth:[retracted]" IDENT = "exasbot" #always lowercase CHANNEL = "exaskryz" #always lowercase
import string from Socket import sendMessage def joinRoom(s): readbuffer = "" Loading = True while Loading: readbuffer = readbuffer + s.recv(1024) temp = string.split(readbuffer, "\n") readbuffer = temp.pop() for line in temp: print(line + "___--___") Loading = loadingComplete(line) sendMessage(s, "Successfully joined chat") def loadingComplete(line): if("End of /NAMES list" + line): return False else: return True
import string def getUser(line): #example of line is :firstname.lastname@example.org PRIVMSG #username :Message separate = line.split(":",2) # the 2 will split this into 3 parts; 2 slices separate the parts user = separate.split("!",1) # 0 preceeds the first colon or exclamation mark return user def getMessage(line): separate = line.split(":",2) try: message = separate except IndexError: message = 'null' return message
What my Command Prompt reads when I run this code:
C:\Users\Me\Google Drive\Notepads\Run.py checkpoint ___--___tch.tv 001 exasbot :Welcome, GLHF! ___--___tch.tv 002 exasbot :Your host is tmi.twitch.tv ___--___tch.tv 003 exasbot :This server is rather new ___--___tch.tv 004 exasbot :- ___--___tch.tv 375 exasbot :- ___--___tch.tv 372 exasbot :You are in a maze of twisty passages, all alike. ___--___tch.tv 376 exasbot :> Sent: PRIVMSG #exaskryz :Successfully joined chat :email@example.com JOIN #exaskryz exasbot typed :null :exasbot.tmi.twitch.tv 353 exasbot = #exaskryz :exasbot exasbot.tmi.twitch.tv 353 exasbot = #exaskryz typed :exasbot :exasbot.tmi.twitch.tv 366 exasbot #exaskryz :End of /NAMES list exasbot.tmi.twitch.tv 366 exasbot #exaskryz typed :End of /NAMES list PING :tmi.twitch.tv I just sent a PONG typed :nullv PING :tmi.twitch.tv I just sent a PONG typed :nullv
—So the Ping and Pong seem to work. But messages do not get sent to chat; despite where it says “Successfully joined chat”, that was not shown.
I have made my bot a moderator in my chat, and I have asked to be put on the known bots list, but I’m not getting any results.
I also realized that my print(line + “___–___”) is weird that it overwrites the first characters. (Weird to me; likely makes sense to someone who learned the language.) If I remove the appending of the – to the print function, those lines get changed into:
:tmi.twitch.tv 001 exasbot :Welcome, GLHF!