Python:索引器错误:列表索引超出IRC B的范围

2024-10-03 06:25:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在制作一个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还是个新手,做了我所知道的一切。这是包含问题的代码区。在

^{pr2}$

这是从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


Tags: thetosendfordataserverqueuedef
1条回答
网友
1楼 · 发布于 2024-10-03 06:25:54

好吧,从我看来,捕捉到这个异常是很自然的,不应该是有害的。每隔一段时间,服务器上没有任何新内容可供提取,因此data是一个空字符串。然而,一个更清晰的方法可能是(另外,我随意重写了一些代码,我假设您的代码的最后一个块也在里面,虽然是真的):

#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子句的正确语法是

^{pr2}$

我只是不能在我的评论中提到这一点。Link to documentation

相关问题 更多 >