From 038d375ae727afd24738d583b02653b5976d55e2 Mon Sep 17 00:00:00 2001 From: Noah Theus Date: Mon, 13 Oct 2025 17:02:20 +0200 Subject: [PATCH] refactor!: change config structure to be more readable --- botIO/authenticate.go | 5 +++-- config/config.go | 2 +- config/types.go | 34 +++++++++++++++++++++------------- deepL/translate.go | 4 ++-- main.go | 2 +- steam/api.go | 2 +- steam/profile.go | 4 ++-- 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/botIO/authenticate.go b/botIO/authenticate.go index eb856c8..ea61f2f 100644 --- a/botIO/authenticate.go +++ b/botIO/authenticate.go @@ -1,13 +1,14 @@ package botIO import ( + "watn3y/steamsalty/config" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/rs/zerolog/log" - "watn3y/steamsalty/config" ) func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) { - bot, err := tgbotapi.NewBotAPI(config.BotConfig.TelegramAPIToken) + bot, err := tgbotapi.NewBotAPI(config.BotConfig.Telegram.APIToken) if err != nil { log.Fatal().Err(err).Msg("Failed to authenticate to Telegram") } diff --git a/config/config.go b/config/config.go index b8af148..081d7d5 100644 --- a/config/config.go +++ b/config/config.go @@ -24,7 +24,7 @@ func LoadConfig() { } zerolog.SetGlobalLevel(zerolog.Level(BotConfig.LogLevel)) - BotConfig.TranslateLanguage = strings.ToUpper(BotConfig.TranslateLanguage) + BotConfig.Translate.Language = strings.ToUpper(BotConfig.Translate.Language) log.Info().Msg("Config loaded successfully") diff --git a/config/types.go b/config/types.go index c169b33..716a2e0 100644 --- a/config/types.go +++ b/config/types.go @@ -1,19 +1,27 @@ package config type config struct { - LogLevel int `env:"STEAMSALTY_LOGLEVEL" default:"1"` + LogLevel int `env:"STEAMSALTY_LOGLEVEL, default=1"` + + ChatID int64 `env:"STEAMSALTY_CHATID, required` + Watchers []uint64 `env:"STEAMSALTY_WATCHERS, required` + SleepInterval int `env:"STEAMSALTY_SLEEPINTERVAL, default=60"` + + Translate struct { + Enabled bool `env:"STEAMSALTY_TRANSLATE_ENABLED, default=False"` + Language string `env:"STEAMSALTY_TRANSLATE_LANGUAGE, default=EN-US"` + } + + Telegram struct { + APIToken string `env:"STEAMSALTY_TELEGRAM_APITOKEN, required"` + } + + Steam struct { + APIKey string `env:"STEAMSALTY_STEAMAPIKEY, required` + } - SleepInterval int `env:"STEAMSALTY_SLEEPINTERVAL" default:"60"` - ChatID int64 `env:"STEAMSALTY_CHATID" required:"true"` - Watchers []uint64 `env:"STEAMSALTY_WATCHERS" required:"true"` - TranslateEnabled bool `env:"STEAMSALTY_TRANSLATE_ENABLED" default:"false"` - TranslateLanguage string `env:"STEAMSALTY_TRANSLATE_LANGUAGE" default:"EN-US"` - TelegramAPIToken string `env:"STEAMSALTY_TELEGRAMAPITOKEN" required:"true"` - SteamAPIKey string `env:"STEAMSALTY_STEAMAPIKEY" required:"true"` - DeepL struct { - APIKey string `env:"STEAMSALTY_DEEPL_APIKEY"` - FreeTier bool `env:"STEAMSALTY_DEEPL_FREETIER" default:"true"` - } + APIKey string `env:"STEAMSALTY_DEEPL_APIKEY"` + FreeTier bool `env:"STEAMSALTY_DEEPL_FREETIER, default=True"` + } } - diff --git a/deepL/translate.go b/deepL/translate.go index 3cc0c4d..72beeda 100644 --- a/deepL/translate.go +++ b/deepL/translate.go @@ -38,7 +38,7 @@ func Translate(text string) (translatedText string, sourceLanguage string, err e log.Debug().Str("text", text).Msg("Starting translation") req := translateRequest{ Text: []string{text}, - TargetLang: config.BotConfig.TranslateLanguage, + TargetLang: config.BotConfig.Translate.Language, } body, err := json.Marshal(req) @@ -100,7 +100,7 @@ func getAndValidateLanguages() (err error) { TargetLanguages[l.Language] = l.Name } - if _, ok := TargetLanguages[config.BotConfig.TranslateLanguage]; !ok { + if _, ok := TargetLanguages[config.BotConfig.Translate.Language]; !ok { return fmt.Errorf("Selected language not supported by DeepL") } diff --git a/main.go b/main.go index b25c635..2476176 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ func main() { println("Starting SteamSalty...") configureLogger() config.LoadConfig() - if config.BotConfig.TranslateEnabled { + if config.BotConfig.Translate.Enabled { deepl.Init() } diff --git a/steam/api.go b/steam/api.go index f73f19b..5896f5e 100644 --- a/steam/api.go +++ b/steam/api.go @@ -9,7 +9,7 @@ import ( func GetPlayerDetails(steamID uint64) (summary steamapi.PlayerSummary) { - response, err := steamapi.GetPlayerSummaries([]uint64{steamID}, config.BotConfig.SteamAPIKey) + response, err := steamapi.GetPlayerSummaries([]uint64{steamID}, config.BotConfig.Steam.APIKey) if err != nil { log.Error().Err(err).Msg("Failed to retrive player summary") } diff --git a/steam/profile.go b/steam/profile.go index 93264fc..1c903ab 100644 --- a/steam/profile.go +++ b/steam/profile.go @@ -69,9 +69,9 @@ func watcher(bot *tgbotapi.BotAPI, steamID uint64, sleeptime time.Duration) { "
" + comment.Text + "
", } - if config.BotConfig.TranslateEnabled { + if config.BotConfig.Translate.Enabled { translatedText, translatedTextLanguage, err := deepl.Translate(comment.Text) - if translatedTextLanguage == config.BotConfig.TranslateLanguage { + if translatedTextLanguage == config.BotConfig.Translate.Language { continue } if err != nil {