Compare commits
No commits in common. "04aef2f0e44e4ee4bcdb419f6ade282f27b5ddfc" and "a151b081422ae331f02e4eb779545efef726fbe9" have entirely different histories.
04aef2f0e4
...
a151b08142
35
bot/bot.go
35
bot/bot.go
@ -76,8 +76,7 @@ func (b *Bot) RegisterHandlers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Run() error {
|
func Run() error {
|
||||||
initConfig()
|
setupConfig()
|
||||||
go reloadConfig()
|
|
||||||
|
|
||||||
if err := lib.SeedMathRand(); err != nil {
|
if err := lib.SeedMathRand(); err != nil {
|
||||||
log.Warn(err)
|
log.Warn(err)
|
||||||
@ -98,7 +97,8 @@ func Run() error {
|
|||||||
|
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||||
|
|
||||||
if err = dg.Open(); err != nil {
|
err = dg.Open()
|
||||||
|
if err != nil {
|
||||||
log.Fatalf("error opening connection: %v\n", err)
|
log.Fatalf("error opening connection: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +115,9 @@ func Run() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func setupConfig() {
|
||||||
|
var err error
|
||||||
|
|
||||||
C = NewConfig()
|
C = NewConfig()
|
||||||
|
|
||||||
viper.SetEnvPrefix("BEEPBOOP")
|
viper.SetEnvPrefix("BEEPBOOP")
|
||||||
@ -125,43 +127,22 @@ func initConfig() {
|
|||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
|
|
||||||
viper.SetDefault("debug", false)
|
viper.BindEnv("DEBUG")
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithField("filename", viper.ConfigFileUsed()).Info(
|
err = viper.Unmarshal(&C)
|
||||||
"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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user