// 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 [reason] args := strings.SplitN(msg.Text, " ", 3) if len(args) < 2 { msg.Reply(bot, "Usage: /ban <user_id> [reason] 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("✅ User Banned\n\nID: %d\nReason: %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: /unban <user_id> 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 %d 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 := "🚫 Banned Users:\n\n" for i, u := range users { text += fmt.Sprintf("%d. %d — %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 – 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: /addfsub <channel_id>", &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 %d. 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 %d added to force-sub list.", chanID), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}, ) return nil } // DelFSub handles /delfsub – 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: /delfsub <channel_id>", &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 %d 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 := "📢 Force-Sub Channels:\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 (%d)", chat.Title, chanID) } text += fmt.Sprintf("%d. %s\n", i+1, name) } text += "\nUse /addfsub and /delfsub to manage." ctx.EffectiveMessage.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) return nil }