This commit is contained in:
Akulij 2025-03-20 03:06:10 +07:00
parent ccc98b4298
commit 14afe8ff4d

View File

@ -1,14 +1,14 @@
package main package main
import ( import (
"fmt"
"log"
"strings"
"strconv"
"errors" "errors"
"fmt"
"io"
"log"
"net/http" "net/http"
"os" "os"
"io" "strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@ -27,11 +27,11 @@ type User struct {
} }
func (u User) IsAdmin() bool { func (u User) IsAdmin() bool {
return u.RoleBitmask & 1 == 1 return u.RoleBitmask&1 == 1
} }
func (u User) IsEffectiveAdmin() bool { func (u User) IsEffectiveAdmin() bool {
return u.RoleBitmask & 0b10 == 0b10 return u.RoleBitmask&0b10 == 0b10
} }
type BotContent struct { type BotContent struct {
@ -121,11 +121,11 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
bc.db.First(&user, "id", UserID) bc.db.First(&user, "id", UserID)
if user == (User{}) { if user == (User{}) {
log.Printf("New user: [%d]", UserID) log.Printf("New user: [%d]", UserID)
user = User{ID: UserID , State: "start"} user = User{ID: UserID, State: "start"}
bc.db.Create(&user) bc.db.Create(&user)
} }
bc.db.Model(&user).Update("MsgCounter", user.MsgCounter + 1) bc.db.Model(&user).Update("MsgCounter", user.MsgCounter+1)
log.Printf("User[%d] messages: %d", user.ID, user.MsgCounter) log.Printf("User[%d] messages: %d", user.ID, user.MsgCounter)
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
@ -170,13 +170,13 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
bc.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, strconv.FormatInt(update.Message.Chat.ID, 10))) bc.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, strconv.FormatInt(update.Message.Chat.ID, 10)))
} else if possibleCommand == "/secret" && len(args) > 0 && args[0] == bc.cfg.AdminPass { } else if possibleCommand == "/secret" && len(args) > 0 && args[0] == bc.cfg.AdminPass {
bc.db.Model(&user).Update("state", "start") bc.db.Model(&user).Update("state", "start")
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask | 0b11) // set real admin ID (0b1) and effective admin toggle (0b10) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b11) // set real admin ID (0b1) and effective admin toggle (0b10)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are admin now!") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are admin now!")
bc.bot.Send(msg) bc.bot.Send(msg)
} else if possibleCommand == "/panel" && user.IsAdmin() { } else if possibleCommand == "/panel" && user.IsAdmin() {
if !user.IsEffectiveAdmin() { if !user.IsEffectiveAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask | 0b10) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b10)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You was in usermode, turned back to admin mode...") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You was in usermode, turned back to admin mode...")
bc.bot.Send(msg) bc.bot.Send(msg)
} }
@ -196,7 +196,7 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
bc.bot.Send(msg) bc.bot.Send(msg)
} else if possibleCommand == "/usermode" && user.IsEffectiveAdmin() { } else if possibleCommand == "/usermode" && user.IsEffectiveAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask & (^uint(0b10))) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask&(^uint(0b10)))
log.Printf("Set role bitmask (%b) for user: %d", user.RoleBitmask, user.ID) log.Printf("Set role bitmask (%b) for user: %d", user.RoleBitmask, user.ID)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Simulating user experience!") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Simulating user experience!")
bc.bot.Send(msg) bc.bot.Send(msg)
@ -326,9 +326,9 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
if strings.HasPrefix(update.CallbackQuery.Data, "update:") { if strings.HasPrefix(update.CallbackQuery.Data, "update:") {
Label := strings.Split(update.CallbackQuery.Data, ":")[1] Label := strings.Split(update.CallbackQuery.Data, ":")[1]
if Label == "preview_image" { if Label == "preview_image" {
bc.db.Model(&user).Update("state", "imgset:" + Label) bc.db.Model(&user).Update("state", "imgset:"+Label)
} else { } else {
bc.db.Model(&user).Update("state", "stringset:" + Label) bc.db.Model(&user).Update("state", "stringset:"+Label)
} }
bc.bot.Send(tgbotapi.NewMessage(user.ID, "Send me asset (text or picture (NOT as file))")) bc.bot.Send(tgbotapi.NewMessage(user.ID, "Send me asset (text or picture (NOT as file))"))
} }
@ -336,7 +336,7 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
if user.IsAdmin() { if user.IsAdmin() {
if update.CallbackQuery.Data == "panel" { if update.CallbackQuery.Data == "panel" {
if !user.IsEffectiveAdmin() { if !user.IsEffectiveAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask | 0b10) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b10)
msg := tgbotapi.NewMessage(user.ID, "You was in usermode, turned back to admin mode...") msg := tgbotapi.NewMessage(user.ID, "You was in usermode, turned back to admin mode...")
bc.bot.Send(msg) bc.bot.Send(msg)
} }
@ -366,7 +366,7 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
var admins []User var admins []User
bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins) bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins)
for _, admin := range admins { for _, admin := range admins {
bc.bot.Send(tgbotapi.NewMessage(admin.ID, "ChannelID is set to " + strconv.FormatInt(post.SenderChat.ID, 10))) bc.bot.Send(tgbotapi.NewMessage(admin.ID, "ChannelID is set to "+strconv.FormatInt(post.SenderChat.ID, 10)))
delcmd := tgbotapi.NewDeleteMessage(post.SenderChat.ID, post.MessageID) delcmd := tgbotapi.NewDeleteMessage(post.SenderChat.ID, post.MessageID)
bc.bot.Send(delcmd) bc.bot.Send(delcmd)
} }