测试用户在lis中的时间

2024-09-30 08:28:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个用于twitch的机器人,它跟踪用户在频道中花费的时间。!time You have spent 10 seconds in the stream。但是,当多个用户使用此命令!time时,每个用户没有单独的“计数”。例如:

Rustie: !time
Bot: Rustie, you have 20 seconds in the stream.
~1 minute later~
John: !time
Bot: John, you have 1 minute 20 seconds in the stream.

我的当前代码:

usersForTime = []

if "time" in message:
    if user in usersForTime:
        endTime = time.time() # I already made startTime in the !start command (which starts the time tracker in the first place)
        ellapsed = (endTime - startTime)
        sendMessage(s, user + ", you have " + "%.2f" % round(ellapsed, 2) + " seconds in the stream.")
    else:
        sendMessage(s ,"You need to start tracking your time with the !start command.")

Tags: the用户inyoustreamtimehavebot
1条回答
网友
1楼 · 发布于 2024-09-30 08:28:40

您需要存储与特定用户关联的startTime,例如

userStart = {}

if user in usersForTime:
  ...
  ellapsed = (endTime - userStart[user])

它使用字典来查找个人的开始时间。你知道吗

要最初存储它(在!start):

userStart[user] = time.time()

相关问题 更多 >

    热门问题