词库增加设置概率功能

This commit is contained in:
源文雨 2022-12-02 20:24:44 +08:00
parent 83778afd51
commit 3b1ad2250f
2 changed files with 36 additions and 6 deletions

View File

@ -252,6 +252,7 @@ zerobot [-h] [-n nickname] [-t token] [-u url] [-p prefix] [-d|w] [-c|s config.j
`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/thesaurus"` `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/thesaurus"`
- [x] 切换[kimo|傲娇|可爱]词库 - [x] 切换[kimo|傲娇|可爱]词库
- [x] 设置词库触发概率0.x (0<x<9)
</details> </details>
<details> <details>

View File

@ -21,7 +21,7 @@ func init() {
engine := control.Register("thesaurus", &ctrl.Options[*zero.Ctx]{ engine := control.Register("thesaurus", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false, DisableOnDefault: false,
Brief: "词典匹配回复", Brief: "词典匹配回复",
Help: "- 切换[kimo|傲娇|可爱]词库", Help: "- 切换[kimo|傲娇|可爱]词库\n- 设置词库触发概率0.x (0<x<9)",
PublicDataFolder: "Chat", PublicDataFolder: "Chat",
}) })
engine.OnRegex(`^切换(kimo|傲娇|可爱)词库$`, zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { engine.OnRegex(`^切换(kimo|傲娇|可爱)词库$`, zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
@ -34,15 +34,40 @@ func init() {
if gid == 0 { if gid == 0 {
gid = -ctx.Event.UserID gid = -ctx.Event.UserID
} }
var err error d := c.GetData(gid)
t := int64(0)
switch ctx.State["regex_matched"].([]string)[1] { switch ctx.State["regex_matched"].([]string)[1] {
case "kimo": case "kimo":
err = c.SetData(gid, tKIMO) t = tKIMO
case "傲娇": case "傲娇":
err = c.SetData(gid, tDERE) t = tDERE
case "可爱": case "可爱":
err = c.SetData(gid, tKAWA) t = tKAWA
} }
err := c.SetData(gid, (d&^3)|t)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
})
engine.OnRegex(`^设置词库触发概率\s*0.(\d)$`, zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if !ok {
ctx.SendChain(message.Text("ERROR: 找不到 manager"))
return
}
n := ctx.State["regex_matched"].([]string)[1][0] - '0'
if n <= 0 || n >= 9 {
ctx.SendChain(message.Text("ERROR: 概率越界"))
return
}
n-- // 0~7
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
d := c.GetData(gid)
err := c.SetData(gid, (d&3)|(int64(n)<<59))
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -142,6 +167,9 @@ const (
func canmatch(typ int64) zero.Rule { func canmatch(typ int64) zero.Rule {
return func(ctx *zero.Ctx) bool { return func(ctx *zero.Ctx) bool {
if zero.HasPicture(ctx) {
return false
}
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if !ok { if !ok {
return false return false
@ -150,7 +178,8 @@ func canmatch(typ int64) zero.Rule {
if gid == 0 { if gid == 0 {
gid = -ctx.Event.UserID gid = -ctx.Event.UserID
} }
return c.GetData(gid) == typ d := c.GetData(gid)
return d&3 == typ && rand.Int63n(10) <= d>>59
} }
} }