Spaces:
Sleeping
Sleeping
Download plugins/admin.go from Thaimozhi/Go-Filter-Bot: direct link, hf CLI and curl.
- Browser
- Download file 7.82 kB
-
https://huggingface.co/spaces/Thaimozhi/Go-Filter-Bot/resolve/main/plugins/admin.go
- Command line
-
hf download hf://spaces/Thaimozhi/Go-Filter-Bot/plugins/admin.go
-
curl -L -o admin.go https://huggingface.co/spaces/Thaimozhi/Go-Filter-Bot/resolve/main/plugins/admin.go
7.82 kB
| // Admin commands: ban, unban, banned list, force-sub channel management. | |
| package plugins | |
| import ( | |
| "fmt" | |
| "strconv" | |
| "strings" | |
| "github.com/Jisin0/Go-Filter-Bot/utils" | |
| "github.com/PaulSonOfLars/gotgbot/v2" | |
| "github.com/PaulSonOfLars/gotgbot/v2/ext" | |
| ) | |
| // ββ Ban / Unban βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // BanUser handles the /ban command. | |
| // | |
| //nolint:errcheck | |
| func BanUser(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 | |
| reason = "No reason provided" | |
| err error | |
| ) | |
| // /ban reply: get user from replied message | |
| if msg.ReplyToMessage != nil && msg.ReplyToMessage.From != nil { | |
| targetID = msg.ReplyToMessage.From.Id | |
| args := strings.SplitN(msg.Text, " ", 2) | |
| if len(args) >= 2 { | |
| reason = args[1] | |
| } | |
| } else { | |
| // /ban <user_id> [reason] | |
| args := strings.SplitN(msg.Text, " ", 3) | |
| if len(args) < 2 { | |
| msg.Reply(bot, "Usage: <code>/ban <user_id> [reason]</code> or reply to a 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 len(args) == 3 { | |
| reason = args[2] | |
| } | |
| } | |
| if utils.IsAdmin(targetID) { | |
| msg.Reply(bot, "β You cannot ban another bot admin!", &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| if err := DB.BanUser(targetID, reason); err != nil { | |
| msg.Reply(bot, fmt.Sprintf("β Failed to ban: %v", err), &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| msg.Reply(bot, | |
| fmt.Sprintf("β <b>User Banned</b>\n\n<b>ID:</b> <code>%d</code>\n<b>Reason:</b> %s", targetID, reason), | |
| &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}, | |
| ) | |
| return nil | |
| } | |
| // UnbanUser handles the /unban command. | |
| // | |
| //nolint:errcheck | |
| func UnbanUser(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>/unban <user_id></code> or reply to a 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.UnbanUser(targetID); err != nil { | |
| msg.Reply(bot, fmt.Sprintf("β Failed to unban: %v", err), &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| msg.Reply(bot, | |
| fmt.Sprintf("β User <code>%d</code> has been unbanned.", targetID), | |
| &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}, | |
| ) | |
| return nil | |
| } | |
| // BannedList handles the /banned command β lists all banned users. | |
| // | |
| //nolint:errcheck | |
| func BannedList(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 | |
| } | |
| users, err := DB.GetBannedUsers() | |
| if err != nil { | |
| ctx.EffectiveMessage.Reply(bot, fmt.Sprintf("β Error: %v", err), &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| if len(users) == 0 { | |
| ctx.EffectiveMessage.Reply(bot, "β No banned users.", &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| text := "<b>π« Banned Users:</b>\n\n" | |
| for i, u := range users { | |
| text += fmt.Sprintf("%d. <code>%d</code> β %s\n", i+1, u.ID, u.Reason) | |
| } | |
| ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) | |
| return nil | |
| } | |
| // ββ Force-Sub Management ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // AddFSub handles /addfsub <channel_id> β adds a channel to force-sub list. | |
| // | |
| //nolint:errcheck | |
| func AddFSub(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 | |
| } | |
| args := strings.SplitN(ctx.Message.Text, " ", 2) | |
| if len(args) < 2 { | |
| ctx.Message.Reply(bot, "Usage: <code>/addfsub <channel_id></code>", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) | |
| return nil | |
| } | |
| chanID, err := strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64) | |
| if err != nil { | |
| ctx.Message.Reply(bot, "β Invalid channel ID. Use the numeric ID (e.g. -1001234567890).", &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| // Verify bot is in the channel | |
| _, err = bot.GetChat(chanID, nil) | |
| if err != nil { | |
| ctx.Message.Reply(bot, fmt.Sprintf("β Cannot access channel <code>%d</code>. Make sure I'm an admin there.", chanID), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) | |
| return nil | |
| } | |
| if err := DB.AddForceSubChannel(chanID); err != nil { | |
| ctx.Message.Reply(bot, fmt.Sprintf("β Failed to add: %v", err), &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| ctx.Message.Reply(bot, | |
| fmt.Sprintf("β Channel <code>%d</code> added to force-sub list.", chanID), | |
| &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}, | |
| ) | |
| return nil | |
| } | |
| // DelFSub handles /delfsub <channel_id> β removes a channel from force-sub list. | |
| // | |
| //nolint:errcheck | |
| func DelFSub(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 | |
| } | |
| args := strings.SplitN(ctx.Message.Text, " ", 2) | |
| if len(args) < 2 { | |
| ctx.Message.Reply(bot, "Usage: <code>/delfsub <channel_id></code>", &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) | |
| return nil | |
| } | |
| chanID, err := strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64) | |
| if err != nil { | |
| ctx.Message.Reply(bot, "β Invalid channel ID.", &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| if err := DB.RemoveForceSubChannel(chanID); err != nil { | |
| ctx.Message.Reply(bot, fmt.Sprintf("β Failed to remove: %v", err), &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| ctx.Message.Reply(bot, | |
| fmt.Sprintf("β Channel <code>%d</code> removed from force-sub list.", chanID), | |
| &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}, | |
| ) | |
| return nil | |
| } | |
| // ListFSub handles /fsub β lists all force-sub channels. | |
| // | |
| //nolint:errcheck | |
| func ListFSub(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 | |
| } | |
| channels := DB.GetForceSubChannels() | |
| if len(channels) == 0 { | |
| ctx.EffectiveMessage.Reply(bot, "βΉοΈ No force-sub channels configured.", &gotgbot.SendMessageOpts{}) | |
| return nil | |
| } | |
| text := "<b>π’ Force-Sub Channels:</b>\n\n" | |
| for i, chanID := range channels { | |
| name := fmt.Sprintf("%d", chanID) | |
| chat, err := bot.GetChat(chanID, nil) | |
| if err == nil { | |
| name = fmt.Sprintf("%s (<code>%d</code>)", chat.Title, chanID) | |
| } | |
| text += fmt.Sprintf("%d. %s\n", i+1, name) | |
| } | |
| text += "\n<i>Use /addfsub and /delfsub to manage.</i>" | |
| ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) | |
| return nil | |
| } | |