-1

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

Mr.Unknown21
  • 35
  • 1
  • 6
  • Why did you re-post this question? It wasn't clear the first time around. – Martijn Pieters May 03 '14 at 21:20
  • possible duplicate of [Python Split Issue: Index out of range?](http://stackoverflow.com/questions/23447951/python-split-issue-index-out-of-range) – Martijn Pieters May 03 '14 at 21:20
  • I re-posted do to my newness. I didn't understand how to edit the post. I do now. And. No. None of the answers either didn't work or i didn't understand. – Mr.Unknown21 May 03 '14 at 21:33
  • @Dunno I have changed this in previous testing and the issue still occurred. I will retry though. – Mr.Unknown21 May 03 '14 at 21:35
  • Do `try: user = data.split(':')[1]; except IndexError: print data` and see what gets printed. Also sorry about that comment, probably wasn't an issue – Dunno May 03 '14 at 21:36
  • @Dunno I have been running the code now for just under an hour with no issues! Seems to have worked! Thanks a bunch! :P Hopefully it will continue to work – Mr.Unknown21 May 03 '14 at 22:19
  • @Geekster_Alan it doesn't fix the issue, it just silences the error and prints data to help you with debugging. – Dunno May 03 '14 at 22:33
  • @Dunno Noticed that now. I didn't think it would but it seemed to. I haven't received the error, but it is printing the data, which is blank. It just starts to spam lines with nothing in. I will wait until it next does it and i will type in my IRC to see if that is in fact what is being printed. Does this give you any clues. – Mr.Unknown21 May 03 '14 at 22:45
  • @Geekster_Alan when and how is this code called? You're probably just reading from server when there's nothing to read and get an empty string. – Dunno May 04 '14 at 10:52
  • @Dunno the code is called after connecting to the IRC. I have edited the original question to show more of the code, to when and how it is called. It strange as it works for around 20 mins before the error occurs. – Mr.Unknown21 May 04 '14 at 15:05

1 Answers1

0

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

Dunno
  • 3,632
  • 3
  • 28
  • 43
  • I edited the code to match. The error doesn't occur, instead at the point it normally would (20 or so mins in) it prints "Nothing to get from the server" over and over endlessly. I doesn't crash the program, but it disconnects me from the twitch IRC servers. Also, thanks for the advise with the layout and try....except clauses! any ideas about this disconnection? This would be why the `user = user.split('!')[0]` cannot be found: its not connected anymore. – Mr.Unknown21 May 04 '14 at 16:45
  • I found the issue! I wasn't responding to the Pings from the IRC. Therefore it disconnected me. added this code: `if data.find('PING') != -1: irc.send(data.replace('PING', 'PONG'))` This should fix it. Thanks for all the help! I will keep updated on results. – Mr.Unknown21 May 04 '14 at 16:51