在 twitch irc 机器人上创建 !addcom 和 !delcom 命令。跳过代码行
Creating an !addcom and !delcom commands on twitch irc bot. Jump over line of code
我正在创建一个 twitch 聊天机器人,我正在尝试创建 !addcom 和 !delcom 命令以通过聊天创建和删除命令。而且几乎同一行代码对一个命令有效,而对另一个命令无效。我测试了打印和断点,看起来它只是跳过了它。
他刚刚跳过的那行是:
if globalcommands.has_key(commanddel):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commanddel + '" !!!')
break
这里是完整代码:
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
如果您需要在此处查看任何其他文件,您有我的 github 存储库:https://github.com/BlasterJoni/ClientSideTwitchChatBotPython/
我已经发现我没有正确定义事物。我忘了行尾有一个 \r。
我定义了两种从消息中获取 string1 的不同方法,一种用于 !addcom,另一种用于 !delcom。
我只需要这样定义 !delcom:
def string_1(message):
string1 = message.split("!", 2)[2].split("\r", 1)[0]
return string1
它开始工作了。
我正在创建一个 twitch 聊天机器人,我正在尝试创建 !addcom 和 !delcom 命令以通过聊天创建和删除命令。而且几乎同一行代码对一个命令有效,而对另一个命令无效。我测试了打印和断点,看起来它只是跳过了它。
他刚刚跳过的那行是:
if globalcommands.has_key(commanddel):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commanddel + '" !!!')
break
这里是完整代码:
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
如果您需要在此处查看任何其他文件,您有我的 github 存储库:https://github.com/BlasterJoni/ClientSideTwitchChatBotPython/
我已经发现我没有正确定义事物。我忘了行尾有一个 \r。
我定义了两种从消息中获取 string1 的不同方法,一种用于 !addcom,另一种用于 !delcom。
我只需要这样定义 !delcom:
def string_1(message):
string1 = message.split("!", 2)[2].split("\r", 1)[0]
return string1
它开始工作了。