Files
mygoTgChanBot/internal/telegram/middleware.go

44 lines
1.1 KiB
Go

package telegram
import (
tele "gopkg.in/telebot.v3"
)
// isAdmin 检查用户是否为管理员(配置文件或数据库)
func (b *Bot) isAdmin(userID int64) bool {
return b.cfg.IsAdmin(userID) || b.storage.IsAdmin(userID)
}
func (b *Bot) AdminMiddleware() tele.MiddlewareFunc {
return func(next tele.HandlerFunc) tele.HandlerFunc {
return func(c tele.Context) error {
// 群组中的非命令消息,非管理员直接忽略不回复
if c.Chat().Type != tele.ChatPrivate {
msg := c.Message()
if msg != nil && !msg.IsService() && (msg.Text == "" || msg.Text[0] != '/') {
if !b.isAdmin(c.Sender().ID) {
return nil // 静默忽略
}
}
}
if !b.isAdmin(c.Sender().ID) {
return c.Reply("⛔ 无权限访问")
}
return next(c)
}
}
}
// SuperAdminMiddleware 超级管理员中间件(仅配置文件中的管理员)
func (b *Bot) SuperAdminMiddleware() tele.MiddlewareFunc {
return func(next tele.HandlerFunc) tele.HandlerFunc {
return func(c tele.Context) error {
if !b.cfg.IsSuperAdmin(c.Sender().ID) {
return c.Reply("⛔ 需要超级管理员权限")
}
return next(c)
}
}
}