-1

我一直在制作 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)

下面是我使用机器人的命令的代码。这只是使用data.find

4

1 回答 1

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

我根本无法将其放入我的评论中。链接到文档

于 2014-05-04T16:08:48.853 回答