bingobot/internal/discord/handlers.go
Piper Pentagram 9b00241d2b 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.
2024-11-14 10:42:02 -08:00

36 lines
894 B
Go

package discord
import (
"github.com/bwmarrin/discordgo"
"gitlab.com/whom/bingobot/internal/logging"
)
func addHandlers() {
session.s.AddHandler(handleConnect)
session.s.AddHandler(handleDisconnect)
session.s.AddHandler(handleVoiceStateUpdate)
}
func handleConnect(s *discordgo.Session, e *discordgo.Connect) {
session.connected = true
logging.Info("discord session connected")
}
func handleDisconnect(s *discordgo.Session, e *discordgo.Disconnect) {
session.connected = false
logging.Info("discord session disconnected")
}
func handleVoiceStateUpdate(_ *discordgo.Session, e *discordgo.VoiceStateUpdate) {
if e.ChannelID == "" {
// user disconnected
logging.Info("user left channel", "uid", e.UserID)
stopActivityTimer(e.UserID)
return
}
// user connected
logging.Info("user joined channel", "uid", e.UserID, "channel", e.ChannelID)
startActivityTimer(e.UserID)
}