我一直在制作一个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
好吧,从我看来,捕捉到这个异常是很自然的,不应该是有害的。每隔一段时间,服务器上没有任何新内容可供提取,因此
data
是一个空字符串。然而,一个更清晰的方法可能是(另外,我随意重写了一些代码,我假设您的代码的最后一个块也在里面,虽然是真的):顺便说一下,
^{pr2}$try...except
子句的正确语法是我只是不能在我的评论中提到这一点。Link to documentation
相关问题 更多 >
编程相关推荐