From d9d82814ea518dc38a9da17940c6fe436b3a68d7 Mon Sep 17 00:00:00 2001 From: Santiago Ramirez Date: Tue, 19 Dec 2023 16:15:21 +0000 Subject: [PATCH] refactor: some stuff --- kreacher/client.go | 25 +++++++++++++++++-------- kreacher/logger.go | 16 +++++++++------- kreacher/main.go | 25 ++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/kreacher/client.go b/kreacher/client.go index 9dffece..1cd8af5 100755 --- a/kreacher/client.go +++ b/kreacher/client.go @@ -1,22 +1,31 @@ package kreacher import ( + "log" + td "github.com/gotd/td/telegram" redis "github.com/redis/go-redis/v9" tele "gopkg.in/telebot.v3" ) type Kreacher struct { - RedisDB *redis.Client - KreacherBot *tele.Bot - UserBot *td.Client + Logger *log.Logger + Bot *tele.Bot + UserBot *td.Client + RedisDB *redis.Client } -func NewKreacher(redisDB *redis.Client, kreacherBot *tele.Bot, userBot *td.Client) *Kreacher { +func NewKreacher( + logger *log.Logger, + kreacherBot *tele.Bot, + userBot *td.Client, + redisDB *redis.Client, +) *Kreacher { + return &Kreacher{ - RedisDB: redisDB, - KreacherBot: kreacherBot, - UserBot: userBot, + Logger: logger, + Bot: kreacherBot, + UserBot: userBot, + RedisDB: redisDB, } - } diff --git a/kreacher/logger.go b/kreacher/logger.go index 3f93f93..7ba6620 100644 --- a/kreacher/logger.go +++ b/kreacher/logger.go @@ -2,18 +2,20 @@ package kreacher import ( "log" - "log/syslog" + "os" ) -func NewLogger() *log.Logger { +func NewLogger(name string, path string) (*log.Logger, error) { var logger *log.Logger - - syslogWriter, err := syslog.New(syslog.LOG_INFO, "my-app") + file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) if err != nil { - log.Fatal(err) + return nil, err } - logger.SetOutput(syslogWriter) + + defer file.Close() + + logger.SetOutput(file) logger.SetFlags(0) - return logger + return logger, nil } diff --git a/kreacher/main.go b/kreacher/main.go index 9f4fb10..75e03e7 100755 --- a/kreacher/main.go +++ b/kreacher/main.go @@ -10,6 +10,12 @@ import ( ) func main() { + logger, err := NewLogger("kreacher", "kreacher.log") + + if err != nil { + panic(err) + } + redisDB := redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", NewConfig().RedisHost, NewConfig().RedisPort), Password: NewConfig().RedisPassword, @@ -17,16 +23,29 @@ func main() { Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3 }) - kreacherBot, _ := tele.NewBot(tele.Settings{ + defer redisDB.Close() + + bot, err := tele.NewBot(tele.Settings{ Token: NewConfig().BotToken, Poller: &tele.LongPoller{Timeout: 10 * time.Second}, }) + if err != nil { + panic(err) + } + + defer bot.Close() + userBot := td.NewClient(NewConfig().APIID, NewConfig().APIHash, td.Options{}) - kreacher := NewKreacher(redisDB, kreacherBot, userBot) + kreacher := NewKreacher( + logger, + bot, + userBot, + redisDB, + ) - err := kreacher.KreacherBot.SetCommands([]tele.Command{ + err = kreacher.Bot.SetCommands([]tele.Command{ {Text: "config", Description: "Set the bot's configuration"}, {Text: "help", Description: "How to use this"}, {Text: "leave", Description: "Leave the voice chat"},