Spaces:
Sleeping
Sleeping
Download plugins/adminmgmt.go from Thaimozhi/Go-Filter-Bot: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/spaces/Thaimozhi/Go-Filter-Bot/resolve/refs%2Fpr%2F1/plugins/adminmgmt.go
- Command line
-
hf download hf://spaces/Thaimozhi/Go-Filter-Bot@refs/pr/1/plugins/adminmgmt.go
-
curl -L -o adminmgmt.go https://huggingface.co/spaces/Thaimozhi/Go-Filter-Bot/resolve/refs%2Fpr%2F1/plugins/adminmgmt.go
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 <user_id></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 <user_id></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 | |
| } | |