I am creating a twitch chat bot and I am trying to create and !addcom and !delcom commands to create and delete commands trough chat. And the thing is almost the same line of code is working for one command and for the other doesn't work. I tested with prints and breaks and it looks like it just jumps over it.
The line he just jumps over is:
if globalcommands.has_key(commanddel):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commanddel + '" !!!')
break
And here is the full code:
import string
import json
from Socket import openSocket, sendMessage
from Initialize import joinRoom
from Read import getUser, getMessage, getChannel, string_1, string_2_plus
irc = openSocket()
joinRoom(irc)
readbuffer = ""
irc.send("CAP REQ :twitch.tv/membership\r\n")
irc.send("CAP REQ :twitch.tv/commands\r\n")
try:
with file("commands.json","r") as commandsDatabase:
commands = json.load(commandsDatabase)
except IOError:
commands = {}
with file("commands.json","w") as commandsDatabase:
json.dump(commands, commandsDatabase)
globalcommands = {"addcom": True, "delcom": True, "spank": True}
while True:
readbuffer = readbuffer + irc.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
for line in temp:
###Essenciais###--------------------------------------------------------------------------------------------------------------------------------------------
#Mostra a linha que e dada pelo servidor de IRC (So pelo sim pelo nao).-----------------------------------------------------------------------
print (line)
#---------------------------------------------------------------------------------------------------------------------------------------------
#Impede que seja desconectado pelo servidor de IRC.-------------------------------------------------------------------------------------------
if line.startswith('PING'):
irc.send('PONG ' + line.split( ) [ 1 ] + '\r\n')
print "PONGED BACK"
break
#---------------------------------------------------------------------------------------------------------------------------------------------
#Le a linha que e dada pelo servidor de IRC e devevole o utilizador, a menssagem e o canal. Volta se algum for nulo.--------------------------
user = getUser(line)
message = getMessage(line)
channel = getChannel(line)
if channel == None or user == None or message == None:
break
#---------------------------------------------------------------------------------------------------------------------------------------------
#Random Strings.------------------------------------------------------------------------------------------------------------------------------
stringspace = " "
nothing = ""
#---------------------------------------------------------------------------------------------------------------------------------------------
#Formata o texto e mostra mostra na consola.--------------------------------------------------------------------------------------------------
print channel + ": " + user + " > " + message
#---------------------------------------------------------------------------------------------------------------------------------------------
###Essenciais END###----------------------------------------------------------------------------------------------------------------------------------------
if message.startswith("!addcom "):
if message.count(stringspace) >= 2:
try:
commandadd = string_1(message)
answer = string_2_plus(message)
except IndexError:
sendMessage(irc, channel, user + " the command is used this way !addcom !<command_name> <command_answer>")
break
if globalcommands.has_key(commandadd):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commandadd + '" !!!')
break
try:
commands[commandadd]
except KeyError:
commands[commandadd] = answer
sendMessage(irc, channel, user + " the command !" + commandadd + " has been added!!!")
with file("commands.json","w") as commandsDatabase:
json.dump(commands, commandsDatabase)
break
sendMessage(irc, channel, user + " the command you tried to add alredy exists!!!")
break
sendMessage(irc, channel, user + " the command is used this way !addcom !<command_name> <command_answer>")
break
if message.startswith("!delcom "):
if message.count(stringspace) == 1:
try:
commanddel = string_1(message)
except IndexError:
sendMessage(irc, channel, user + "the command is used this way !delcom !<command_name>")
break
if globalcommands.has_key(commanddel):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commanddel + '" !!!')
break
try:
commands[commanddel]
except KeyError:
sendMessage(irc, channel, user + " the command you tried to delete doens't exist!!!")
break
del commands[commanddel]
sendMessage(irc, channel, user + " the command !" + commanddel + " has been deleted!!!")
with file("commands.json","w") as commandsDatabase:
json.dump(commands, commandsDatabase)
break
sendMessage(irc, channel, user + " the command is used this way !delcom !<command_name>")
break
If you need to look at any other files here you have my github repository: https://github.com/BlasterJoni/ClientSideTwitchChatBotPython/
I alredy figured it out I wasnt defining things properly. I forgot there is an \r at the end of the line.
I defined two diferent ways of getting the string1 from the message one for !addcom and on for !delcom.
I just had to define the !delcom one this way:
def string_1(message):
string1 = message.split("!", 2)[2].split("\r", 1)[0]
return string1
and it started working.