73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"log"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"git.lxtend.com/qqbot/auth"
|
|
"git.lxtend.com/qqbot/constants"
|
|
"git.lxtend.com/qqbot/handler"
|
|
"git.lxtend.com/qqbot/model"
|
|
)
|
|
|
|
func init() {
|
|
handler.RegisterFrontMatchHandler("用户权限", setUserLevel, constants.LEVEL_USER)
|
|
handler.RegisterHelpInform("用户权限", "用户权限 [用户] [权限] 设置用户权限。允许的权限有:根用户、管理员、信任、用户、拉黑、忽略")
|
|
}
|
|
|
|
var userLevelMap = map[string]constants.PermissionLevel{
|
|
"根用户": constants.LEVEL_ROOT,
|
|
"管理员": constants.LEVEL_ADMIN,
|
|
"信任": constants.LEVEL_TRUSTED,
|
|
"用户": constants.LEVEL_USER,
|
|
"拉黑": constants.LEVEL_BANNED,
|
|
"忽略": constants.LEVEL_IGNORE,
|
|
}
|
|
|
|
func setUserLevel(msg model.Message) (reply model.Reply) {
|
|
re := regexp.MustCompile(`\s+`)
|
|
tokens := re.Split(msg.RawMsg, -1)
|
|
if len(tokens) < 3 {
|
|
return model.Reply{
|
|
ReplyMsg: "参数不足",
|
|
ReferOriginMsg: true,
|
|
FromMsg: msg,
|
|
}
|
|
}
|
|
userText := tokens[1]
|
|
if userId, err := model.ParseAtMessage(userText); err == nil {
|
|
userText = userId.Data.QQ
|
|
}
|
|
log.Println(userText)
|
|
user, err := strconv.Atoi(userText)
|
|
if err != nil {
|
|
return model.Reply{
|
|
ReplyMsg: "用户解析失败",
|
|
ReferOriginMsg: true,
|
|
FromMsg: msg,
|
|
}
|
|
}
|
|
levelText := tokens[2]
|
|
level, ok := userLevelMap[levelText]
|
|
if !ok {
|
|
return model.Reply{
|
|
ReplyMsg: "权限不存在",
|
|
ReferOriginMsg: true,
|
|
FromMsg: msg,
|
|
}
|
|
}
|
|
if err := auth.SetPermission(int64(user), level); err != nil {
|
|
return model.Reply{
|
|
ReplyMsg: "权限设置失败",
|
|
ReferOriginMsg: true,
|
|
FromMsg: msg,
|
|
}
|
|
}
|
|
return model.Reply{
|
|
ReplyMsg: "权限设置成功",
|
|
ReferOriginMsg: true,
|
|
FromMsg: msg,
|
|
}
|
|
}
|