// 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: /addadmin <user_id> 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 %d has been added as a bot admin.\nThis persists across restarts.", 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: /deladmin <user_id> 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 %d 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 := "👑 Bot Admins\n\n"
// Static admins from env
from_env := DB.GetBotSettingsAdminsEnv()
if len(from_env) > 0 {
text += "From ENV (permanent):\n"
for i, id := range from_env {
text += fmt.Sprintf("%d. %d\n", i+1, id)
}
text += "\n"
}
// Dynamic admins from DB
dynamic := DB.GetDynamicAdmins()
if len(dynamic) > 0 {
text += "Dynamic (added via /addadmin):\n"
for i, id := range dynamic {
text += fmt.Sprintf("%d. %d\n", i+1, id)
}
} else {
text += "No dynamic admins added."
}
text += "\n\nUse /addadmin and /deladmin to manage dynamic admins."
ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML})
return nil
}