213 lines
9 KiB
Go
213 lines
9 KiB
Go
package gaypoints
|
|
|
|
import (
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/rs/zerolog/log"
|
|
"strconv"
|
|
"strings"
|
|
"watn3y/bloaterbotv3/botIO"
|
|
"watn3y/bloaterbotv3/commonlogic"
|
|
"watn3y/bloaterbotv3/config"
|
|
)
|
|
|
|
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.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)
|
|
var messagetext string
|
|
if !errorGettingUser {
|
|
currentName := currentUser.User.FirstName
|
|
if currentUser.User.UserName != "" {
|
|
currentName = currentUser.User.UserName
|
|
}
|
|
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 {
|
|
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{
|
|
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
|
ParseMode: "html",
|
|
DisableWebPagePreview: false,
|
|
Text: messagetext,
|
|
}
|
|
|
|
botIO.SendMessage(message, bot)
|
|
return
|
|
}
|
|
log.Debug().Msg("ReplyToMessage is empty, getting gaypoints for whole chat")
|
|
|
|
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.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
|
|
}
|
|
|
|
message := tgbotapi.MessageConfig{
|
|
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
|
ParseMode: "html",
|
|
DisableWebPagePreview: false,
|
|
Text: messagetext,
|
|
}
|
|
|
|
botIO.SendMessage(message, bot)
|
|
return
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
cmd := strings.ToLower(update.Message.Command())
|
|
commandArgs := strings.Fields(update.Message.CommandArguments())
|
|
|
|
var arg string
|
|
|
|
if len(commandArgs) >= 1 {
|
|
arg = strings.ToLower(commandArgs[0])
|
|
} else {
|
|
arg = ""
|
|
}
|
|
|
|
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",
|
|
}
|
|
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. Your current value seems to be empty.",
|
|
}
|
|
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. Your current value was either too low or I was not able to parse it correctly.",
|
|
}
|
|
|
|
botIO.SendMessage(message, bot)
|
|
return
|
|
}
|
|
|
|
var finalgp int64
|
|
var currentPoints int64
|
|
|
|
if cmd == "addgp" {
|
|
currentPoints = sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID)
|
|
if currentPoints <= 0 {
|
|
finalgp = newPoints
|
|
} else {
|
|
finalgp = currentPoints + newPoints
|
|
}
|
|
}
|
|
|
|
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-newPoints < 0 {
|
|
message := tgbotapi.MessageConfig{
|
|
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
|
DisableWebPagePreview: false,
|
|
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.",
|
|
}
|
|
|
|
botIO.SendMessage(message, bot)
|
|
return
|
|
} else {
|
|
finalgp = currentPoints - newPoints
|
|
}
|
|
}
|
|
|
|
errorGettingUser, currentUser := botIO.GetUserByID(tgbotapi.ChatConfigWithUser{ChatID: update.Message.Chat.ID, UserID: update.Message.ReplyToMessage.From.ID}, bot)
|
|
|
|
var messagetext string
|
|
if !errorGettingUser {
|
|
currentName := currentUser.User.FirstName
|
|
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 {
|
|
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
|
|
}
|
|
|
|
message := tgbotapi.MessageConfig{
|
|
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
|
ParseMode: "html",
|
|
DisableWebPagePreview: false,
|
|
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")
|
|
|
|
sqlSetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID, finalgp)
|
|
botIO.SendMessage(message, bot)
|
|
return
|
|
}
|