Chatango改变时间

时间:2014-07-03 07:52:49

标签: python datetime

运行chatango机器人 我正试图让机器人打印出来:
(in order) time, name, access level, body.

例:
01:02:03 Jan 7th 2014 - YouColdYet(4): Something Something Something

当前代码:

def onMessage(self, room, user, message):
    print(user.name+":"+message.body)
    print("User Access Level: %s" % self.rank(user))

我不喜欢多行,因为它占用空间。 self.rank来自访问模块,因此它可以工作,它只是让它与其他人合作,在同一行上进行PRINT。

我能够使用时间模块asc来处理它,但它不会计算每条消息,只是在同一时间发布。所以它看起来像:

youcoldyet: whatever
User Access Level: 4
Current Time: Always the same
youcoldyet: something something
User Access Level: 4
Current Time: Always the same

欢迎所有反馈。

1 个答案:

答案 0 :(得分:1)

我认为你想要的是:

from datetime import datetime 

def on_message(self, room, user, message):
    template = "{0} - {1.name}({1.rank}): {2.body}"
    timetemp = "%H:%M:%S %b %d %Y"
    print(template.format(datetime.strftime(datetime.now(), # or 'message.time'?
                                            timetemp), 
                          user, message))

使用虚拟对象的演示:

>>> class Dummy(object):
    pass

>>> user = Dummy()
>>> user.name = "YouColdYet"
>>> user.rank = 4
>>> message = Dummy()
>>> message.body = "Something Something Something"
>>> on_message(None, None, user, message)
10:53:48 Jul 03 2014 - YouColdYet(4): Something Something Something