Bind Discord and OpenWeatherMap tokens to environment variables

This commit is contained in:
Ryan Cavicchioni 2022-08-22 19:29:42 -05:00
parent d790094399
commit d75a02554d
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 9 additions and 12 deletions

View File

@ -22,8 +22,6 @@ import (
)
var (
Token string
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
C command.Config
@ -62,27 +60,23 @@ func main() {
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err)
}
Token, ok := viper.Get("discord_token").(string)
err = viper.Unmarshal(&C)
if err != nil {
log.Fatalf("unable to decode into struct: %v", err)
}
if Token == "" {
if C.DiscordToken == "" {
log.Fatalf("Discord token is not set")
}
if !ok {
log.Fatalf("Invalid type assertion")
}
dg, err := discordgo.New(fmt.Sprintf("Bot %s", Token))
dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken))
if err != nil {
log.Fatalf("error creating Discord session: %v\n", err)
}

View File

@ -10,8 +10,10 @@ var (
type (
Config struct {
Handler HandlerConfig `mapstructure:"handler"`
Prefix string `mapstructure:"prefix"`
Handler HandlerConfig `mapstructure:"handler"`
Prefix string `mapstructure:"prefix"`
DiscordToken string `mapstructure:"discord_token"`
OpenWeatherMapToken string `mapstructure:"open_weather_map_token"`
}
HandlerConfig struct {

View File

@ -4,3 +4,4 @@ services:
build: .
environment:
- BEEPBOOP_DISCORD_TOKEN
- BEEPBOOP_OPEN_WEATHER_MAP_TOKEN