Skip to content

Commit

Permalink
✨ 新增忽略账号功能
Browse files Browse the repository at this point in the history
  • Loading branch information
WhitePaper233 committed Aug 2, 2022
1 parent b6e1e1b commit a6a8221
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
7 changes: 5 additions & 2 deletions config.release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
SuperUsers: []

# 命令前缀
CommandPrefix: "/"
CommandPrefix: "."

# 机器人别名
NickNames: ["cocoa", "Cocoa", "心爱"]

# 默认权限设置
DefaultLevel: 5

# 忽略响应账号
IgnoreUsers: []

# go-cqhttp设置
Server:
Address: "127.0.0.1"
Port: 6700
Port: 8080
Token: ""

# 数据库设置
Expand Down
9 changes: 6 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
SuperUsers: []

# 命令前缀
CommandPrefix: "/"
CommandPrefix: "."

# 机器人别名
NickNames: ["cocoa", "Cocoa", "心爱"]

# 默认权限设置
DefaultLevel: 5

# 忽略响应账号
IgnoreUsers: []

# go-cqhttp设置
Server:
Address: "127.0.0.1"
Port: 6700
Port: 8080
Token: ""

# 数据库设置
Expand All @@ -25,4 +28,4 @@ Database:
DatabaseName: "cocoa"

# 调试模式
DEBUG: true
DEBUG: false
1 change: 1 addition & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
CommandPrefix string `yaml:"CommandPrefix"`
NickNames []string `yaml:"NickNames"`
DefaultLevel int64 `yaml:"DefaultLevel"`
IgnoreUsers []int64 `yaml:"IgnoreUsers"`

Server struct {
Address string `yaml:"Address"`
Expand Down
5 changes: 4 additions & 1 deletion utils/control/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ func getEngine(pluginMetadata Metadata, echoLevel EchoLevel) zero.Engine {
engine := zero.Engine{}

// 添加prehandler
// 添加忽略账号prehandler
engine.UsePreHandler(ignoreUserChecker)

// 插件prehandler
engine.UsePreHandler(pluginCheck(pluginMetadata, echoLevel))
engine.UsePreHandler(pluginChecker(pluginMetadata, echoLevel))

// 返回engine
return engine
Expand Down
20 changes: 19 additions & 1 deletion utils/control/prehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
"gorm.io/gorm"

"github.com/DaydreamCafe/Cocoa/V2/src/config"
"github.com/DaydreamCafe/Cocoa/V2/src/conn"
"github.com/DaydreamCafe/Cocoa/V2/src/model"
)

// 通用全局忽略账号prehandler, 用于确认账号是否被设置为忽略
func ignoreUserChecker(ctx *zero.Ctx) bool {
// 加载配置文件
cfg := config.Config{}
cfg.Load()

// 检查该账号是否被忽略
flag := true
for _, userID := range(cfg.IgnoreUsers) {
if userID == ctx.Event.UserID {
flag = false
}
}

return flag
}

// 通用的全局插件prehandler, 用于确认插件是否处于可用状态
func pluginCheck(pluginMetadata Metadata, echoLevel EchoLevel) zero.Rule {
func pluginChecker(pluginMetadata Metadata, echoLevel EchoLevel) zero.Rule {
return func(ctx *zero.Ctx) bool {
// 连接数据库
db, err := conn.GetDB()
Expand Down

0 comments on commit a6a8221

Please sign in to comment.