我的Python Twisted IRC机器人响应命令
大家好,我正在用Python的Twisted库制作一个IRC机器人,基本功能都已经搭建好了,但这个机器人对命令的响应不太如我所愿。比如说,我想在IRC频道里调用一个机器人命令,我希望能直接输入$time,然后机器人就能回复我现在的时间。现在我能做到的是输入crazybot $time,然后它会打印出时间,但我不想每次都要输入机器人的名字……我该怎么让机器人在不输入名字的情况下直接执行命令呢?
这是我的更新——一切都连接好了……
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()
……当我输入!time时,没有错误,也没有输出……
1 个回答
0
你可以对MyFirstIrcBot
进行一些修改:
把代码中的!
替换成$
,具体在:
if not message.startswith('!'): # not a trigger command
return # do nothing
command, sep, rest = message.lstrip('!').partition(' ')
然后添加:
from datetime import datetime
# ...
def command_time(self, rest):
return datetime.utcnow().isoformat()