Skip to content

Commit

Permalink
feat: global config need OnlyToMe
Browse files Browse the repository at this point in the history
  • Loading branch information
RicheyJang committed Feb 25, 2022
1 parent 7087fbc commit 276125c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
23 changes: 23 additions & 0 deletions manager/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"time"

"github.com/RicheyJang/PaimengBot/utils"
"github.com/RicheyJang/PaimengBot/utils/consts"

"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"github.com/spf13/viper"
"github.com/syndtr/goleveldb/leveldb"
zero "github.com/wdvxdr1123/ZeroBot"
"gorm.io/gorm"
Expand Down Expand Up @@ -96,9 +98,30 @@ func (p *PluginProxy) checkRules(rules ...zero.Rule) []zero.Rule {
if p.c.IsSuperOnly {
return append(rules, zero.SuperUserPermission)
}
if p.DoNotNeedOnlyToMe() {
for i, rule := range rules {
if utils.IsSameFunc(rule, zero.OnlyToMe) {
rules = append(rules[:i], rules[i+1:]...)
break
}
}
}
return rules
}

// DoNotNeedOnlyToMe 是否不需要OnlyToMe
func (p *PluginProxy) DoNotNeedOnlyToMe() bool {
c := viper.Get(consts.AlwaysCallKey)
switch c.(type) {
case string, bool:
return cast.ToBool(c)
case []string, []interface{}:
return utils.StringSliceContain(cast.ToStringSlice(c), p.key)
default:
return false
}
}

// ---- 定时任务 ----

// AddScheduleFunc 添加定时任务,并自动启动
Expand Down
7 changes: 0 additions & 7 deletions plugins/chat/deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ var dealers = []Dealer{ // 在此添加新的Dealer即可,其它事宜会自
IDoNotKnow,
}

func checkOnlyToMeWithConfig(ctx *zero.Ctx) bool {
if !proxy.GetConfigBool("onlytome") {
return true
}
return ctx.Event.IsToMe
}

func dealChat(ctx *zero.Ctx) {
question := ctx.ExtractPlainText()
// 优先尝试自定义问答
Expand Down
4 changes: 1 addition & 3 deletions plugins/chat/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ config-plugin文件配置项:
chat.default.self 自我介绍内容
chat.default.donotknow 无法处理某消息时的回答内容,留空则不回答;{nickname}代表机器人昵称
chat.diylevel 自定义问答功能所需的最低管理员权限等级,默认为5,设为0则非群管理员用户也可自定义
chat.onlytome 在群中调用已自定义的问句时是(true)否(false)需要加上机器人名字前缀或者@机器人
chat.at 在群聊中,机器人的回复是(true)否(false)@提问者`,
}

Expand All @@ -56,12 +55,11 @@ func init() {
proxy.OnCommands([]string{"删除对话", "删除问答"}).SetBlock(true).SetPriority(5).Handle(delDialogue)
proxy.OnCommands([]string{"已有对话", "已有问答"}).SetBlock(true).SetPriority(5).Handle(showDialogue)

proxy.OnMessage(checkOnlyToMeWithConfig).SetBlock(true).SetPriority(10).Handle(dealChat)
proxy.OnMessage(zero.OnlyToMe).SetBlock(true).SetPriority(10).Handle(dealChat)

proxy.AddConfig("default.self", "我是派蒙,最好的伙伴!\n才不是应急食品呢")
proxy.AddConfig("default.donotknow", "{nickname}不知道哦")
proxy.AddConfig(DIYDialogueLevelKey, 5)
proxy.AddConfig("onlytome", true)
proxy.AddConfig("at", true)
}

Expand Down
3 changes: 2 additions & 1 deletion pre_works.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func init() {
viper.SetDefault("db.passwd", "password")
viper.SetDefault("db.name", "database")
// 其它配置
viper.SetDefault("tmp.maxcount", 1000) // 同种类临时文件同时存在的最大数量
viper.SetDefault("tmp.maxcount", 1000) // 同种类临时文件同时存在的最大数量
viper.SetDefault(consts.AlwaysCallKey, false) // 是否可以自由调用(完全去除onlytome),不支持热更新
// 此init会在manager.common前被调用,随后manager.common.init调用DoPreWorks
}

Expand Down
2 changes: 2 additions & 0 deletions utils/consts/configs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package consts

const AlwaysCallKey = "plugins-always-call" // 配置无需onlytome的插件时,所用的配置项Key

// 通过Plugin Config来控制一些PaimengBot所给予的基本能力的 配置项

const PluginConfigCDKey = "cd" // 配置各插件CD限流时长时,所用的配置项Key
Expand Down

0 comments on commit 276125c

Please sign in to comment.