2012-01-26 36 views
0

你好人我正在用python构建一个irc bot扭曲,所有东西都是内置的,但机器人不会响应像我想要的命令。例如,如果我想在irc通道上调用一个bot命令,我希望能够像这样$ time调用它,并让bot回复它是什么时间,那么我可以像这样工作 - > crazybot $时间和它打印的时间,但我不想每次输入名称...我怎么让机器人运行命令,而不先调用名称? 这里是更新 - >连接一切....... 我的python扭曲的irc bot响应命令

def privmsg(self, user, channel, msg): 
    user = user.split('!', 1)[0] 

    if not msg.startswith('#'): # not a trigger command 
     return # do nothing 
    command, sep, rest = msg.lstrip('#').partition(' ') 
    func = getattr(self, 'command_' + command, None) 

def command_time(self, *args): 
    return time.asctime() 

.... 当我输入的时间,没有错误,没有输出..

+0

您是否开始使用现有项目?还是你从头开始写这个? – sberry

+0

http://sscce.org/或者你不会得到有用的答案。 –

回答

0

您可以修改! MyFirstIrcBot

通过$在更换!

if not message.startswith('!'): # not a trigger command 
    return # do nothing 
command, sep, rest = message.lstrip('!').partition(' ') 

Add:

from datetime import datetime 

# ... 
def command_time(self, rest): 
    return datetime.utcnow().isoformat() 
+0

谢谢你的回复,但它仍然没有工作:(它没有给出任何错误消息,虽然 – nprezident

+0

@nprezident:[更新你的问题](http://stackoverflow.com/posts/9014630/edit)和帖子[一个最小完整的例子](http://sscce.org/),它显示了Jean-Paul Calderone问的错误。 – jfs