Compare commits

...

5 Commits

View File

@ -76,7 +76,8 @@ func (b *Bot) RegisterHandlers() {
} }
func Run() error { func Run() error {
setupConfig() initConfig()
go reloadConfig()
if err := lib.SeedMathRand(); err != nil { if err := lib.SeedMathRand(); err != nil {
log.Warn(err) log.Warn(err)
@ -97,8 +98,7 @@ func Run() error {
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
err = dg.Open() if err = dg.Open(); err != nil {
if err != nil {
log.Fatalf("error opening connection: %v\n", err) log.Fatalf("error opening connection: %v\n", err)
} }
@ -115,9 +115,7 @@ func Run() error {
return nil return nil
} }
func setupConfig() { func initConfig() {
var err error
C = NewConfig() C = NewConfig()
viper.SetEnvPrefix("BEEPBOOP") viper.SetEnvPrefix("BEEPBOOP")
@ -127,22 +125,43 @@ func setupConfig() {
viper.SetConfigType("toml") viper.SetConfigType("toml")
viper.AddConfigPath(".") viper.AddConfigPath(".")
viper.BindEnv("DEBUG") viper.SetDefault("debug", false)
viper.BindEnv("DISCORD_TOKEN") viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN") viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
loadConfig()
}
func loadConfig() {
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok { if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err) log.Fatalf("fatal error config file: %v", err)
} }
} }
err = viper.Unmarshal(&C) log.WithField("filename", viper.ConfigFileUsed()).Info(
"loaded configuration file",
)
err := viper.Unmarshal(&C)
if err != nil { if err != nil {
log.Fatalf("unable to decode into struct: %v", err) log.Fatalf("unable to decode into struct: %v", err)
} }
if viper.GetBool("debug") { if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
}
func reloadConfig() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP)
for {
<-sc
loadConfig()
} }
} }