Refactor post handling and add command setup for Telegram bot

This commit is contained in:
dela
2026-02-05 00:52:29 +08:00
parent d82badc6e3
commit 8a6859269c
9 changed files with 612 additions and 21 deletions

View File

@@ -4,13 +4,40 @@ 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 !b.cfg.IsAdmin(c.Sender().ID) {
// 群组中的非命令消息,非管理员直接忽略不回复
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)
}
}
}