added proper logging to most of the code
This commit is contained in:
parent
65c6020e49
commit
7d4c9c5ab9
21 changed files with 218 additions and 283 deletions
|
@ -2,7 +2,7 @@ package gaypoints
|
|||
|
||||
import (
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"log"
|
||||
"github.com/rs/zerolog/log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"watn3y/bloaterbotv3/botIO"
|
||||
|
@ -12,12 +12,15 @@ import (
|
|||
|
||||
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")
|
||||
|
||||
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")
|
||||
return
|
||||
}
|
||||
|
||||
if update.Message.ReplyToMessage != nil {
|
||||
log.Println("[gaypoints] Looking for gaypoints for user " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
log.Debug().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)
|
||||
|
@ -27,10 +30,17 @@ func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
if currentUser.User.UserName != "" {
|
||||
currentName = currentUser.User.UserName
|
||||
}
|
||||
messagetext = `<a href="` + `tg://user?id=` + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + `">` + currentName + `</a>` + ` currently has ` + `<b>` + strconv.FormatInt(points, 10) + `</b>` + ` gaypoints` + "\n"
|
||||
log.Debug().Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Int64("gp", points).Msg("got details for user")
|
||||
if points == -1 {
|
||||
messagetext = `<a href="` + `tg://user?id=` + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + `">` + currentName + `</a>` + ` currently has no gaypoints` + "\n"
|
||||
|
||||
} else {
|
||||
messagetext = `<a href="` + `tg://user?id=` + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + `">` + currentName + `</a>` + ` currently has ` + `<b>` + strconv.FormatInt(points, 10) + `</b>` + ` gaypoints` + "\n"
|
||||
}
|
||||
|
||||
} else {
|
||||
messagetext = "Something went wrong :("
|
||||
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."
|
||||
}
|
||||
|
||||
message := tgbotapi.MessageConfig{
|
||||
|
@ -43,20 +53,22 @@ 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.Println("[gaypoints] Looking for gaypoints for chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
gps := sqlGetAllGP(update.Message.Chat.ID)
|
||||
|
||||
var messagetext string
|
||||
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.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
|
||||
|
@ -74,15 +86,19 @@ func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
}
|
||||
|
||||
func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
log.Debug().Int64("chat", update.Message.Chat.ID).Str("text", update.Message.Text).Msg("setting 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 setting gaypoints, chat not enabled")
|
||||
return
|
||||
}
|
||||
|
||||
if !commonlogic.ContainsInt64(config.BotConfig.GayPoints.ModifyUsers, update.Message.From.ID) {
|
||||
log.Debug().Int64("chat", update.Message.Chat.ID).Ints64("enabledChats", config.BotConfig.GayPoints.ModifyUsers).Msg("not setting gaypoints, user not authorised")
|
||||
sticker := tgbotapi.StickerConfig{BaseFile: tgbotapi.BaseFile{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
File: tgbotapi.FilePath("bloater.webp"),
|
||||
}}
|
||||
|
||||
botIO.SendSticker(sticker, bot)
|
||||
return
|
||||
|
||||
|
@ -98,41 +114,41 @@ func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
} else {
|
||||
arg = ""
|
||||
}
|
||||
log.Println("[gaypoints] Trying to modify gaypoints for user " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
|
||||
if update.Message.ReplyToMessage == nil {
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Msg("failed to set gaypoints for user, ReplyToMessage not present")
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
ParseMode: "html",
|
||||
DisableWebPagePreview: false,
|
||||
Text: "Please reply to a message to set gaypoints",
|
||||
}
|
||||
log.Println("[gaypoints] Failed to set gaypoints for user " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
botIO.SendMessage(message, bot)
|
||||
return
|
||||
}
|
||||
|
||||
if arg == "" {
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Msg("failed to set gaypoints for user, empty value")
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
ParseMode: "html",
|
||||
DisableWebPagePreview: false,
|
||||
Text: "Please specify a valid gaypoint value",
|
||||
Text: "Please specify a valid gaypoint value. Your current value seems to be empty.",
|
||||
}
|
||||
log.Println("[gaypoints] Failed to set gaypoints for user " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
botIO.SendMessage(message, bot)
|
||||
return
|
||||
}
|
||||
|
||||
newPoints, err := strconv.ParseInt(arg, 10, 64)
|
||||
if err != nil || newPoints <= 0 {
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Msg("failed to set gaypoints for user, invalid value or value <= 0")
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
ParseMode: "html",
|
||||
DisableWebPagePreview: false,
|
||||
Text: "Please specify a valid gaypoint value",
|
||||
Text: "Please specify a valid gaypoint value. Your current value was either too low or I was not able to parse it correctly.",
|
||||
}
|
||||
log.Println("[gaypoints] Failed to set gaypoints for user " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
|
||||
botIO.SendMessage(message, bot)
|
||||
return
|
||||
}
|
||||
|
@ -142,7 +158,7 @@ func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
|
||||
if cmd == "addgp" {
|
||||
currentPoints = sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID)
|
||||
if currentPoints <= -1 {
|
||||
if currentPoints <= 0 {
|
||||
finalgp = newPoints
|
||||
} else {
|
||||
finalgp = currentPoints + newPoints
|
||||
|
@ -150,15 +166,15 @@ func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
}
|
||||
|
||||
if cmd == "subtractgp" {
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Msg("failed to set gaypoints for user, new value is < 0")
|
||||
currentPoints = sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID)
|
||||
if currentPoints <= 0 {
|
||||
if currentPoints-newPoints < 0 {
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
ParseMode: "html",
|
||||
DisableWebPagePreview: false,
|
||||
Text: "Please specify a valid gaypoint value",
|
||||
Text: "Please specify a valid gaypoint value. After subtracting your current value from the users current gaypoints, they would have < 0 gaypoints. This is not allowed.",
|
||||
}
|
||||
log.Println("[gaypoints] Failed to subtrackt gaypoints for user " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
|
||||
botIO.SendMessage(message, bot)
|
||||
return
|
||||
} else {
|
||||
|
@ -174,11 +190,11 @@ func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
if currentUser.User.UserName != "" {
|
||||
currentName = currentUser.User.UserName
|
||||
}
|
||||
log.Debug().Int64("user", update.Message.ReplyToMessage.From.ID).Int64("chat", update.Message.Chat.ID).Msg("got details for user")
|
||||
messagetext = `<a href="` + `tg://user?id=` + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + `">` + currentName + `</a>` + ` now has ` + `<b>` + strconv.FormatInt(finalgp, 10) + `</b>` + ` gaypoints` + "\n"
|
||||
} else {
|
||||
messagetext = "Something went wrong :("
|
||||
log.Println("[gaypoints] Failed to change gaypoints " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10) + " from " + strconv.FormatInt(currentPoints, 10) + " to " + strconv.FormatInt(finalgp, 10))
|
||||
|
||||
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."
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -189,7 +205,7 @@ func SetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
Text: messagetext,
|
||||
}
|
||||
|
||||
log.Println("[gaypoints] Changing gaypoints " + strconv.FormatInt(update.Message.ReplyToMessage.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10) + " from " + strconv.FormatInt(currentPoints, 10) + " to " + strconv.FormatInt(finalgp, 10))
|
||||
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")
|
||||
|
||||
sqlSetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID, finalgp)
|
||||
botIO.SendMessage(message, bot)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue