From c05eaf17449ce4d1b293171a6809289485df304e Mon Sep 17 00:00:00 2001 From: 0xCA Date: Fri, 29 Dec 2023 01:35:50 +0500 Subject: [PATCH] Fixed variable name verbosity --- main.go | 6 +++--- telegram/bot.go | 44 ++++++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index d4771c3..c3f03f1 100644 --- a/main.go +++ b/main.go @@ -140,9 +140,9 @@ func init() { lvl, _ := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO")) - telegram.TelegramToken = flagTelegramToken - telegram.TelegramAllowConfRequest = flagTelegramAllowConfRequest - telegram.TelegramFloodWait = flagTelegramFloodWait + telegram.Token = flagTelegramToken + telegram.AllowConfRequest = flagTelegramAllowConfRequest + telegram.FloodWait = flagTelegramFloodWait telegram.LogLevel = lvl // print only if log level is INFO or lower diff --git a/telegram/bot.go b/telegram/bot.go index 9ba0ece..f42d9cd 100644 --- a/telegram/bot.go +++ b/telegram/bot.go @@ -18,13 +18,13 @@ type TgBotInitDependencies struct { } var ( - TelegramToken string - TelegramAllowConfRequest bool - TelegramFloodWait int - LogLevel log.Lvl + Token string + AllowConfRequest bool + FloodWait int + LogLevel log.Lvl - TgBot *echotron.API - TgBotMutex sync.RWMutex + Bot *echotron.API + BotMutex sync.RWMutex floodWait = make(map[int64]int64, 0) ) @@ -32,16 +32,16 @@ var ( func Start(initDeps TgBotInitDependencies) (err error) { ticker := time.NewTicker(time.Minute) defer func() { - TgBotMutex.Lock() - TgBot = nil - TgBotMutex.Unlock() + BotMutex.Lock() + Bot = nil + BotMutex.Unlock() ticker.Stop() if r := recover(); r != nil { err = fmt.Errorf("[PANIC] recovered from panic: %v", r) } }() - token := TelegramToken + token := Token if token == "" || len(token) < 30 { return } @@ -54,9 +54,9 @@ func Start(initDeps TgBotInitDependencies) (err error) { return } - TgBotMutex.Lock() - TgBot = &bot - TgBotMutex.Unlock() + BotMutex.Lock() + Bot = &bot + BotMutex.Unlock() if LogLevel <= log.INFO { fmt.Printf("[Telegram] Authorized as %s\n", res.Result.Username) @@ -68,7 +68,7 @@ func Start(initDeps TgBotInitDependencies) (err error) { } }() - if !TelegramAllowConfRequest { + if !AllowConfRequest { return } @@ -78,7 +78,7 @@ func Start(initDeps TgBotInitDependencies) (err error) { userid := update.Message.Chat.ID if _, wait := floodWait[userid]; wait { bot.SendMessage( - fmt.Sprintf("You can only request your configs once per %d minutes", TelegramFloodWait), + fmt.Sprintf("You can only request your configs once per %d minutes", FloodWait), userid, &echotron.MessageOptions{ ReplyToMessageID: update.Message.ID, @@ -106,15 +106,15 @@ func Start(initDeps TgBotInitDependencies) (err error) { } func SendConfig(userid int64, clientName string, confData, qrData []byte, ignoreFloodWait bool) error { - TgBotMutex.RLock() - defer TgBotMutex.RUnlock() + BotMutex.RLock() + defer BotMutex.RUnlock() - if TgBot == nil { + if Bot == nil { return fmt.Errorf("telegram bot is not configured or not available") } if _, wait := floodWait[userid]; wait && !ignoreFloodWait { - return fmt.Errorf("this client already got their config less than %d minutes ago", TelegramFloodWait) + return fmt.Errorf("this client already got their config less than %d minutes ago", FloodWait) } if !ignoreFloodWait { @@ -122,14 +122,14 @@ func SendConfig(userid int64, clientName string, confData, qrData []byte, ignore } qrAttachment := echotron.NewInputFileBytes("qr.png", qrData) - _, err := TgBot.SendPhoto(qrAttachment, userid, &echotron.PhotoOptions{Caption: clientName}) + _, err := Bot.SendPhoto(qrAttachment, userid, &echotron.PhotoOptions{Caption: clientName}) if err != nil { log.Error(err) return fmt.Errorf("unable to send qr picture") } confAttachment := echotron.NewInputFileBytes(clientName+".conf", confData) - _, err = TgBot.SendDocument(confAttachment, userid, nil) + _, err = Bot.SendDocument(confAttachment, userid, nil) if err != nil { log.Error(err) return fmt.Errorf("unable to send conf file") @@ -138,7 +138,7 @@ func SendConfig(userid int64, clientName string, confData, qrData []byte, ignore } func updateFloodWait() { - thresholdTS := time.Now().Unix() - 60*int64(TelegramFloodWait) + thresholdTS := time.Now().Unix() - 60*int64(FloodWait) for userid, ts := range floodWait { if ts < thresholdTS { delete(floodWait, userid)