我一直在制作Python Twitch IRC Bot,除了一个问题外,它都有效。大约每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消息之间的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)
下面是我使用bot的命令的代码。这只是使用data.find
答案 0 :(得分:0)
从我看到的情况来看,抓住这个异常非常自然,不应该是有害的。每隔一段时间,服务器上没有任何新的东西可以拉,因此data
是一个空字符串。但是,更明确的方法可能是(同样,我冒昧地重写了一些代码,我假设你的代码的最后一个块也在内部,而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
我根本无法在评论中说明这一点。 Link to documentation