Implement UserActive events

This change introduces the UserActiveTimer,  which tracks voice activity
and emits UserActive events.

UserActiveTimer is basically a fancy wrapper around a context with
a deadline and cancelFunc. When a user joins a voice channel, a
UserActiveTimer is started.

If the user stays in the voice channel for an amount of time defined in the configs,
the timer context's deadline trips and a UserActive event is emitted. A new timer is then started.

If instead the user leaves the voice channel, the timer's context is
cancelled.

This change introduces two config values to manage this process:

VoiceActivityThresholdSeconds defines the length of time a user is
required to stay in vc before a UserActive event is generated.

VoiceActivityTimerSleepInterval defines how long the timer sleeps at any
one time. After this interval, it wakes up to check if its context has
been cancelled.

This change also changes the state package's UserEvent validation to
remove an import loop. We will now assume that the discord package
has already validated any UIDs it passes along to the state package.
This commit is contained in:
Piper Pentagram 2024-11-13 16:32:58 -08:00
parent 8110037ddf
commit 9b00241d2b
5 changed files with 219 additions and 16 deletions

View file

@ -13,7 +13,6 @@ import (
"sync"
"time"
"gitlab.com/whom/bingobot/internal/discord"
"gitlab.com/whom/bingobot/internal/logging"
)
@ -359,20 +358,8 @@ func (ue UserEvent) Data() map[string]string {
}
func (ue UserEvent) Validate() error {
if discord.Connected() {
_, err := discord.User(ue.uid)
return err
} else {
// I would love to know how to actually fail here
// and still have unit testable code.
logging.Error(
"can't validate UserEvent: nil discord session",
"event",
fmt.Sprintf("%+v", ue),
)
return nil
}
// empty for now, we may do some validation later.
return nil
}
type ChallengeEvent struct {