logging changes

This commit is contained in:
watn3y 2023-12-29 07:54:27 +01:00
parent be0cc403b9
commit 02c9f2af6b
14 changed files with 96 additions and 77 deletions

View file

@ -10,19 +10,18 @@ import (
"watn3y.de/bloaterbot/commands/download"
"watn3y.de/bloaterbot/commands/gaypoints"
"watn3y.de/bloaterbot/commands/notify"
"watn3y.de/bloaterbot/commonlogic"
"watn3y.de/bloaterbot/config"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log"
)
//TODO logging
func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
cmd := strings.ToLower(update.Message.Command())
log.Debug().Str("cmd", cmd).Msg("Matching command")
log.Trace().Str("cmd", cmd).Msg("Matching command")
switch cmd {
case "shutup":
@ -93,13 +92,13 @@ func info(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
if commit == "" {
commit = "not available"
}
//TODO calculate total size
info, _ := os.Stat("data/db/gaypoints.db")
dbSizeInKiloBytes := info.Size() / 1000
textInfo := "<b>" + me.FirstName + "</b>" + "\n\n" +
"<b>Commit:</b> " + "<code>" + commit + "</code>" + "\n" +
"<b>DB Size:</b> " + "<code>" + strconv.FormatInt(dbSizeInKiloBytes, 10) + "KB" + "</code>"
"<b>DB Size:</b> " + "\n" +
"gaypoints.db: " + "<code>" + strconv.FormatInt(commonlogic.GetFileSize("data/db/gaypoints.db"), 10) + " Bytes" + "</code>" + "\n" +
"notify.db: " + "<code>" + strconv.FormatInt(commonlogic.GetFileSize("data/db/notify.db"), 10) + " Bytes" + "</code>" + "\n" +
"nenefoot.db: " + "<code>" + strconv.FormatInt(commonlogic.GetFileSize("data/db/nenefoot.db"), 10) + " Bytes" + "</code>" + "\n"
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},

View file

@ -11,7 +11,6 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
@ -23,8 +22,9 @@ import (
"github.com/rs/zerolog/log"
)
//TODO logging
// TODO logging
// TODO error handling
// TODO rewrite
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")
@ -92,8 +92,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
botIO.EditMessage(message, bot)
serveMedia(update, bot, downloadTarget, files[0].Name())
log2.Println("[download] Served URL " + update.Message.CommandArguments() + " for " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
log.Debug().Str("URL", update.Message.CommandArguments()).Int64("user", update.Message.From.ID).Int64("chat", update.Message.Chat.ID).Msg("Served Video")
time.Sleep(5 * time.Second)
log.Info().Msg("downloaded file")
@ -148,8 +147,8 @@ func runYTDL(URL string, targetDir string) (success bool) {
func matchURL(URL string) (matchedURL string) {
parsedURL, err := url.Parse(URL)
if err != nil {
log2.Fatal(err)
log.Err(err).Msg("Failed to parse URL")
return ""
}
var domainRegex = regexp.MustCompile(`(youtube\.com$)|(youtu\.be$)`)
@ -167,7 +166,8 @@ func matchURL(URL string) (matchedURL string) {
func shortURL(URL string) (shorturl string) {
resp, err := http.Get(config.BotConfig.Download.Yourls.APIPath + "?signature=" + config.BotConfig.Download.Yourls.Signature + "&action=shorturl&format=simple&url=" + URL)
if err != nil {
log2.Fatal(err)
log.Err(err).Msg("Failed to get shortURL")
return ""
}
defer resp.Body.Close()
@ -175,7 +175,8 @@ func shortURL(URL string) (shorturl string) {
body, err := io.ReadAll(resp.Body)
if err != nil {
log2.Fatal(err)
log.Err(err).Msg("Failed to get shortURL")
return ""
}
return string(body)

View file

@ -12,18 +12,20 @@ import (
"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")
log.Trace().Int64("ChatID",update.Message.Chat.ID).Msg(("Getting Gaypoints"))
if !commonlogic.ContainsInt64(config.BotConfig.GayPoints.EnabledChats, update.Message.Chat.ID) {
log.Debug().Int64("chat", update.Message.Chat.ID).Ints64("enabledChats", config.BotConfig.GayPoints.EnabledChats).Msg("not getting gaypoints, chat not enabled")
log.Debug().Int64("chat", update.Message.Chat.ID).Ints64("enabledChats", config.BotConfig.GayPoints.EnabledChats).Msg("Not getting Gaypoints, Chat not enabled")
return
}
if update.Message.ReplyToMessage != nil {
log.Debug().Msg("ReplyToMessage not empty, getting gaypoints for individual user")
log.Trace().Msg("ReplyToMessage not empty, getting Gaypoints for individual user")
points := sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID)
errorGettingUser, currentUser := botIO.GetUserByID(tgbotapi.ChatConfigWithUser{ChatID: update.Message.Chat.ID, UserID: update.Message.ReplyToMessage.From.ID}, bot)
@ -42,7 +44,7 @@ func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
}
} else {
log.Error().Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Msg("error getting details for user")
log.Error().Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Msg("Error getting Details for User")
messagetext = "Something went wrong :( Forward me a message from the user and try again. This might fix it."
}
@ -56,7 +58,7 @@ func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
botIO.SendMessage(message, bot)
return
}
log.Debug().Msg("ReplyToMessage is empty, getting gaypoints for whole chat")
log.Debug().Msg("ReplyToMessage is empty, getting Gaypoints for whole chat")
gps := sqlGetAllGP(update.Message.Chat.ID)
@ -64,14 +66,14 @@ func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
for _, gaypointInfo := range gps {
errorGettingUser, currentUser := botIO.GetUserByID(tgbotapi.ChatConfigWithUser{ChatID: update.Message.Chat.ID, UserID: gaypointInfo.userID}, bot)
if errorGettingUser {
log.Error().Int64("user", update.Message.From.ID).Int64("chat", update.Message.Chat.ID).Msg("error getting details for user")
log.Error().Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Msg("Error getting Details for User")
continue
}
currentName := currentUser.User.FirstName
if currentUser.User.UserName != "" {
currentName = currentUser.User.UserName
}
log.Debug().Int64("user", gaypointInfo.userID).Int64("chat", update.Message.Chat.ID).Int64("gp", gaypointInfo.gaypoints).Msg("got details for user")
text := `<a href="` + `tg://user?id=` + strconv.FormatInt(gaypointInfo.userID, 10) + `">` + currentName + `</a>` + ` currently has ` + `<b>` + strconv.FormatInt(gaypointInfo.gaypoints, 10) + `</b>` + ` gaypoints` + "\n"
messagetext = messagetext + text
@ -208,7 +210,7 @@ func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
Text: messagetext,
}
log.Info().Int64("oldGP", currentPoints).Int64("newGP", finalgp).Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Int64("changedBy", update.Message.From.ID).Msg("setting gaypoints")
log.Info().Int64("oldGP", currentPoints).Int64("newGP", finalgp).Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Int64("changedBy", update.Message.From.ID).Msg("Set Gaypoints")
sqlSetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID, finalgp)
botIO.SendMessage(message, bot)

View file

@ -12,12 +12,11 @@ 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 {
log.Error().Err(err).Msg("failed to execute SQL to get get gaypoints for chat")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't get gaypoints for chat")
return
}
@ -31,11 +30,12 @@ func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
gaypoints: b,
})
default:
log.Error().Err(err).Msg("failed to parse SQL to get get gaypoints for chat")
log.Error().Err(err).Msg("Failed to parse SQL Result. Can't get gaypoints for chat")
return
}
log.Debug().Msg("executed SQL to get gaypoints for chat")
log.Trace().Int64("ChatID", chatid).Interface("GP", gaypoints).Msg(("SQL: got SQL for Chat"))
}
return gaypoints
@ -51,26 +51,25 @@ func sqlGetGP(chatid int64, userid int64) (gaypoints int64) {
case nil:
break
default:
log.Error().Err(err).Msg("failed to execute SQL to get get gaypoints for user")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't get gaypoints for User")
return
}
log.Debug().Msg("executed SQL to get gaypoints for user")
log.Trace().Int64("ChatID", chatid).Int64("UserID", userid).Interface("GP", gaypoints).Msg(("SQL: got SQL for User"))
return gaypoints
}
func sqlSetGP(chatid int64, userid int64, gaypoints int64) {
_, err := gpSet.Exec(chatid, userid, gaypoints)
if err != nil {
log.Error().Err(err).Msg("failed to execute SQL to get set gaypoints for user")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't set gaypoints for User")
return
}
log.Debug().Msg("executed SQL to set gaypoints for user")
log.Info().Int64("ChatID", chatid).Int64("UserID", userid).Interface("GP", gaypoints).Msg(("SQL: set SQL for User"))
}
func InitDB() {
const dbPath string = "data/db/gaypoints.db"
log.Info().Str("dbpath", dbPath).Msg("init database")
db, err := sql.Open("sqlite", dbPath)
if err != nil {
log.Panic().Err(err).Msg("failed to open sqlite database")
@ -99,5 +98,5 @@ func InitDB() {
log.Panic().Err(err).Msg("failed to create sql statement")
}
log.Info().Msg("init database: done")
log.Info().Str("dbpath", dbPath).Msg("Database prepared and connected")
}

View file

@ -12,10 +12,10 @@ import (
"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")
log.Trace().Str("args", update.Message.CommandArguments()).Msg(("Parsing new Reminder"))
commandArgs := strings.Fields(update.Message.CommandArguments())
var timeArg string
@ -25,7 +25,9 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
timeArg = strings.ToLower(commandArgs[0])
textArg = strings.Replace(update.Message.CommandArguments(), timeArg, "", 1)
} else {
log.Error().Str("args", update.Message.CommandArguments()).Msg("invalid new reminder")
log.Error().Str("CommandArgs", update.Message.CommandArguments()).Msg("Failed to add Reminder. Unable to parse reminder")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
ParseMode: "html",
@ -39,7 +41,9 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
isValidFormat := regexp.MustCompile(`(?m)^\d{1,3}[mhd]$`)
if !isValidFormat.MatchString(timeArg) {
log.Error().Str("args", update.Message.CommandArguments()).Msg("invalid new reminder")
log.Error().Str("CommandArgs", update.Message.CommandArguments()).Msg("Failed to add Reminder. Unable to parse reminder")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
ParseMode: "html",
@ -78,8 +82,6 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
reminderText: textArg,
}
log.Info().Int("ID", reminder.updateID).Int64("chat", reminder.chatID).Int64("user", reminder.userID).Int64("time", reminder.notifyTime).Msg("adding new reminder")
sqlAddReminder(reminder)
message := tgbotapi.MessageConfig{
@ -89,12 +91,15 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
Text: "Set reminder for " + notifyTime.Format("02.01.2006") + " at " + notifyTime.Format("15:04") + " UTC ",
}
botIO.SendMessage(message, bot)
log.Info().Int("ID", reminder.updateID).Int64("chat", reminder.chatID).Int64("user", reminder.userID).Int64("time", reminder.notifyTime).Msg("Added new Reminder")
return
}
func NotifyHandler(bot *tgbotapi.BotAPI) {
log.Info().Msg("Handling Reminders")
for {
reminders := sqlGetReminders()
@ -109,11 +114,11 @@ func NotifyHandler(bot *tgbotapi.BotAPI) {
DisableWebPagePreview: false,
Text: "Reminder: " + details.reminderText,
}
log.Info().Int64("chat", details.chatID).Int64("user", details.userID).Str("text", details.reminderText).Msg("sent reminder")
botIO.SendMessage(message, bot)
sqlDeleteReminder(updateID)
log.Info().Int64("chat", details.chatID).Int64("user", details.userID).Str("text", details.reminderText).Msg("Sent Reminder")
}
}

