Documentación / Developers / Cookbook / 15) Voice channel time tracker
BuildSDK 0.10.1

15) Voice channel time tracker

These docs are public. To publish a plugin you need a YourBot account: sign in to open the Dev Portal.

Track how long each user spends in voice channels. Useful for activity-based rewards.

from yourbot_sdk.events import VoiceStateUpdate
import time

@plugin.on_event("voice_state_update")
def track(ctx: Context, event: VoiceStateUpdate):
    uid = event["user_id"]
    if event.get("after_channel_id") and not event.get("before_channel_id"):
        # joined voice
        ctx.kv.set(f"voice_in:{uid}", str(int(time.time())))
    elif event.get("before_channel_id") and not event.get("after_channel_id"):
        # left voice
        join_t = ctx.kv.get(f"voice_in:{uid}")
        if join_t:
            duration = int(time.time()) - int(join_t)
            ctx.kv.increment(f"voice_secs:{uid}", amount=duration)
            ctx.kv.delete(f"voice_in:{uid}")
YourBot docs Reference tables are generated from the code that is running. Ask in Discord Suggest a correction