Spaces:
Sleeping
Sleeping
File size: 3,969 Bytes
891b1b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | // 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
}
|