I’m working on a Twitch chat bot and having trouble with !create and !remove command functionality. The weird thing is that nearly identical code works for one command but not the other. When I add debug prints and breakpoints, it seems like the program just skips over certain lines completely.
The problematic line that gets skipped is:
if protectedcommands.has_key(cmdtoremove):
transmitMessage(socket, chatroom, username + " you cannot remove a protected command " + '"!' + cmdtoremove + '" !!!')
break
Here’s my complete bot code:
import string
import json
from Connection import createSocket, transmitMessage
from Setup import enterChannel
from Parser import extractUser, extractMessage, extractChannel, first_word, remaining_words
socket = createSocket()
enterChannel(socket)
buffer = ""
socket.send("CAP REQ :twitch.tv/membership\r\n")
socket.send("CAP REQ :twitch.tv/commands\r\n")
try:
with file("botcommands.json","r") as cmdfile:
botcommands = json.load(cmdfile)
except IOError:
botcommands = {}
with file("botcommands.json","w") as cmdfile:
json.dump(botcommands, cmdfile)
protectedcommands = {"create": True, "remove": True, "ban": True}
while True:
buffer = buffer + socket.recv(1024)
lines = string.split(buffer, "\n")
buffer = lines.pop()
for currentline in lines:
print (currentline)
if currentline.startswith('PING'):
socket.send('PONG ' + currentline.split( ) [ 1 ] + '\r\n')
print "PONG SENT"
break
username = extractUser(currentline)
message = extractMessage(currentline)
chatroom = extractChannel(currentline)
if chatroom == None or username == None or message == None:
break
space = " "
empty = ""
print chatroom + ": " + username + " > " + message
if message.startswith("!create "):
if message.count(space) >= 2:
try:
newcmd = first_word(message)
response = remaining_words(message)
except IndexError:
transmitMessage(socket, chatroom, username + " usage: !create !<cmd_name> <cmd_response>")
break
if protectedcommands.has_key(newcmd):
transmitMessage(socket, chatroom, username + " you cannot create a command named " + '"!' + newcmd + '" !!!')
break
try:
botcommands[newcmd]
except KeyError:
botcommands[newcmd] = response
transmitMessage(socket, chatroom, username + " command !" + newcmd + " created successfully!!!")
with file("botcommands.json","w") as cmdfile:
json.dump(botcommands, cmdfile)
break
transmitMessage(socket, chatroom, username + " that command already exists!!!")
break
transmitMessage(socket, chatroom, username + " usage: !create !<cmd_name> <cmd_response>")
break
if message.startswith("!remove "):
if message.count(space) == 1:
try:
cmdtoremove = first_word(message)
except IndexError:
transmitMessage(socket, chatroom, username + " usage: !remove !<cmd_name>")
break
if protectedcommands.has_key(cmdtoremove):
transmitMessage(socket, chatroom, username + " you cannot remove a protected command " + '"!' + cmdtoremove + '" !!!')
break
try:
botcommands[cmdtoremove]
except KeyError:
transmitMessage(socket, chatroom, username + " command does not exist!!!")
break
del botcommands[cmdtoremove]
transmitMessage(socket, chatroom, username + " command !" + cmdtoremove + " removed successfully!!!")
with file("botcommands.json","w") as cmdfile:
json.dump(botcommands, cmdfile)
break
transmitMessage(socket, chatroom, username + " usage: !remove !<cmd_name>")
break
Any ideas why this specific check gets bypassed? The logic looks identical to the working version but behaves completely different.