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 commands
|
|||
|
||||
import (
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"log"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -18,6 +18,8 @@ func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
|
||||
cmd := strings.ToLower(update.Message.Command())
|
||||
|
||||
log.Debug().Str("cmd", cmd).Msg("Matching command")
|
||||
|
||||
switch cmd {
|
||||
case "shutup":
|
||||
shutup(update, bot)
|
||||
|
@ -46,7 +48,7 @@ func shutup(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
config.BotConfig.Shutup[update.Message.Chat.ID] = time.Now().UTC()
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Shutting up")
|
||||
|
||||
log.Println("Shutting up for Chat: " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
log.Info().Int64("chat", update.Message.Chat.ID).Msg("Shutting up")
|
||||
|
||||
botIO.SendMessage(msg, bot)
|
||||
|
||||
|
|
|
@ -3,8 +3,9 @@ package download
|
|||
import (
|
||||
"errors"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
"io"
|
||||
"log"
|
||||
log2 "log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -22,7 +23,7 @@ import (
|
|||
|
||||
func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
log.Println("[download] Downloading URL " + update.Message.CommandArguments() + " for " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("starting download")
|
||||
|
||||
commandArgs := strings.Fields(update.Message.CommandArguments())
|
||||
|
||||
|
@ -36,7 +37,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
//TODO distinguish
|
||||
//service := matchURL(commandArgs[0])
|
||||
} else {
|
||||
log.Println("[download] Failed to download URL " + update.Message.CommandArguments() + " NO URL")
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Str("args", update.Message.CommandArguments()).Msg("failed to download. empty arg")
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
Text: "Please specify a valid YouTube URL to download something.",
|
||||
|
@ -61,7 +62,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
files, err := os.ReadDir("./videos/" + downloadTarget)
|
||||
|
||||
if err != nil {
|
||||
log.Println("[download] Failed to process URL "+update.Message.CommandArguments(), " FAILED TO READ DIRECTORY: ", err.Error())
|
||||
log.Error().Err(err).Msg("failed to download. unable to read target directory")
|
||||
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
|
@ -72,7 +73,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
}
|
||||
|
||||
if len(files) > 1 {
|
||||
log.Println("[download] Failed to process URL "+update.Message.CommandArguments(), " TOO MANY FILES")
|
||||
log.Error().Err(err).Msg("failed to download. too many files")
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
Text: "Something went wrong while downloading your media :(",
|
||||
|
@ -88,10 +89,11 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
botIO.EditMessage(message, bot)
|
||||
|
||||
serveMedia(update, bot, downloadTarget, files[0].Name())
|
||||
log.Println("[download] Served URL " + update.Message.CommandArguments() + " for " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
|
||||
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))
|
||||
|
||||
time.Sleep(10 * time.Second)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
log.Info().Msg("downloaded file")
|
||||
bot.Send(tgbotapi.NewDeleteMessage(workingMessage.Chat.ID, workingMessage.MessageID))
|
||||
|
||||
}
|
||||
|
@ -117,19 +119,19 @@ func runYTDL(URL string, targetDir string) (success bool) {
|
|||
)
|
||||
|
||||
if errors.As(err, &ee) {
|
||||
log.Println("[download.ytdl] Failed to download URL "+URL, " NON ZERO EXIT CODE: ", ee.ExitCode())
|
||||
log2.Println("[download.ytdl] Failed to download URL "+URL, " NON ZERO EXIT CODE: ", ee.ExitCode())
|
||||
success = false
|
||||
|
||||
} else if errors.As(err, &pe) {
|
||||
log.Println("[download.ytdl] Failed to download URL "+URL, " OS PATH ERROR: ", pe.Error())
|
||||
log2.Println("[download.ytdl] Failed to download URL "+URL, " OS PATH ERROR: ", pe.Error())
|
||||
success = false
|
||||
|
||||
} else if err != nil {
|
||||
log.Println("[download.ytdl] Failed to download URL "+URL, " GENERAL ERROR: ", err.Error())
|
||||
log2.Println("[download.ytdl] Failed to download URL "+URL, " GENERAL ERROR: ", err.Error())
|
||||
success = false
|
||||
|
||||
} else {
|
||||
log.Println("[download.ytdl] Downloaded URL " + URL)
|
||||
log2.Println("[download.ytdl] Downloaded URL " + URL)
|
||||
success = true
|
||||
}
|
||||
|
||||
|
@ -143,7 +145,7 @@ func runYTDL(URL string, targetDir string) (success bool) {
|
|||
func matchURL(URL string) (matchedURL string) {
|
||||
parsedURL, err := url.Parse(URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log2.Fatal(err)
|
||||
|
||||
}
|
||||
|
||||
|
@ -162,7 +164,7 @@ 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 {
|
||||
log.Fatal(err)
|
||||
log2.Fatal(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
@ -170,7 +172,7 @@ func shortURL(URL string) (shorturl string) {
|
|||
body, err := io.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log2.Fatal(err)
|
||||
}
|
||||
|
||||
return string(body)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package notify
|
|||
|
||||
import (
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"log"
|
||||
"github.com/rs/zerolog/log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -11,6 +11,7 @@ import (
|
|||
)
|
||||
|
||||
func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
log.Debug().Str("args", update.Message.CommandArguments()).Msg("parsing new reminder")
|
||||
commandArgs := strings.Fields(update.Message.CommandArguments())
|
||||
|
||||
var timeArg string
|
||||
|
@ -20,6 +21,7 @@ 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")
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
ParseMode: "html",
|
||||
|
@ -30,11 +32,10 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
return
|
||||
}
|
||||
|
||||
log.Println("[notify] Attempting to set reminder for user " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10) + " with arguments " + timeArg)
|
||||
|
||||
isValidFormat := regexp.MustCompile(`(?m)^\d{1,3}[mhd]$`)
|
||||
|
||||
if !isValidFormat.MatchString(timeArg) {
|
||||
log.Error().Str("args", update.Message.CommandArguments()).Msg("invalid new reminder")
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
ParseMode: "html",
|
||||
|
@ -64,9 +65,7 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
|
||||
notifyTime := time.Now().UTC().Add(time.Duration(number) * modifyTime)
|
||||
|
||||
var reminder reminderConfig
|
||||
|
||||
reminder = reminderConfig{
|
||||
reminder := reminderConfig{
|
||||
updateID: update.UpdateID,
|
||||
notifyTime: notifyTime.Unix(),
|
||||
chatID: update.Message.Chat.ID,
|
||||
|
@ -75,6 +74,8 @@ 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{
|
||||
|
@ -95,17 +96,19 @@ func NotifyHandler(bot *tgbotapi.BotAPI) {
|
|||
|
||||
for updateID, reminderTime := range reminders {
|
||||
if reminderTime <= time.Now().UTC().Unix() {
|
||||
log.Info().Int("ID", updateID).Msg("reminder is due")
|
||||
details := sqlGetReminderDetails(updateID)
|
||||
|
||||
message := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: details.chatID, ReplyToMessageID: details.messageToReplyToID},
|
||||
ParseMode: "html",
|
||||
DisableWebPagePreview: false,
|
||||
|
||||
Text: details.reminderText,
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,7 @@ package notify
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"strconv"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var notifySetReminder *sql.Stmt
|
||||
|
@ -13,61 +12,69 @@ var notifyDeleteReminder *sql.Stmt
|
|||
|
||||
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 notify (updateID INTEGER,notifyTime INTEGER,chatID INTEGER, userID INTEGER,messageToReplyToID INTEGER,notifyText TEXT)")
|
||||
if err != nil {
|
||||
log.Panicf("Failed to create table: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create table")
|
||||
}
|
||||
log.Println("[notify.sql] Created sqlite table in database at " + dbPath)
|
||||
|
||||
notifySetReminder, err = db.Prepare("INSERT OR REPLACE INTO notify (updateID,notifyTime,chatID, userID,messageToReplyToID,notifyText) values (?,?,?,?,?,?)")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql insert: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
notifyGetReminders, err = db.Prepare("SELECT updateID,notifyTime FROM notify")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
notifyGetReminderDetails, err = db.Prepare("SELECT chatID, userID,messageToReplyToID,notifyText FROM notify WHERE updateID=?")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
notifyDeleteReminder, err = db.Prepare("DELETE FROM notify WHERE updateID=?")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
log.Panic().Err(err).Msg("failed to create sql statement")
|
||||
}
|
||||
|
||||
log.Info().Msg("init database: done")
|
||||
}
|
||||
|
||||
func sqlDeleteReminder(updateID int) {
|
||||
_, err := notifyDeleteReminder.Exec(updateID)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to delete reminder: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to delete reminder")
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug().Msg("executed SQL to delete reminder")
|
||||
}
|
||||
|
||||
func sqlAddReminder(reminder reminderConfig) {
|
||||
_, err := notifySetReminder.Exec(reminder.updateID, reminder.notifyTime, reminder.chatID, reminder.userID, reminder.messageToReplyToID, reminder.reminderText)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to set add reminder: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to add reminder")
|
||||
return
|
||||
}
|
||||
log.Println("[notify.sql] Set new reminder for " + strconv.FormatInt(reminder.userID, 10) + " in chat " + strconv.FormatInt(reminder.chatID, 10))
|
||||
|
||||
log.Debug().Msg("executed SQL to add reminder")
|
||||
}
|
||||
|
||||
func sqlGetReminders() (reminders map[int]int64) {
|
||||
rows, err := notifyGetReminders.Query()
|
||||
if err != nil {
|
||||
log.Panicf("Failed to query reminders: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to get reminder")
|
||||
return
|
||||
}
|
||||
|
||||
reminders = make(map[int]int64)
|
||||
|
@ -78,11 +85,13 @@ func sqlGetReminders() (reminders map[int]int64) {
|
|||
case nil:
|
||||
reminders[c] = b
|
||||
default:
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to parse SQL to get reminder")
|
||||
return
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msg("executed SQL to get reminders")
|
||||
return reminders
|
||||
}
|
||||
|
||||
|
@ -93,9 +102,10 @@ func sqlGetReminderDetails(updateID int) (reminder reminderConfig) {
|
|||
case nil:
|
||||
break
|
||||
default:
|
||||
log.Panicf("Failed to get reminder details: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to execute SQL to get reminder details")
|
||||
return
|
||||
|
||||
}
|
||||
log.Println("[notify.sql] Got reminder details for update ID" + string(rune(updateID)))
|
||||
log.Debug().Msg("executed SQL to get reminder details")
|
||||
return reminder
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue