Search code examples
pythonbotstwitch

Testing how long a user has been in a list


I'm using a bot for twitch and it tracks the time the user has spent in the channel. !time You have spent 10 seconds in the stream. However when multiple users use this command !time it doesn't have a seperate 'count' for each user. Ex:

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.

My current code:

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.")

Solution

  • You'll want to store startTime associated with a specific user, something like

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

    which uses a dictionary to look up the individual's startTime.

    To store it originally (in !start):

    userStart[user] = time.time()