Compare commits
5 Commits
a151b08142
...
04aef2f0e4
Author | SHA1 | Date | |
---|---|---|---|
04aef2f0e4 | |||
9221a218b9 | |||
a551a10e59 | |||
d8a28fb211 | |||
2ac0df3494 |
35
bot/bot.go
35
bot/bot.go
@ -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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user