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

@ -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")
}