Python irc bot输入

时间:2014-08-04 09:12:18

标签: python irc

我一直在研究irc机器人并且已经停止了,我无法弄清楚如何接受函数参数的输入,特别是我的函数add(),继承函数,

def add(x1,x2):
    xtot = x1 + x2
    ircsock.send("PRIVMSG "+ channel + " :Toatal =  %s.\n" % (xtot))

和heres怎么称呼

if ircmsg.find('add.' + botnick) != -1:
    add()

我想知道如何在运行该函数的同时附加输入,如add.mybot 1,2 而不仅仅是add.mybot

1 个答案:

答案 0 :(得分:0)

我不知道您正在使用哪个库,但我认为ircmsg包含输入(否则,请用ircmsg.line / ircmsg.input / ...替换ircmsg。

import re
if ircmsg.find('add.' + botnick) != -1:  #you find a line with add.mybot
    try:
        (x,y) = [int(i) for i in re.match(".*\((.*),(.*)\).*",a).groups()] #we try to retrieve what we want
        add(x, y)
    except:
        pass # or some logs

但是,我认为转换不应该在这里完成,而应该在你的add函数中完成(+如果你希望能够添加例如浮点数)。

例如:

import re
if ircmsg.find('add.' + botnick) != -1:  #you find a line with add.mybot
    try:
        (x,y) = re.match(".*\((.*),(.*)\).*",a).groups() #we try to retrieve what we want
        add(x, y)
    except:
        pass # or some logs

def add(x1,x2):
    if x1.isdigit() and x2.isdigit():
        xtot = int(x1) + int(x2)
        ircsock.send("PRIVMSG "+ channel + " :Toatal =  %s.\n" % (xtot))
    else:
        try:
            xtot = float(x1) + float(x2)
            ircsock.send("PRIVMSG "+ channel + " :Toatal =  %s.\n" % (xtot))
        except:
            pass #or send the concatenation x1+x2

希望我没有错。

相关问题