From 6115e09602732189cb7c41576c824bfea7a73373 Mon Sep 17 00:00:00 2001 From: Watn3y Date: Sat, 18 Mar 2023 23:50:42 +0100 Subject: [PATCH] new gaypoints feature --- Dockerfile | 3 + commands/commands.go | 28 +++++- commands/gaypoints/gaypoints.go | 167 ++++++++++++++++++++++++++++++++ commands/gaypoints/sql.go | 97 +++++++++++++++++++ config.toml.example | 11 ++- config/config.go | 4 + docker-compose.yaml | 1 + go.mod | 7 +- go.sum | 4 +- main.go | 2 + 10 files changed, 310 insertions(+), 14 deletions(-) create mode 100644 commands/gaypoints/gaypoints.go create mode 100644 commands/gaypoints/sql.go diff --git a/Dockerfile b/Dockerfile index 242acd0..03a1fd4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /tmp/build #install update system and install packages RUN apk update RUN apk upgrade --available +RUN apk add alpine-sdk #build bloaterbot COPY . . @@ -12,6 +13,8 @@ RUN go mod download RUN mkdir /app +ARG CGO_ENABLED=1 + RUN go build -o /app/bloaterbot WORKDIR /app diff --git a/commands/commands.go b/commands/commands.go index 48a53ea..a29318e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -7,16 +7,34 @@ import ( "strings" "time" "watn3y/bloaterbotv3/botIO" + "watn3y/bloaterbotv3/commands/gaypoints" "watn3y/bloaterbotv3/config" ) func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { - if strings.ToLower(update.Message.Command()) == "shutup" || strings.ToLower(update.Message.Command()) == "shut" { - 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)) + cmd := strings.ToLower(update.Message.Command()) - botIO.SendMessage(msg, bot) + switch cmd { + case "shutup": + shutup(update, bot) + case "shut": + shutup(update, bot) + case "gp": + gaypoints.GetGP(update, bot) + case "addgp": + gaypoints.SetGP(update, bot) + case "subtracktgp": + gaypoints.SetGP(update, bot) } } + +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)) + + botIO.SendMessage(msg, bot) + +} diff --git a/commands/gaypoints/gaypoints.go b/commands/gaypoints/gaypoints.go new file mode 100644 index 0000000..a032cc2 --- /dev/null +++ b/commands/gaypoints/gaypoints.go @@ -0,0 +1,167 @@ +package gaypoints + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "log" + "strconv" + "strings" + "watn3y/bloaterbotv3/botIO" + "watn3y/bloaterbotv3/commonlogic" + "watn3y/bloaterbotv3/config" +) + +func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + + if !commonlogic.ContainsInt64(config.BotConfig.GayPoints.EnabledChats, update.Message.Chat.ID) { + 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)) + points := sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID) + + currentUser := botIO.GetUserByID(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID, bot) + currentName := currentUser.User.FirstName + if currentUser.User.UserName != "" { + currentName = currentUser.User.UserName + } + messagetext := `` + currentName + `` + ` currently has ` + `` + strconv.FormatInt(points, 10) + `` + ` gaypoints` + "\n" + + 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.Println("[gaypoints] Looking for gaypoints for chat " + strconv.FormatInt(update.Message.Chat.ID, 10)) + gps := sqlGetAllGP(update.Message.Chat.ID) + + var messagetext string + for userid, points := range gps { + currentUser := botIO.GetUserByID(update.Message.Chat.ID, userid, bot) + currentName := currentUser.User.FirstName + if currentUser.User.UserName != "" { + currentName = currentUser.User.UserName + } + text := `` + currentName + `` + ` currently has ` + `` + strconv.FormatInt(points, 10) + `` + ` 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) { + if !commonlogic.ContainsInt64(config.BotConfig.GayPoints.EnabledChats, update.Message.Chat.ID) || !commonlogic.ContainsInt64(config.BotConfig.GayPoints.ModifyUsers, update.Message.From.ID) { + 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 = "" + } + 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 { + 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 == "" { + 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", + } + 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 { + 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", + } + 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 + } + + var finalgp int64 + var currentPoints int64 + + if cmd == "addgp" { + currentPoints = sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID) + if currentPoints <= -1 { + finalgp = newPoints + } else { + finalgp = currentPoints + newPoints + } + } + + if cmd == "subtracktgp" { + currentPoints = sqlGetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID) + if currentPoints <= 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", + } + 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 { + finalgp = currentPoints - newPoints + } + } + + log.Println("[gaypoints] Changing gaypoints for user " + 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)) + + sqlSetGP(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID, finalgp) + + currentUser := botIO.GetUserByID(update.Message.Chat.ID, update.Message.ReplyToMessage.From.ID, bot) + currentName := currentUser.User.FirstName + if currentUser.User.UserName != "" { + currentName = currentUser.User.UserName + } + message := tgbotapi.MessageConfig{ + BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, + ParseMode: "html", + DisableWebPagePreview: false, + Text: `` + currentName + `` + ` now has ` + `` + strconv.FormatInt(finalgp, 10) + `` + ` gaypoints` + "\n", + } + botIO.SendMessage(message, bot) + return +} diff --git a/commands/gaypoints/sql.go b/commands/gaypoints/sql.go new file mode 100644 index 0000000..27c306d --- /dev/null +++ b/commands/gaypoints/sql.go @@ -0,0 +1,97 @@ +package gaypoints + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "log" + "strconv" +) + +var gpSelectUser *sql.Stmt + +var gpSelectChat *sql.Stmt + +var gpSet *sql.Stmt + +func sqlGetAllGP(chatid int64) (gaypoints map[int64]int64) { + rows, err := gpSelectChat.Query(chatid) + if err != nil { + log.Panicf("Failed to query gaypoints: %v\n", err) + } + + gaypoints = make(map[int64]int64) + var c int64 + var b int64 + for rows.Next() { + switch err := rows.Scan(&c, &b); err { + case sql.ErrNoRows: + gaypoints[c] = -1 + case nil: + gaypoints[c] = b + default: + log.Panicf("Failed to query gaypoints: %v\n", err) + + } + log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(c, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(b, 10)) + } + + return gaypoints + +} + +func sqlGetGP(chatid int64, userid int64) (gaypoints int64) { + row := gpSelectUser.QueryRow(chatid, userid) + + switch err := row.Scan(&gaypoints); err { + case sql.ErrNoRows: + gaypoints = -1 + case nil: + break + default: + log.Panicf("Failed to query gaypoints: %v\n", err) + + } + log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(gaypoints, 10)) + 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.Println("[gaypoints.sql] Set gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + " to " + strconv.FormatInt(gaypoints, 10)) +} + +func InitDB() { + const dbPath string = "./bloater.db" + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + log.Panicf("Failed to open sqlite database: %v\n", err) + } + + _, 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.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) + } + + 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) + } + + 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) + } + +} diff --git a/config.toml.example b/config.toml.example index 50073b9..5b29d51 100644 --- a/config.toml.example +++ b/config.toml.example @@ -1,13 +1,18 @@ APIToken = "073fv3b07tvrudgbdf" DebugMode = true -PrivilegedUsers = [1187583771] +PrivilegedUsers = [1212121212] [Nhentai] APIEndpoint = "0.0.0.0" Domain = "https://nhentai.net" -EnabledChats = [-1001737304244,-1001462089309,-1001616528600] +EnabledChats = [-00000,-111111,-22222] [Balonlyl] EnabledChats = [-1001462089309] BalonlylAT = "@Balonlyl" -TriggerWords = ["french","france","baguette","casque bleu"] \ No newline at end of file +TriggerWords = ["french","france","baguette","casque bleu"] + + +[GayPoints] +EnabledChats = [-00000,-111111,-22222] +ModifyUsers = [-00000,-111111,-22222] \ No newline at end of file diff --git a/config/config.go b/config/config.go index ec6a808..89a9775 100644 --- a/config/config.go +++ b/config/config.go @@ -22,6 +22,10 @@ type config struct { BalonlylAT string TriggerWords []string } + GayPoints struct { + EnabledChats []int64 + ModifyUsers []int64 + } } var BotConfig config diff --git a/docker-compose.yaml b/docker-compose.yaml index b5e581a..63fb407 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,6 +6,7 @@ services: restart: unless-stopped volumes: - /opt/bloaterbot/config.toml:/app/config.toml + - /opt/bloaterbot/bloater.db:/app/bloater.db networks: - expose networks: diff --git a/go.mod b/go.mod index b6f0173..badae20 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,8 @@ module watn3y/bloaterbotv3 go 1.20 -require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 - require ( - github.com/BurntSushi/toml v1.2.1 // indirect - gopkg.in/ini.v1 v1.67.0 // indirect + github.com/BurntSushi/toml v1.2.1 + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/mattn/go-sqlite3 v1.14.16 ) diff --git a/go.sum b/go.sum index caaf3c9..10188eb 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,5 @@ github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= -gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= -gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= diff --git a/main.go b/main.go index 2c82cb8..517dc4e 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,13 @@ package main import ( + "watn3y/bloaterbotv3/commands/gaypoints" "watn3y/bloaterbotv3/config" ) func main() { config.LoadConfig() + gaypoints.InitDB() bot() }