Compare commits

..

5 Commits

View File

@ -76,7 +76,8 @@ func (b *Bot) RegisterHandlers() {
}
func Run() error {
setupConfig()
initConfig()
go reloadConfig()
if err := lib.SeedMathRand(); err != nil {
log.Warn(err)
@ -97,8 +98,7 @@ func Run() error {
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
err = dg.Open()
if err != nil {
if err = dg.Open(); err != nil {
log.Fatalf("error opening connection: %v\n", err)
}
@ -115,9 +115,7 @@ func Run() error {
return nil
}
func setupConfig() {
var err error
func initConfig() {
C = NewConfig()
viper.SetEnvPrefix("BEEPBOOP")
@ -127,22 +125,43 @@ func setupConfig() {
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.BindEnv("DEBUG")
viper.SetDefault("debug", false)
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
loadConfig()
}
func loadConfig() {
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
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 {
log.Fatalf("unable to decode into struct: %v", err)
}
if viper.GetBool("debug") {
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()
}
}