TypeError: 'int' object has no attribute '__getitem__' - Error recieved in IRC weather bot in Python -
okay, i've got bit of python irc bot code, , added weather command it, doesn't seem work... heres code
# import necessary libraries. import socket import time import httplib def commands(nick,channel,message): if message.find('!test')!=-1: ircsock.send('privmsg %s :%s: test complete\r\n' % (channel,nick)) elif message.find('!help')!=-1: ircsock.send('privmsg %s :%s: other command test.\r\n' % (channel,nick)) elif message.find('!sex')!=-1: ircsock.send('privmsg %s :%s: why?\r\n' % (channel,nick)) elif message.find('!quit')!=-1: ircsock.send('quit :for bones of weak shall support me\r\n') die('quit command given') elif message.find('!op')!=-1: ircsock.send('mode %s +o :%s\n' % (channel,nick)) elif message.find('!deop')!=-1: ircsock.send('mode %s -o :%s\n' % (channel,nick)) elif message.find('!weather')!=-1: tmp = message.find(':!weather') city = tmp[1].strip() reqest_str = '/laika_zinas/?city=' + city c = httplib.httpconnection("www.1188.lv") c.request("get", reqest_str) ra = c.getresponse() datas = ra.read() temp, wind = tpars(datas) ircsock.send('privmsg %s :%s: [+] temp: '+ temp +' c | wind: '+ wind +' m/s' % (channel,nick)) c.close() # basic variables used configure bot server = "n0cht.bawx.net" # server channel = "#python" # channel botnick = "pyledrivr" # bots nick def ping(ircmsg): # our first function! respond server pings. ircsock.send("pong "+ ircmsg +"\n") print("ping replied\n\r") def sendmsg(chan , msg): # send message function, sends messages channel. ircsock.send("privmsg "+ chan +" :"+ msg +"\n") def joinchan(chan): # function used join channels. ircsock.send("join "+ chan +"\n") ircsock = socket.socket(socket.af_inet, socket.sock_stream) ircsock.connect((server, 6667)) # here connect server using port 6667 ircsock.send("user "+ botnick +" "+ botnick +" "+ botnick +" :pyledrivr\n") # user authentication ircsock.send("nick "+ botnick +"\n") # here assign nick bot joinchan(channel) # join channel using functions defined while 1: # careful these! might send infinite loop ircmsg = ircsock.recv(2048) # receive data server ircmsg = ircmsg.strip('\n\r') # removing unnecessary linebreaks. print(ircmsg) # here print what's coming server if ircmsg.find(' privmsg ')!=-1: nick=ircmsg.split('!')[0][1:] channel=ircmsg.split(' privmsg ')[-1].split(' :')[0] commands(nick,channel,ircmsg) if ircmsg.find("ping :") != -1: # if server pings we've got respond! ping(ircmsg)
now, when run bot, works fine, happens when issue command:
<wh0r3[mint]> !weather 99654 * pyledrivr has quit (client exited)
and here's term shows:
:wh0r3[mint]!~wh0r3@n0cht-d1d272d.gci.net privmsg #lobby :!weather 99654 traceback (most recent call last): file "pyledrivr.py", line 65, in <module> commands(nick,channel,ircmsg) file "pyledrivr.py", line 22, in commands city = tmp[1].strip() typeerror: 'int' object has no attribute '__getitem__'
i have no idea means or how fix it. , ideas?
this line:
tmp = message.find(':!weather')
assigns integer tmp
: position @ string ':!weather'
found in message
(see find
). may not found @ all, since check in line above '!weather'
in message
, not ':!weather'
.
then try , access tmp[1]
. tmp
number; doesn't have [1]
.
if want substring of message follows '!weather'
, this:
city = message[message.find('!weather')+8:].strip()
(8 being length of '!weather'
)
or might find easier use split
:
city = message.split('!weather')[1].strip()
Comments
Post a Comment