Python:Indexer错误:列表索引超出IRC Bot的范围

Python:Indexer错误:列表索引超出IRC Bot的范围,python,split,irc,twitch,Python,Split,Irc,Twitch,我一直在制作一个Python Twitch IRC机器人,除了一个问题外,它还可以正常工作。大约每20分钟它就会因错误而崩溃: Traceback (most recent call last): File "E:\Geekster_Bot\Geekster_Bot Code\Geekster_Bot_Test_Write", line 54, in <module> user = data.split(':')[1] IndexError: list index out

我一直在制作一个Python Twitch IRC机器人,除了一个问题外,它还可以正常工作。大约每20分钟它就会因错误而崩溃:

Traceback (most recent call last):
  File "E:\Geekster_Bot\Geekster_Bot Code\Geekster_Bot_Test_Write", line 54, in <module>
    user = data.split(':')[1]
IndexError: list index out of range
这是来自IRC的传入数据被分割的点。“:”是拆分,因为这是在IRC中昵称和IRC消息之间使用的内容。但是即使不使用,它也不会崩溃约20分钟。使用时,它也会这样做。直到20分钟后才发生碰撞。

有什么想法吗

更新

queue = 0 #sets variable for anti-spam queue functionality

newsmsg = 'whitelist'

irc = socket.socket()
irc.connect((server, 6667)) #connects to the server

#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')

def queuetimer(): #function for resetting the queue every 30 seconds
    global queue
    print 'queue reset'
    queue = 0
    threading.Timer(30,queuetimer).start()
queuetimer()

while True:

    def message(msg): #function for sending messages to the IRC chat
        global queue
        queue = queue + 1
        print queue
        if queue < 20: #ensures does not send >20 msgs per 30 seconds.
            irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
        else:
            print 'Message deleted'

    def socialtimer(): #function for announcing social 
        global ntimer
        z = open('E:\mIRC\Twitter.txt')
        SOCIAL = z.read()
        message (SOCIAL)
        print 'Social Timers Started!'
        ntimer = threading.Timer(1200,socialtimer)
        ntimer.start()


data = irc.recv(1204) #gets output from IRC server
try: user = data.split(':')[1];
except IndexError: print (data)
user = user.split('!')[0] #determines the sender of the messages
print (data)
queue=0#设置反垃圾邮件队列功能的变量
newsmsg='白名单'
irc=socket.socket()
irc.connect((服务器,6667))#连接到服务器
#发送用于连接到twitch聊天室的变量
irc.send('PASS'+password+'\r\n')
irc.send('USER'+nick+'0*:'+bot\u owner+'\r\n')
irc.send('NICK'+NICK+'\r\n')
irc.send('JOIN'+channel+'\r\n')
def queuetimer():#用于每30秒重置队列的函数
全局队列
打印“队列重置”
队列=0
threading.Timer(30,queuetimer.start())
队列计时器()
尽管如此:
def消息(msg):#用于向IRC聊天室发送消息的功能
全局队列
队列=队列+1
打印队列
如果队列<20:#确保不会每30秒发送>20个MSG。
发送('PRIVMSG'+channel+':'+msg+'\r\n')
其他:
打印“已删除邮件”
def socialtimer():#用于宣布社交信息的函数
全局计数器
z=open('E:\mIRC\Twitter.txt')
社交=z.阅读()
信息(社会)
打印“社交计时器已启动!”
ntimer=线程计时器(1200,socialtimer)
ntimer.start()
data=irc.recv(1204)#从irc服务器获取输出
try:user=data.split(“:”)[1];
除索引器外:打印(数据)
user=user.split(“!”)[0]#确定邮件的发件人
打印(数据)

下面是我使用bot执行的命令的代码。这只是使用了
数据。查找

好的,从我看来,捕获异常是很自然的,不应该有害。每隔一段时间,服务器上就没有任何新内容可供提取,因此
数据
是一个空字符串。然而,一个更清晰的方法可能是(另外,我还随意重写了一些代码,我假设代码的最后一块也在里面,而这是真的):


我的评论根本不符合这一点

你为什么重新发布这个问题?第一次的时候还不清楚。可能是我的新发的重复。我不知道如何编辑这篇文章。我现在知道了。和。不。没有一个答案不起作用或者我不明白。@Dunno我在以前的测试中改变了这个,问题仍然存在。但是我会重试。Do
try:user=data.split(':')[1];除索引器外:打印数据
,然后查看打印的内容。对于那个评论,我也很抱歉,可能不是我编辑代码以匹配的问题。错误不会发生,相反,在正常情况下(20分钟左右),它会无休止地反复打印“没有从服务器获取任何信息”。我没有使程序崩溃,但它会断开我与twitch IRC服务器的连接。另外,感谢您的建议与布局和尝试…除了条款!关于这个断线有什么想法吗?这就是为什么找不到
user=user.split(“!”)[0]
的原因:它不再连接。我发现了问题!我对IRC的ping没有反应。因此它断开了我的连接。添加了此代码:
if data.find('PING')!=-1:irc.send(data.replace('PING','PONG'))
这应该可以修复它。谢谢你的帮助!我会不断更新结果。
queue = 0 #sets variable for anti-spam queue functionality

newsmsg = 'whitelist'

irc = socket.socket()
irc.connect((server, 6667)) #connects to the server

#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')

def queuetimer(): #function for resetting the queue every 30 seconds
    global queue
    print 'queue reset'
    queue = 0
    threading.Timer(30,queuetimer).start()
queuetimer()

while True:

    def message(msg): #function for sending messages to the IRC chat
        global queue
        queue = queue + 1
        print queue
        if queue < 20: #ensures does not send >20 msgs per 30 seconds.
            irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
        else:
            print 'Message deleted'

    def socialtimer(): #function for announcing social 
        global ntimer
        z = open('E:\mIRC\Twitter.txt')
        SOCIAL = z.read()
        message (SOCIAL)
        print 'Social Timers Started!'
        ntimer = threading.Timer(1200,socialtimer)
        ntimer.start()


data = irc.recv(1204) #gets output from IRC server
try: user = data.split(':')[1];
except IndexError: print (data)
user = user.split('!')[0] #determines the sender of the messages
print (data)
#It's good practice to define functions first, too keep definitions in one place

def queuetimer(): #function for resetting the queue every 30 seconds
    global queue
    print 'queue reset'
    queue = 0
    threading.Timer(30,queuetimer).start()

def message(msg): #function for sending messages to the IRC chat
    global queue
    queue = queue + 1
    print queue
    if queue < 20: #ensures does not send >20 msgs per 30 seconds.
        irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
    else:
        print 'Message deleted'

def socialtimer(): #function for announcing social 
    global ntimer
    z = open('E:\mIRC\Twitter.txt')
    SOCIAL = z.read()
    message (SOCIAL)
    print 'Social Timers Started!'
    ntimer = threading.Timer(1200,socialtimer)
    ntimer.start()

queue = 0 #sets variable for anti-spam queue functionality

newsmsg = 'whitelist'

irc = socket.socket()
irc.connect((server, 6667)) #connects to the server

#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')


queuetimer()

while True:
    data = irc.recv(1024) #gets output from IRC server, 1024 is a better number than 1204
    #make sure data isn't an empty string
    if data != '':
        user = data.split(':')[1]
        user = user.split('!')[0] #determines the sender of the messages
        print (data)
    else:
        print ("Nothing to get from the server")
try:
    #do something
except ExceptionName:
    #do something else
else:
    #do something if no exceptions occurred
finally:
    #do something even if an unhandled exception occurs and then rise it