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)
|
||||
|
|
|
@ -3,8 +3,7 @@ package gaypoints
|
|||
import (
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"strconv"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var gpSelectUser *sql.Stmt
|
||||
|
@ -16,7 +15,8 @@ var gpSet *sql.Stmt
|
|||
func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
|
||||
rows, err := gpSelectChat.Query(chatid)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to get get gaypoints for chat")
|
||||
return
|
||||
}
|
||||
|
||||
var c int64
|
||||
|
@ -29,10 +29,11 @@ func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
|
|||
gaypoints: b,
|
||||
})
|
||||
default:
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to parse SQL to get get gaypoints for chat")
|
||||
return
|
||||
|
||||
}
|
||||
log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(c, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(b, 10))
|
||||
log.Debug().Msg("executed SQL to get gaypoints for chat")
|
||||
}
|
||||
|
||||
return gaypoints
|
||||
|
@ -48,50 +49,53 @@ func sqlGetGP(chatid int64, userid int64) (gaypoints int64) {
|
|||
case nil:
|
||||
break
|
||||
default:
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to get get gaypoints for user")
|
||||
return
|
||||
|
||||
}
|
||||
log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(gaypoints, 10))
|
||||
log.Debug().Msg("executed SQL to get gaypoints for user")
|
||||
return gaypoints
|
||||
}
|
||||
|
||||
func sqlSetGP(chatid int64, userid int64, gaypoints int64) {
|
||||
_, err := gpSet.Exec(chatid, userid, gaypoints)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to set gaypoints: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to get set gaypoints for user")
|
||||
return
|
||||
}
|
||||
log.Println("[gaypoints.sql] Set gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + " to " + strconv.FormatInt(gaypoints, 10))
|
||||
log.Debug().Msg("executed SQL to set gaypoints for user")
|
||||
}
|
||||
|
||||
func InitDB() {
|
||||
const dbPath string = "./bloater.db"
|
||||
log.Info().Str("dbpath", dbPath).Msg("init database")
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to open sqlite database: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to open sqlite database")
|
||||
}
|
||||
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS gaypoints (chatid bigint, userid bigint,gaypoints bigint,UNIQUE(chatid,userid))")
|
||||
if err != nil {
|
||||
log.Panicf("Failed to create table: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create table")
|
||||
}
|
||||
log.Println("[gaypoints.sql] Created sqlite table in database at " + dbPath)
|
||||
|
||||
gpSelectUser, err = db.Prepare("SELECT gaypoints FROM gaypoints WHERE chatid = ? AND userid = ?")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
gpSelectChat, err = db.Prepare("SELECT userid,gaypoints FROM gaypoints WHERE chatid = ? ORDER BY gaypoints DESC")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
gpSet, err = db.Prepare("INSERT OR REPLACE INTO gaypoints (chatid, userid, gaypoints) values (?,?,?)")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql insert: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
log.Info().Msg("init database: done")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue