Go-Filter-Bot / plugins /adminmgmt.go
Thaimozhi's picture
first commit
891b1b2
Raw History Blame
3.97 kB
// Dynamic admin management – /addadmin and /deladmin commands.
// These store admins in the DB so they persist across restarts.
package plugins
import (
"fmt"
"strconv"
"strings"
"github.com/Jisin0/Go-Filter-Bot/utils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)
// AddAdmin handles the /addadmin command – adds a dynamic admin.
//
//nolint:errcheck
func AddAdmin(bot *gotgbot.Bot, ctx *ext.Context) error {
if !utils.IsAdmin(ctx.EffectiveUser.Id) {
ctx.EffectiveMessage.Reply(bot, "β›” Only bot admins can use this command!", &gotgbot.SendMessageOpts{})
return nil
}
msg := ctx.Message
var (
targetID int64
err error
)
if msg.ReplyToMessage != nil && msg.ReplyToMessage.From != nil {
targetID = msg.ReplyToMessage.From.Id
} else {
args := strings.SplitN(msg.Text, " ", 2)
if len(args) < 2 {
msg.Reply(bot, "Usage: <code>/addadmin &lt;user_id&gt;</code> or reply to a user's message", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
return nil
}
targetID, err = strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
if err != nil {
msg.Reply(bot, "❌ Invalid user ID.", &gotgbot.SendMessageOpts{})
return nil
}
}
if err := DB.AddDynamicAdmin(targetID); err != nil {
msg.Reply(bot, fmt.Sprintf("❌ Failed to add admin: %v", err), &gotgbot.SendMessageOpts{})
return nil
}
msg.Reply(bot,
fmt.Sprintf("βœ… User <code>%d</code> has been added as a bot admin.\n<i>This persists across restarts.</i>", targetID),
&gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML},
)
return nil
}
// DelAdmin handles the /deladmin command – removes a dynamic admin.
//
//nolint:errcheck
func DelAdmin(bot *gotgbot.Bot, ctx *ext.Context) error {
if !utils.IsAdmin(ctx.EffectiveUser.Id) {
ctx.EffectiveMessage.Reply(bot, "β›” Only bot admins can use this command!", &gotgbot.SendMessageOpts{})
return nil
}
msg := ctx.Message
var (
targetID int64
err error
)
if msg.ReplyToMessage != nil && msg.ReplyToMessage.From != nil {
targetID = msg.ReplyToMessage.From.Id
} else {
args := strings.SplitN(msg.Text, " ", 2)
if len(args) < 2 {
msg.Reply(bot, "Usage: <code>/deladmin &lt;user_id&gt;</code> or reply to a user's message", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
return nil
}
targetID, err = strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
if err != nil {
msg.Reply(bot, "❌ Invalid user ID.", &gotgbot.SendMessageOpts{})
return nil
}
}
if err := DB.RemoveDynamicAdmin(targetID); err != nil {
msg.Reply(bot, fmt.Sprintf("❌ Failed to remove admin: %v", err), &gotgbot.SendMessageOpts{})
return nil
}
msg.Reply(bot,
fmt.Sprintf("βœ… User <code>%d</code> has been removed from the admin list.", targetID),
&gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML},
)
return nil
}
// ListAdmins handles the /admins command – shows all current admins.
//
//nolint:errcheck
func ListAdmins(bot *gotgbot.Bot, ctx *ext.Context) error {
if !utils.IsAdmin(ctx.EffectiveUser.Id) {
ctx.EffectiveMessage.Reply(bot, "β›” Only bot admins can use this command!", &gotgbot.SendMessageOpts{})
return nil
}
text := "<b>πŸ‘‘ Bot Admins</b>\n\n"
// Static admins from env
from_env := DB.GetBotSettingsAdminsEnv()
if len(from_env) > 0 {
text += "<u>From ENV (permanent):</u>\n"
for i, id := range from_env {
text += fmt.Sprintf("%d. <code>%d</code>\n", i+1, id)
}
text += "\n"
}
// Dynamic admins from DB
dynamic := DB.GetDynamicAdmins()
if len(dynamic) > 0 {
text += "<u>Dynamic (added via /addadmin):</u>\n"
for i, id := range dynamic {
text += fmt.Sprintf("%d. <code>%d</code>\n", i+1, id)
}
} else {
text += "<i>No dynamic admins added.</i>"
}
text += "\n\n<i>Use /addadmin and /deladmin to manage dynamic admins.</i>"
ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
return nil
}