import socket, string
HOST = "irc.twitch.tv"
NICK = "FrostyBOT"
PORT = 6667
PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxx"
readbuffer = ""
MODT = False
s = socket.socket()
s.connect((HOST, PORT))
s.send("PASS " + PASS + "\r\n")
s.send("NICK " + NICK + "\r\n")
s.send("JOIN #thefrosty0806 \r\n")
def Send_message(message):
s.send("PRIVMSG #thefrosty0806 :" + message + "\r\n")
while True:
readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
for line in temp:
if (line[0] == "PING"):
s.send("PONG %s\r\n" % line[1])
else:
parts = string.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 = ':'.join(parts[2:])
except:
message = ""
usernamesplit = string.split(parts[1], "!")
username = usernamesplit[0]
if MODT:
print username + ': ' +message
if message == "Hey":
Send_message('Welcome to my stream, ' + username)
for l in parts:
if "End of /NAMES list" in l:
MODT = True
I am having trouble with making the BOT send messages to the chat. Any reasons why this might be happening? It reads the variable “message”.
Does your oauth token include all the relevant scopes? Sounds like you requested only chat:read
When you get your token, you must specify scopes (the permissions you are allowed on behalf of the authorized Twitch user). In this example, you need a token with the chat:read and chat:edit scopes.
I believe it does, I wrote two different versions of this BOT. (2nd version below)
import socket, string, re
HOST = "irc.twitch.tv"
NICK = "FrostyBOT"
PORT = 6667
PASS = "oauth:xxxxxxxxxxxxxxxxxxxxxxxxx"
readbuffer = ""
MODT = False
s = socket.socket()
s.connect((HOST, PORT))
s.send("PASS " + PASS + "\r\n")
s.send("NICK " + NICK + "\r\n")
s.send("JOIN #thefrosty0806 \r\n")
def Send_message(message):
s.send("PRIVMSG #thefrosty0806 :" + message + "\r\n")
while True:
readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
for line in temp:
# Checks whether the message is PING because its a method of Twitch to check if you're afk
if (line[0] == "PING"):
s.send("PONG %s\r\n" % line[1])
else:
# Splits the given string to work with it better
parts = string.split(line, ":")
if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
try:
message = parts[2][:len(parts[2]) - 1]
except:
message = ""
usernamesplit = string.split(parts[1], "!")
username = usernamesplit[0]
if MODT:
print username + ": " + message
#Adding Plain Commands
if message == "!bot":
Send_message("This is a BOT to for my seminar class")
for l in parts:
if "End of /NAMES list" in l:
MODT = True
The only thing I changed in version 2 was
message = parts[2][:len(parts[2]) - 1]
this works, but it cannot read inputs such as https://google.com (anything that comes after “http” it wont read)
but this version sends message just fine.
Which is splitting around the : in https:// of the URL in the example your provided…
You can either fix this so it’s only splitting once, or you need to look at using a “proper” IRC Tokeniser to “more correctly” parse a incoming Raw IRC line. You’ll probably need this anyway, as if you look at doing more complex stuff with Twitch Chat, you’ll enable Commands and Tags. Which’ll include more : in the raw line
I believe that part of the code doesn’t split the inputs of the user but rather the structure of the chat.
Username: "whatever the user writes*
So its splitting Username and "whatever the user writes.