mirror of
https://github.com/watn3y/steamsalty.git
synced 2025-10-25 09:51:02 +02:00
refactor!: change config structure to be more readable
This commit is contained in:
parent
f17601c470
commit
038d375ae7
7 changed files with 31 additions and 22 deletions
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -1,19 +1,27 @@
|
|||
package config
|
||||
|
||||
type config struct {
|
||||
LogLevel int `env:"STEAMSALTY_LOGLEVEL" default:"1"`
|
||||
LogLevel int `env:"STEAMSALTY_LOGLEVEL, default=1"`
|
||||
|
||||
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"`
|
||||
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`
|
||||
}
|
||||
|
||||
DeepL struct {
|
||||
APIKey string `env:"STEAMSALTY_DEEPL_APIKEY"`
|
||||
FreeTier bool `env:"STEAMSALTY_DEEPL_FREETIER" default:"true"`
|
||||
FreeTier bool `env:"STEAMSALTY_DEEPL_FREETIER, default=True"`
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
|
|||
2
main.go
2
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()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,9 +69,9 @@ func watcher(bot *tgbotapi.BotAPI, steamID uint64, sleeptime time.Duration) {
|
|||
"<blockquote>" + comment.Text + "</blockquote>",
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue