Python IRC Bot:设置来自频道阅读的变量

Python IRC Bot: Set variables from channel reading

我正在开发一个简单的 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]))

感谢匿名

尝试正则表达式:

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