From 1a6953900c6e862874603bfc68b4694157cc2b34 Mon Sep 17 00:00:00 2001 From: watn3y Date: Fri, 29 Dec 2023 02:35:22 +0100 Subject: [PATCH] various improvements --- bot.go | 1 - botIO/authenticate.go | 13 ++++---- botIO/getinfo.go | 8 ++++- commands/commands.go | 2 ++ commands/download/download.go | 11 ++++--- commands/gaypoints/gaypoints.go | 2 +- commands/gaypoints/sql.go | 3 +- commands/notify/notify.go | 1 + commands/notify/sql.go | 3 +- config/config.go | 58 +++++++++++++++++---------------- main.go | 13 ++++++-- text/balonlyl/balonlyl.go | 1 + text/matcher.go | 2 +- webserver/webserver.go | 2 +- 14 files changed, 71 insertions(+), 49 deletions(-) diff --git a/bot.go b/bot.go index ed8d92f..9c46354 100644 --- a/bot.go +++ b/bot.go @@ -6,7 +6,6 @@ import ( "watn3y/bloaterbot/commands" "watn3y/bloaterbot/commands/notify" "watn3y/bloaterbot/text" - "github.com/rs/zerolog/log" ) diff --git a/botIO/authenticate.go b/botIO/authenticate.go index 2c7a1b1..80db0bc 100644 --- a/botIO/authenticate.go +++ b/botIO/authenticate.go @@ -2,25 +2,24 @@ package botIO import ( "watn3y/bloaterbot/config" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/rs/zerolog/log" ) func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) { - b, err := tgbotapi.NewBotAPI(config.BotConfig.APIToken) + bot, err := tgbotapi.NewBotAPI(config.BotConfig.APIToken) if err != nil { log.Panic().Err(err).Msg("Failed to authorize bot") } - b.Debug = config.BotConfig.DebugMode + bot.Debug = config.BotConfig.DebugMode - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 + updates := tgbotapi.NewUpdate(0) + updates.Timeout = 60 - log.Info().Int64("ID", b.Self.ID).Str("username", b.Self.UserName).Msg("Successfully authorized bot") + log.Info().Int64("ID", bot.Self.ID).Str("username", bot.Self.UserName).Msg("Successfully authorized bot") - return b.GetUpdatesChan(u), b + return bot.GetUpdatesChan(updates), bot } diff --git a/botIO/getinfo.go b/botIO/getinfo.go index b34cd11..66dae1b 100644 --- a/botIO/getinfo.go +++ b/botIO/getinfo.go @@ -1,12 +1,18 @@ package botIO -import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +import ( + "github.com/rs/zerolog/log" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) func GetUserByID(userToGet tgbotapi.ChatConfigWithUser, bot *tgbotapi.BotAPI) (error bool, member tgbotapi.ChatMember) { member, err := bot.GetChatMember(tgbotapi.GetChatMemberConfig{ChatConfigWithUser: userToGet}) if err != nil { + log.Error().Err(err).Msg("Unable to get info for user") return true, member } + log.Debug().Interface("member",member).Msg("Got Info for user") return false, member } diff --git a/commands/commands.go b/commands/commands.go index 5a10540..727d12e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -15,6 +15,8 @@ import ( "github.com/rs/zerolog/log" ) +//TODO logging + func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { cmd := strings.ToLower(update.Message.Command()) diff --git a/commands/download/download.go b/commands/download/download.go index cf733fa..453815f 100644 --- a/commands/download/download.go +++ b/commands/download/download.go @@ -22,6 +22,8 @@ import ( "github.com/rs/zerolog/log" ) +//TODO logging + func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) { log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("starting download") @@ -30,18 +32,17 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) { msg := tgbotapi.MessageConfig{ BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, - Text: "Downloading your media...", + Text: "Downloading your YouTube Video...", } workingMessage := botIO.SendMessage(msg, bot) if len(commandArgs) >= 1 && matchURL(commandArgs[0]) != "" { - //TODO distinguish - //service := matchURL(commandArgs[0]) + } else { - log.Error().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Str("args", update.Message.CommandArguments()).Msg("failed to download. empty arg") + log.Error().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Str("args", update.Message.CommandArguments()).Msg("Failed to download YouTube Video. Empty args") message := tgbotapi.EditMessageTextConfig{ BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, - Text: "Please specify a valid YouTube URL to download something.", + Text: "Please specify a valid YouTube URL.", } botIO.EditMessage(message, bot) return diff --git a/commands/gaypoints/gaypoints.go b/commands/gaypoints/gaypoints.go index 1669064..e67c126 100644 --- a/commands/gaypoints/gaypoints.go +++ b/commands/gaypoints/gaypoints.go @@ -10,7 +10,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/rs/zerolog/log" ) - +//TODO logging func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) { log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("getting gaypoints") diff --git a/commands/gaypoints/sql.go b/commands/gaypoints/sql.go index f8f7507..e86aee5 100644 --- a/commands/gaypoints/sql.go +++ b/commands/gaypoints/sql.go @@ -11,7 +11,8 @@ var gpSelectUser *sql.Stmt var gpSelectChat *sql.Stmt var gpSet *sql.Stmt - +//TODO logging +//TODO switch to SQL func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) { rows, err := gpSelectChat.Query(chatid) if err != nil { diff --git a/commands/notify/notify.go b/commands/notify/notify.go index 0de42a7..1240385 100644 --- a/commands/notify/notify.go +++ b/commands/notify/notify.go @@ -10,6 +10,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/rs/zerolog/log" ) +//TODO logging func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) { log.Debug().Str("args", update.Message.CommandArguments()).Msg("parsing new reminder") diff --git a/commands/notify/sql.go b/commands/notify/sql.go index e84de6e..707a162 100644 --- a/commands/notify/sql.go +++ b/commands/notify/sql.go @@ -9,7 +9,8 @@ var notifySetReminder *sql.Stmt var notifyGetReminders *sql.Stmt var notifyGetReminderDetails *sql.Stmt var notifyDeleteReminder *sql.Stmt - +//TODO logging +//TODO switch to SQL func InitDB() { const dbPath string = "./bloater.db" log.Info().Str("dbpath", dbPath).Msg("init database") diff --git a/config/config.go b/config/config.go index 7420442..1a996be 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "github.com/BurntSushi/toml" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" "os" "time" @@ -11,11 +12,11 @@ type config struct { APIToken string DebugMode bool PrivilegedUsers []int64 - Shutup map[int64]time.Time //is always filled at startup, not present in config - Webserver struct { + Shutup map[int64]time.Time //! is always filled at startup, not present in config + Webserver struct { Port string } - Balonlyl struct { + Balonlyl struct { EnabledChats []int64 BalonlylAT string TriggerWords []string @@ -39,7 +40,7 @@ func LoadConfig() { configFile, err := os.ReadFile("config.toml") if err != nil { - log.Panic().Err(err).Msg("rror opening config file") + log.Panic().Err(err).Msg("error opening config file") } _, err = toml.Decode(string(configFile), &BotConfig) @@ -49,6 +50,10 @@ func LoadConfig() { BotConfig.Shutup = make(map[int64]time.Time) + if !BotConfig.DebugMode { + zerolog.SetGlobalLevel(zerolog.InfoLevel) + } + log.Info().Msg("loaded config file") log.Debug().Interface("config", BotConfig).Msg("") @@ -59,27 +64,27 @@ func EncodeToml() { exampleConfig := config{ APIToken: "1234567:h8Y7ma7hMG93kxDx5o", DebugMode: false, - PrivilegedUsers: []int64{1234567,9764382423}, - Webserver: struct{ Port string }{ + PrivilegedUsers: []int64{1234567, 9764382423}, + Webserver: struct{ Port string }{ Port: "3000", }, - Balonlyl: struct { + Balonlyl: struct { EnabledChats []int64 BalonlylAT string TriggerWords []string }{ - EnabledChats: []int64{353454534,8743658}, + EnabledChats: []int64{353454534, 8743658}, BalonlylAT: "@Balonlyl", TriggerWords: []string{"French, France"}, }, - GayPoints: struct { + GayPoints: struct { EnabledChats []int64 ModifyUsers []int64 }{ - EnabledChats: []int64{353454534,8743658}, - ModifyUsers: []int64{3827468324,1736576}, + EnabledChats: []int64{353454534, 8743658}, + ModifyUsers: []int64{3827468324, 1736576}, }, - Download: struct { + Download: struct { Prefix string Yourls struct { APIPath string @@ -97,21 +102,18 @@ func EncodeToml() { }, } - - f, err := os.Create("config.toml.example") -if err != nil { - log.Panic().Err(err).Msg("error encoding config file. Cannot open config.toml.example") + if err != nil { + log.Panic().Err(err).Msg("error encoding config file. Cannot open config.toml.example") + } + + if err := toml.NewEncoder(f).Encode(exampleConfig); err != nil { + log.Panic().Err(err).Msg("error encoding config file") + } + if err := f.Close(); err != nil { + log.Panic().Err(err).Msg("error encoding config file. Cannot close config.toml.example") + + } + + log.Info().Msg("Wrote example config to config.toml.example. Bye Bye") } - -if err := toml.NewEncoder(f).Encode(exampleConfig); err != nil { - log.Panic().Err(err).Msg("error encoding config file") -} -if err := f.Close(); err != nil { - log.Panic().Err(err).Msg("error encoding config file. Cannot close config.toml.example") - -} - -log.Info().Msg("Wrote example config to config.toml.example. Bye Bye") - -} \ No newline at end of file diff --git a/main.go b/main.go index 16865fe..5ffb58c 100644 --- a/main.go +++ b/main.go @@ -26,16 +26,25 @@ func main() { } config.LoadConfig() + gaypoints.InitDB() + notify.InitDB() - go webserver.RunWeb() + + go webserver.RunServer() + bot() } func configureLogger() { output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.DateTime} + log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger() - zerolog.SetGlobalLevel(zerolog.TraceLevel) + + //! note that we overwrite the loglevel after loading the config in config/config.go:53. This is just the default + zerolog.SetGlobalLevel(zerolog.DebugLevel) + log.Info().Msg("Started zerolog logger") + } diff --git a/text/balonlyl/balonlyl.go b/text/balonlyl/balonlyl.go index 6049c2e..b13cd83 100644 --- a/text/balonlyl/balonlyl.go +++ b/text/balonlyl/balonlyl.go @@ -6,6 +6,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) +//TODO logging func Balonlyl(update tgbotapi.Update, bot *tgbotapi.BotAPI) { diff --git a/text/matcher.go b/text/matcher.go index 3ac23d5..baea18c 100644 --- a/text/matcher.go +++ b/text/matcher.go @@ -10,7 +10,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/rs/zerolog/log" ) - +//TODO logging func Matcher(update tgbotapi.Update, bot *tgbotapi.BotAPI) { log.Debug().Str("text", update.Message.Text).Int64("chat", update.Message.Chat.ID).Msg("Starting text matcher") diff --git a/webserver/webserver.go b/webserver/webserver.go index 5e9fc83..24424d3 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -7,7 +7,7 @@ import ( "github.com/rs/zerolog/log" ) -func RunWeb() { +func RunServer() { fs := http.FileServer(http.Dir("./videos")) http.Handle("/", fs)