refactor: 重构handler系统

This commit is contained in:
lixiangwuxian 2024-10-20 02:40:02 +08:00
parent 6dad9bb76f
commit 03ee846b7a
4 changed files with 26 additions and 9 deletions

View File

@ -1,7 +1,7 @@
package auth
import (
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/sqlite3"
)
@ -17,7 +17,7 @@ func init() {
sqlite3.TryCreateTable(authTableCreateSQL)
}
func HasPermission(qqID int64, level int) bool {
func HasPermission(qqID int64, level constants.PermissionLevel) bool {
auth, err := getAuth(qqID)
if err != nil {
return false
@ -25,7 +25,7 @@ func HasPermission(qqID int64, level int) bool {
return auth.Role <= level
}
func TryExecHandler(msg model.Message, level int, handler handler.Handler) (reply model.Reply) {
func TryExecHandler(msg model.Message, level constants.PermissionLevel, handler model.Handler) (reply model.Reply) {
if HasPermission(msg.UserId, level) {
return handler(msg)
}

View File

@ -1,6 +1,8 @@
package auth
import "git.lxtend.com/qqbot/constants"
type Auth struct {
Qqid string `json:"qqid" db:"qqid"`
Role int `json:"role" db:"role"`
Qqid string `json:"qqid" db:"qqid"`
Role constants.PermissionLevel `json:"role" db:"role"`
}

View File

@ -1,8 +1,11 @@
package constants
type PermissionLevel int
const (
LEVEL_ROOT = 0
LEVEL_ADMIN = 10
LEVEL_USER = 20 //default
LEVEL_BANNED = 100
LEVEL_ROOT = 0
LEVEL_ADMIN = 50
LEVEL_TRUSTED = 100
LEVEL_USER = 200 //default
LEVEL_BANNED = 400
)

12
model/handler.go Normal file
View File

@ -0,0 +1,12 @@
package model
import "git.lxtend.com/qqbot/constants"
type Handler func(msg Message) (reply Reply)
type TryCatchHandler func(msg Message) (reply Reply, catched bool)
type HandlerInfo[H Handler | TryCatchHandler] struct {
Trigger string
Handler H
Level constants.PermissionLevel
}