Skip to content

watchtime

orciument edited this page Nov 28, 2024 · 1 revision

Coins and Watchtime are staple features of most twitch bots. Watchtime is calculated with the time the user is in the viewer-list and is used a pretty close guess as to how long a user has watched the stream.

Coins are points that are given out periodically for watching the stream for a specific duration. These Coins can then be used to take part in giveaways. In our implementation Coins are only urned via watching the stream and cannot be obtained via some sort of payment through a donation, or some boost give out for a donation or subscription.

Counting Watchtime

Since twitch's shift to the new websocket based chat api, instead of the old irc api, join and leave event are no longer transmitted in realtime. The new solution is to query the full userlist from twitch.

Because of this requirement our system is based on a polling approach. Every minute (this interval can be configured) the userlist is queried and each user is added one minute of watchtime.

This Results in some error because the user could have joined the chat in between our polling cycles. That would mean that we would theoretically add in between 0 and close to one minute too much watchtime. (the upper error would always be the polling interval)

When a user leaves the chat we have the opposite problem. We would add to little watchtime because we can't know that the user still was X second in the chat, between our polling cycles.

That means that our error range is -1 min to +1 min. More generally: -POLLING_INTERVAL to +POLLING_INTERVAL. With the average error being relatively close to 0 (when we assume totally random error time distribution for join and leave)

This error range can be reduced by increasing the polling interval. But increasing the polling interval could result in the twitch rate limit being reached. To note is also that polling the viewerlist is not always one request, but scales linearly with every thousand viewers started.

Counting Coins

The coin counting system is very similar to the watchtime counting. A counter that tracks the amount of watchtime since the user received the last coins is increased by the polling interval. if this counter reaches or exceeds the specified time a amount of coins is added for this user, and this time counter is reset.

This results in X amount of coins being added to each user in the chat every Y Seconds.

The coins have the same error characteristics as the watchtime. Specific +/- deviations per hour change with the payout interval and amount.

Clone this wiki locally