I'm working on a simple IRC bot and I'm trying to create a timer highlight function.
When I enter the following:
!hl 10
I want to assign 10 (or whatever might be there in it's place) as a variable called 'var_time' in minutes.
var_time= 0 #External statement
def timer_commands(nick,channel,message):
global var_time
if message.find("!hl", var_time )!=-1:
ircsock.send('PRIVMSG %s :%s I will highlight you in %s minutes!\r\n' % (channel,nick, var_time))
time.sleep(float(var_time)) #The delay here is in seconds
ircsock.send('PRIVMSG %s :%s, You asked me %s minutes ago to highlight you.!\r\n' % (channel,nick,var_time))
I know var_time is not taking the value 10, that is precisely my question, how can I make that happen?
Here is how the function is called:
while 1:
ircmsg = ircsock.recv(2048) # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
ircraw = ircmsg.split(' ')
print(ircmsg) # Here we print what's coming from the server
if ircmsg.find(' PRIVMSG ')!=-1:
nick=ircmsg.split('!')[0][1:]
channel=ircmsg.split(' PRIVMSG ')[-1].split(':')[0]
timer_commands(nick,channel,ircmsg)
Thanks in advance.
Solution:
def timer_commands(nick,channel,message):
if ircraw[3] == ':!hl':
var_time = float(ircraw[4])
ircsock.send('PRIVMSG %s :I will highlight you in %s minutes!\r\n' % (channel, nick, ircraw[4]))
time.sleep(var_time*60) #Delay in Minutes
ircsock.send('PRIVMSG %s :%s pYou asked me %s minutes ago to highlight you.! \r\n' % (channel, nick, ircraw[4]))
Thanks Anonymous
try regular expressions:
>>> re.match('!hl ([0-9]+)$', '!hl 91445569').groups()[0]
'91445569'
>>> re.match('!hl ([0-9]+)$', '!hl 1').groups()[0]
'1'
or, in your code:
import re
m = re.match('!hl ([0-9]+)$', ircmsg)
if m is not None:
var_time = int(re.groups()[0])