I have been making a Python Twitch IRC Bot and it all works besides one issue. About every 20 mins it will crash with the error:
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
I have researched this and tried several things. But to not prevail. I am still very new to python, and done everything i know of. This is the code area containing the issue.
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
Its the point the incoming data from the IRC gets split. the ':' is the split because this is what is used in the IRC between the Nickname and IRC Message. But. Even without use, it doesn't crash for around 20 mins. With use, it does the same. No crash until after 20 mins.
Any Ideas?
UPDATE
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)
Below this is code for the commands i use the bot for. This just uses data.find
Ok from what I see, catching that exception is quite natural and shouldn't be harmful. Once every while, there isn't anything new on the server to pull and so data
is an empty string. However, a clearer way to do this might be (also, I took liberty of rewriting some of your code and I'm assuming the last block of your code is also inside 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")
By the way, correct syntax for try...except
clause is
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
I simply couldn't fit that in my comment. Link to documentation