Python: IndexError: 列表索引超出范围,IRC Bot
我正在制作一个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
我查阅了很多资料,尝试了几种方法,但都没有解决这个问题。我对Python还是很陌生,做了我能想到的所有事情。这段代码是出问题的地方。
data = irc.recv(1204) #gets output from IRC server
user = data.split(':')[1]
user = user.split('!')[0] #determines the sender of the messages
print data
问题出在接收到的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)
下面是我用这个机器人执行命令的代码。这段代码只是使用了data.find
。
1 个回答
0
根据我看到的,捕捉这个异常是很自然的,应该不会有什么坏处。偶尔服务器上没有新的数据可以获取,所以data
会是一个空字符串。不过,有一种更清晰的做法可能是(另外,我稍微改写了一下你的代码,并假设你最后的代码块也是在while True里面):
#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...except
的正确写法是
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
我实在没法把这个放进我的评论里。这是文档链接