Python IRC Bot:从通道读取设置变量

时间:2015-08-29 16:33:38

标签: python sockets bots irc timedelay

我正在研究一个简单的IRC机器人,我正在尝试创建一个计时器突出显示功能。

当我输入以下内容时:

  

!HL   10

我想在分钟中将10(或其中可能存在的任何内容)分配为名为'var_time'的变量。

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))

我知道 var_time 没有取值10,这正是我的问题,我该如何实现呢?

以下是调用函数的方法:

  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)

提前致谢。

解决方案:

  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]))

感谢匿名

1 个答案:

答案 0 :(得分:1)

尝试正则表达式:

>>> re.match('!hl ([0-9]+)$', '!hl 91445569').groups()[0]
'91445569'
>>> re.match('!hl ([0-9]+)$', '!hl 1').groups()[0]
'1'

或者,在您的代码中:

import re
m = re.match('!hl ([0-9]+)$', ircmsg)
if m is not None:
    var_time = int(re.groups()[0])