【问题标题】:Print time in terminal, and update time every 30 seconds终端打印时间,每30秒更新一次
【发布时间】:2020-03-04 15:41:51
【问题描述】:

我正在使用discord.py 和 python 3.7 编写一个不和谐的机器人。我有一个循环来改变机器人的活动,每 30 秒打印一次终端中的当前时间和终端中的当前 ping。但问题是它没有打印更新的时间。如果我在 15:23:52 启动机器人,机器人将在接下来的几个小时内始终打印 15:23:52。它不更新时间。

import discord, datetime, time
import datetime as DT

time_date_now = DT.datetime.now().strftime('Date: %d-%m-%Y\nTime: %H:%M:%S')

*****

@tasks.loop(seconds=30)
async def change_status():
   await client.change_presence(activity=discord.Game(f'Ping: {round(client.latency * 1000)}ms'))
   print(time_date_now)
   print(f'Ping: {round(client.latency * 1000)}ms\n---------------------------')

如何正确更新打印时间?

【问题讨论】:

  • 您只计算了一次time_date_now。将该行移到任务中。

标签: python-3.x discord.py-rewrite


【解决方案1】:

变量time_date_now 仅在脚本开始时声明和分配一次。如果您将那个内部移动到您的任务中,您会看到它在工作。

【讨论】:

    猜你喜欢
    • 2016-11-07
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多