Go-Filter-Bot / plugins /admin.go
Thaimozhi's picture
first commit
891b1b2
Raw History Blame Contribute Delete
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 &lt;user_id&gt; [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 &lt;user_id&gt;</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 &lt;channel_id&gt;</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 &lt;channel_id&gt;</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
}