Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创造一个新的世界!地址和地址!twitch irc bot上的delcom命令。跳过代码行_Python_Python 2.7_Irc_Twitch - Fatal编程技术网

Python 创造一个新的世界!地址和地址!twitch irc bot上的delcom命令。跳过代码行

Python 创造一个新的世界!地址和地址!twitch irc bot上的delcom命令。跳过代码行,python,python-2.7,irc,twitch,Python,Python 2.7,Irc,Twitch,我正在创建一个twitch聊天机器人,我正在尝试创建和!地址和地址!用于通过聊天创建和删除命令的delcom命令。事实上,几乎是同一行代码对一个命令起作用,而对另一个命令不起作用。我用指纹和破片测试了一下,它看起来像是跳过了它 他刚才跳过的线是: if globalcommands.has_key(commanddel): sendMessage(irc, channel, user + " you can't add a command called "

我正在创建一个twitch聊天机器人,我正在尝试创建和!地址和地址!用于通过聊天创建和删除命令的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
导入字符串
导入json
从套接字导入openSocket,发送消息
从初始化导入joinRoom
从读取导入getUser、getMessage、getChannel、字符串\u 1、字符串\u 2\u plus
irc=openSocket()
会议室(irc)
readbuffer=“”
irc.send(“CAP请求:twitch.tv/membership\r\n”)
irc.send(“CAP REQ:twitch.tv/commands\r\n”)
尝试:
将文件(“commands.json”、“r”)作为commandsDatabase:
commands=json.load(commandsDatabase)
除IOError外:
命令={}
将文件(“commands.json”、“w”)作为commandsDatabase:
dump(命令、commandsDatabase)
全局命令={“addcom”:True,“delcom”:True,“spank”:True}
尽管如此:
readbuffer=readbuffer+irc.recv(1024)
temp=string.split(readbuffer,“\n”)
readbuffer=temp.pop()
对于线路输入温度:
###香精###--------------------------------------------------------------------------------------------------------------------------------------------
#这是一个很好的例子-----------------------------------------------------------------------
打印(行)
#---------------------------------------------------------------------------------------------------------------------------------------------
#这是一个很好的例子-------------------------------------------------------------------------------------------
如果line.startswith('PING'):
irc.send('PONG'+line.split()[1]+'\r\n')
打印“积水回”
打破
#---------------------------------------------------------------------------------------------------------------------------------------------
#这是一条运河,用于开发和利用IRC服务。伏尔塔·塞尔古姆代表努洛--------------------------
user=getUser(行)
message=getMessage(行)
通道=获取通道(线)
如果通道==无或用户==无或消息==无:
打破
#---------------------------------------------------------------------------------------------------------------------------------------------
#随机字符串------------------------------------------------------------------------------------------------------------------------------
stringspace=“”
什么也没有
#---------------------------------------------------------------------------------------------------------------------------------------------
#格式为文本,格式为控制台--------------------------------------------------------------------------------------------------
打印频道+“:“+用户+”>“+消息
#---------------------------------------------------------------------------------------------------------------------------------------------
###本质是结束###----------------------------------------------------------------------------------------------------------------------------------------
如果message.startswith(“!addcom”):
如果message.count(stringspace)>=2:
尝试:
commandadd=string_1(消息)
答案=字符串2加上(消息)
除索引器外:
sendMessage(irc、channel、user+“命令是这样使用的!addcom!”)
打破
如果GlobalCommand.has_键(commandadd):
sendMessage(irc、频道、用户+“您不能添加名为“+”!“+commandadd+”“!!!”)的命令)
打破
尝试:
命令[命令添加]
除KeyError外:
命令[commandadd]=应答
sendMessage(irc、频道、用户+“命令!”+commandadd+“已添加!!!”)
将文件(“commands.json”、“w”)作为commandsDatabase:
dump(命令、commandsDatabase)
打破
sendMessage(irc、频道、用户+“您试图添加的命令已存在!!!”)
打破
sendMessage(irc、channel、user+“命令是这样使用的!addcom!”)
打破
如果message.startswith(“!delcom”):
如果message.count(stringspace)==1:
尝试:
commanddel=string_1(消息)
除索引器外:
sendMessage(irc、channel、user+“命令是这样使用的!delcom!”)
打破
如果GlobalCommand.具有_键(commanddel):
sendMessage(irc、频道、用户+“您不能添加名为“+”!“+commanddel+”!!!”)的命令)
打破
尝试:
命令[commanddel]
除KeyError外:
sendMessage(irc、频道、用户+“您试图删除的命令不存在!!!”)
打破
del命令[commanddel]
sendMessage(irc、频道、用户+“命令!”+commanddel+“已被删除!!!”)
将文件(“commands.json”、“w”)作为commandsDatabase:
dump(命令、commandsDatabase)
打破
sendMessage(irc、channel、user+“命令是这样使用的!delcom!”)
打破

如果您需要查看此处的任何其他文件,您可以查看我的github存储库:

我已经发现我没有正确定义内容。我忘了在li的末尾有一个\r
def string_1(message):
string1 = message.split("!", 2)[2].split("\r", 1)[0]
return string1