[Solved] First Bot - Can read the chat, but cannot send messages

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!")

Socket.Py

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)

Settings.Py

HOST = "irc.chat.twitch.tv"
PORT = 6667
PASS = "oauth:[retracted]"
IDENT = "exasbot" #always lowercase
CHANNEL = "exaskryz" #always lowercase

Init.Py

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

Read.Py

import string

def getUser(line): #example of line is :username!username@username.tmi.twitch.tv PRIVMSG #username :Message
	separate = line.split(":",2) # the 2 will split this into 3 parts; 2 slices separate the parts
	user = separate[1].split("!",1)[0] # 0 preceeds the first colon or exclamation mark
	return user
def getMessage(line):
	separate = line.split(":",2)
	try:
		message = separate[2]
	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
:exasbot!exasbot@exasbot.tmi.twitch.tv 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!

[Solved].

I messed up the Messaging Syntax.

s.send(messageTemp + "\r\n")

I forgot to append the end characters to my messages. I imagine they were sent, but the server never posted them into chat because it was never told that was the end of my message. How embarrassing; just one thing I missed while trying to keep up with the video. (Source code was linked to in description, which is how I noticed this error.)

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