View file

@ -11,11 +11,8 @@ var notifyGetReminders *sql.Stmt
var notifyGetReminderDetails *sql.Stmt
var notifyDeleteReminder *sql.Stmt
// TODO logging
// TODO switch to SQL
func InitDB() {
const dbPath string = "data/db/notify.db"
log.Info().Str("dbpath", dbPath).Msg("init database")
db, err := sql.Open("sqlite", dbPath)
if err != nil {
log.Panic().Err(err).Msg("failed to open sqlite database")
@ -50,33 +47,32 @@ func InitDB() {
log.Panic().Err(err).Msg("failed to create sql statement")
}
log.Info().Msg("init database: done")
log.Info().Str("dbpath", dbPath).Msg("Database prepared and connected")
}
func sqlDeleteReminder(updateID int) {
_, err := notifyDeleteReminder.Exec(updateID)
if err != nil {
log.Error().Err(err).Msg("failed to execute SQL to delete reminder")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't delete Reminder")
return
}
log.Debug().Msg("executed SQL to delete reminder")
log.Trace().Int("ID", updateID).Msg(("SQL: deleted reminder"))
}
func sqlAddReminder(reminder reminderConfig) {
_, err := notifySetReminder.Exec(reminder.updateID, reminder.notifyTime, reminder.chatID, reminder.userID, reminder.messageToReplyToID, reminder.reminderText)
if err != nil {
log.Error().Err(err).Msg("failed to execute SQL to add reminder")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't add Reminder")
return
}
log.Debug().Msg("executed SQL to add reminder")
log.Trace().Interface("Reminder", reminder).Msg(("SQL: added Reminder"))
}
func sqlGetReminders() (reminders map[int]int64) {
rows, err := notifyGetReminders.Query()
if err != nil {
log.Error().Err(err).Msg("failed to execute SQL to get reminder")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't get Reminders")
return
}
@ -88,13 +84,13 @@ func sqlGetReminders() (reminders map[int]int64) {
case nil:
reminders[c] = b
default:
log.Error().Err(err).Msg("failed to parse SQL to get reminder")
log.Error().Err(err).Msg("Failed to parse SQL Result. Can't get Reminders")
return
}
}
log.Debug().Msg("executed SQL to get reminders")
log.Trace().Interface("Reminders", reminders).Msg(("SQL: got Reminders"))
return reminders
}
@ -105,10 +101,10 @@ func sqlGetReminderDetails(updateID int) (reminder reminderConfig) {
case nil:
break
default:
log.Error().Err(err).Msg("failed to execute SQL to get reminder details")
log.Error().Err(err).Msg("Failed to execute SQL Query. Can't get Reminder Details")
return
}
log.Debug().Msg("executed SQL to get reminder details")
log.Trace().Interface("Reminder", reminder).Msg(("SQL: got Reminder Details"))
return reminder
}