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.
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type AppConfig struct {
|
|
LogFile string `yaml:"log_file"`
|
|
LogDir string `yaml:"log_dir"`
|
|
LogMaxSizeMB int `yaml:"log_max_size_mb"`
|
|
LogMaxBackups int `yaml:"log_max_backups"`
|
|
LogMaxAgeDays int `yaml:"log_max_age_days"`
|
|
LogCompression bool `yaml:"log_compression"`
|
|
LogAddSource bool `yaml:"log_add_source"`
|
|
|
|
/*
|
|
how long (in seconds) a user needs to be in vc in order to generate a
|
|
UserActive event
|
|
*/
|
|
VoiceActivityThresholdSeconds int `yaml:"voice_activity_threshold_seconds"`
|
|
|
|
/*
|
|
how long (in milliseconds) a voice activity timer sleeps at a time between
|
|
context cancellation checks.
|
|
|
|
a higher value means the function sleeps longer which could be
|
|
useful for some reason in the future
|
|
|
|
a higher value also means that the timer could take longer to cancel.
|
|
|
|
current recommended value is 1000ms.
|
|
*/
|
|
VoiceActivityTimerSleepIntervalMillis int `yaml:"voice_activity_timer_sleep_interval_millis"`
|
|
}
|
|
|
|
var config *AppConfig
|
|
|
|
func init() {
|
|
setDefaults()
|
|
viper.Unmarshal(&config)
|
|
}
|
|
|
|
func Get() *AppConfig {
|
|
return config
|
|
}
|
|
|
|
func GetDefaultConfig() *AppConfig {
|
|
var config *AppConfig
|
|
setDefaults()
|
|
viper.Unmarshal(&config)
|
|
return config
|
|
}
|
|
|
|
func Init() error {
|
|
setDefaults()
|
|
|
|
// automatically populate config with any matching environment vars
|
|
viper.SetEnvPrefix("BB")
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(".")
|
|
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("error reading configs: %s", err)
|
|
}
|
|
|
|
err = viper.Unmarshal(&config)
|
|
if err != nil {
|
|
return fmt.Errorf("error unmarshaling configs: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func setDefaults() {
|
|
viper.SetDefault("LogFile", "bingobot.log")
|
|
viper.SetDefault("LogDir", "log")
|
|
viper.SetDefault("LogMaxSizeMB", 500)
|
|
viper.SetDefault("LogMaxBackups", 3)
|
|
viper.SetDefault("LogMaxAgeDays", 365)
|
|
viper.SetDefault("LogCompression", false)
|
|
viper.SetDefault("LogAddSource", true)
|
|
viper.SetDefault("VoiceActivityThresholdSeconds", 600)
|
|
viper.SetDefault("VoiceActivityTimerSleepIntervalMillis", 1000)
|
|
}
|