feat: 更新权限系统

This commit is contained in:
lixiangwuxian 2024-10-20 02:46:56 +08:00
parent 03ee846b7a
commit 049b5774bf
15 changed files with 77 additions and 51 deletions

View File

@ -4,20 +4,26 @@ import (
"log"
"strconv"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/service/beatleader"
)
func init() {
handler.RegisterHandler("查bl", getMyBL)
handler.RegisterHandler("查bl", getMyBL, constants.LEVEL_USER)
handler.RegisterHelpInform("查bl", "查看您的最新分数")
handler.RegisterHandler("绑定bl", bindBL)
handler.RegisterHandler("解绑bl", unbindBL)
handler.RegisterHandler("最新bl", getMyRecentScore)
handler.RegisterHandler("最热bl", getRecentScore)
handler.RegisterHandler("截图bl", screenShotBL)
handler.RegisterHandler("jbl", screenShotBL)
handler.RegisterHandler("绑定bl", bindBL, constants.LEVEL_USER)
handler.RegisterHelpInform("绑定bl", "绑定您的beatleader账号")
handler.RegisterHandler("解绑bl", unbindBL, constants.LEVEL_USER)
handler.RegisterHelpInform("解绑bl", "解绑您的beatleader账号")
handler.RegisterHandler("最新bl", getMyRecentScore, constants.LEVEL_USER)
handler.RegisterHelpInform("最新bl", "查看您的最新游戏记录")
handler.RegisterHandler("最热bl", getRecentScore, constants.LEVEL_USER)
handler.RegisterHelpInform("最热bl", "查看全大陆的最新游戏记录")
handler.RegisterHandler("截bl", screenShotBL, constants.LEVEL_USER)
handler.RegisterHandler("jbl", screenShotBL, constants.LEVEL_USER)
handler.RegisterHelpInform("截bl/jbl", "beatleader主页截图")
}
func getMyBL(msg model.Message) (reply model.Reply) {

View File

@ -1,12 +1,13 @@
package echo
import (
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
func init() {
handler.RegisterHandler("echo", echo)
handler.RegisterHandler("echo", echo, constants.LEVEL_USER)
handler.RegisterHelpInform("echo", "再说一遍")
}

View File

@ -1,13 +1,14 @@
package getweb
import (
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/util"
)
func init() {
handler.RegisterHandler("上网", getweb)
handler.RegisterHandler("上网", getweb, constants.LEVEL_TRUSTED)
handler.RegisterHelpInform("上网", "上网 <URL> 截取网页")
}

View File

@ -4,38 +4,45 @@ import (
"log"
"strings"
"git.lxtend.com/qqbot/auth"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/model"
)
type Handler func(msg model.Message) (reply model.Reply)
type TryCatchHandler func(msg model.Message) (reply model.Reply, catched bool)
var handlers = make(map[string]Handler)
var frontMatchHandlers = make(map[string]Handler)
var liveHandlers = make(map[int64]map[int64]TryCatchHandler)
var livePrivateHandlers = make(map[int64]TryCatchHandler)
var handlers = make(map[string]model.HandlerInfo[model.Handler])
var frontMatchHandlers = make(map[string]model.HandlerInfo[model.Handler])
var liveHandlers = make(map[int64]map[int64]model.TryCatchHandler)
var livePrivateHandlers = make(map[int64]model.TryCatchHandler)
var HelpInforms = make(map[string]string)
var privateAllHandler Handler
var privateAllHandler model.Handler
func RegisterPrivateHandler(handler Handler) {
func RegisterPrivateHandler(handler model.Handler) {
privateAllHandler = handler
}
func RegisterHandler(trigger string, handler Handler) {
handlers[trigger] = handler
func RegisterHandler(trigger string, handler model.Handler, level constants.PermissionLevel) {
handlers[trigger] = model.HandlerInfo[model.Handler]{
Trigger: trigger,
Handler: handler,
Level: level,
}
}
func RegisterFrontMatchHandler(trigger string, handler Handler) {
frontMatchHandlers[trigger] = handler
func RegisterFrontMatchHandler(trigger string, handler model.Handler, level constants.PermissionLevel) {
frontMatchHandlers[trigger] = model.HandlerInfo[model.Handler]{
Trigger: trigger,
Handler: handler,
Level: level,
}
}
func RegisterLiveHandler(groupID int64, userID int64, handler TryCatchHandler) { //userID=-1 means for all users in groupID
func RegisterLiveHandler(groupID int64, userID int64, handler model.TryCatchHandler) { //userID=-1 means for all users in groupID
if groupID == 0 {
livePrivateHandlers[userID] = handler
return
}
if _, ok := liveHandlers[groupID]; !ok {
liveHandlers[groupID] = make(map[int64]TryCatchHandler)
liveHandlers[groupID] = make(map[int64]model.TryCatchHandler)
}
liveHandlers[groupID][userID] = handler
}
@ -74,12 +81,12 @@ func MsgInHandler(msg model.Message) (reply model.Reply) {
}
for trigger, handler := range frontMatchHandlers {
if strings.HasPrefix(msg.RawMsg, trigger) {
return handler(msg)
return auth.TryExecHandler(msg, handler.Level, handler.Handler)
}
}
msgArray := strings.Split(msg.RawMsg, " ")
if handler, ok := handlers[msgArray[0]]; ok {
return handler(msg)
return auth.TryExecHandler(msg, handler.Level, handler.Handler)
}
if !msg.GroupInfo.IsGroupMsg && privateAllHandler != nil {
return privateAllHandler(msg)

View File

@ -6,6 +6,7 @@ import (
"strings"
"time"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/util"
@ -18,7 +19,7 @@ var (
)
func init() {
handler.RegisterHandler("校长", headmasterHandler)
handler.RegisterHandler("校长", headmasterHandler, constants.LEVEL_USER)
handler.RegisterPrivateHandler(headmasterHandler)
}

View File

@ -1,12 +1,13 @@
package health
import (
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
func init() {
handler.RegisterHandler("health", health)
handler.RegisterHandler("health", health, constants.LEVEL_USER)
}
func health(msg model.Message) (reply model.Reply) {

View File

@ -1,12 +1,13 @@
package help
import (
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
func init() {
handler.RegisterHandler("!help", help)
handler.RegisterHandler("!help", help, constants.LEVEL_BANNED)
}
func help(msg model.Message) (reply model.Reply) {

View File

@ -3,6 +3,7 @@ package jrrp
import (
"fmt"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/service/jrrp"
@ -12,8 +13,8 @@ var jrrpInstance *jrrp.Jrrp
func init() {
jrrpInstance = jrrp.NewJrrp()
handler.RegisterHandler("今日人品", jrrpHandler)
handler.RegisterHandler("jrrp", jrrpHandler)
handler.RegisterHandler("今日人品", jrrpHandler, constants.LEVEL_USER)
handler.RegisterHandler("jrrp", jrrpHandler, constants.LEVEL_USER)
handler.RegisterHelpInform("今日人品/jrrp", "今日人品或jrrp 查询今日人品")
}

View File

@ -7,6 +7,7 @@ import (
"os/exec"
"time"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
@ -14,10 +15,10 @@ import (
var hasVaildBuild = true
func init() {
handler.RegisterHandler("/重启bot", restart)
handler.RegisterHandler("/构建bot", build)
handler.RegisterHandler("/构建重启", buildAndRestart)
handler.RegisterHandler("/gitpull", pullCode)
handler.RegisterHandler("/重启bot", restart, constants.LEVEL_ADMIN)
handler.RegisterHandler("/构建bot", build, constants.LEVEL_ADMIN)
handler.RegisterHandler("/构建重启", buildAndRestart, constants.LEVEL_ADMIN)
handler.RegisterHandler("/gitpull", pullCode, constants.LEVEL_ADMIN)
}
func restart(msg model.Message) model.Reply {

View File

@ -6,13 +6,14 @@ import (
"strconv"
"time"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"golang.org/x/exp/rand"
)
func init() {
handler.RegisterHandler("roll", roll)
handler.RegisterHandler("roll", roll, constants.LEVEL_USER)
handler.RegisterHelpInform("roll", "roll点")
}

View File

@ -1,12 +1,13 @@
package say
import (
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
func init() {
handler.RegisterHandler("kw", say)
handler.RegisterHandler("kw", say, constants.LEVEL_TRUSTED)
}
func say(msg model.Message) (reply model.Reply) {

View File

@ -5,24 +5,25 @@ import (
"strconv"
"strings"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/service/scoresaber"
)
func init() {
handler.RegisterHandler("查ss", getSSProfile)
handler.RegisterHandler("查ss", getSSProfile, constants.LEVEL_USER)
handler.RegisterHelpInform("查ss", "查看您的最新分数")
handler.RegisterHandler("绑定ss", bindSS)
handler.RegisterHandler("绑定ss", bindSS, constants.LEVEL_USER)
handler.RegisterHelpInform("绑定ss", "绑定您的scoresaber账号")
handler.RegisterHandler("解绑ss", unbindSS)
handler.RegisterHandler("解绑ss", unbindSS, constants.LEVEL_USER)
handler.RegisterHelpInform("解绑ss", "解绑您的scoresaber账号")
handler.RegisterHandler("最新ss", getMyRecentScore)
handler.RegisterHandler("最新ss", getMyRecentScore, constants.LEVEL_USER)
handler.RegisterHelpInform("最新ss", "查看您的最新游戏记录")
handler.RegisterHandler("最热ss", getRecentScore)
handler.RegisterHandler("最热ss", getRecentScore, constants.LEVEL_USER)
handler.RegisterHelpInform("最热ss", "查看全大陆的最新游戏记录")
handler.RegisterHandler("截ss", screenshotSS)
handler.RegisterHandler("jss", screenshotSS)
handler.RegisterHandler("截ss", screenshotSS, constants.LEVEL_USER)
handler.RegisterHandler("jss", screenshotSS, constants.LEVEL_USER)
handler.RegisterHelpInform("截ss/jss", "scoresaber主页截图")
}

View File

@ -4,14 +4,15 @@ import (
"fmt"
"strconv"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
func init() {
// Register the handler with the server
handler.RegisterHandler("提单", newTicket)
handler.RegisterHandler("结单", doneTicket)
handler.RegisterHandler("提单", newTicket, constants.LEVEL_USER)
handler.RegisterHandler("结单", doneTicket, constants.LEVEL_ROOT)
}
func newTicket(msg model.Message) model.Reply {

View File

@ -8,15 +8,16 @@ import (
"regexp"
"strings"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/util"
)
func init() {
handler.RegisterFrontMatchHandler("[CQ:json", cqJsonUrlParser)
handler.RegisterFrontMatchHandler("http://", plainTextUrlParser)
handler.RegisterFrontMatchHandler("https://", plainTextUrlParser)
handler.RegisterFrontMatchHandler("[CQ:json", cqJsonUrlParser, constants.LEVEL_USER)
handler.RegisterFrontMatchHandler("http://", plainTextUrlParser, constants.LEVEL_USER)
handler.RegisterFrontMatchHandler("https://", plainTextUrlParser, constants.LEVEL_USER)
}
func plainTextUrlParser(msg model.Message) (reply model.Reply) {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/service/xibao"
@ -11,9 +12,9 @@ import (
)
func init() {
handler.RegisterHandler("喜报", xiBao)
handler.RegisterHandler("喜报", xiBao, constants.LEVEL_USER)
handler.RegisterHelpInform("喜报", "喜报 [内容] 生成喜报图片,支持换行")
handler.RegisterHandler("悲报", beiBao)
handler.RegisterHandler("悲报", beiBao, constants.LEVEL_USER)
handler.RegisterHelpInform("悲报", "悲报 [内容] 生成悲报图片,支持换行")
}