Skip to content

Commit

Permalink
词库增加设置概率功能
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Dec 2, 2022
1 parent 83778af commit 3b1ad22
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

- [x] 切换[kimo|傲娇|可爱]词库
- [x] 设置词库触发概率0.x (0<x<9)

</details>
<details>
Expand Down
41 changes: 35 additions & 6 deletions plugin/thesaurus/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {
engine := control.Register("thesaurus", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "词典匹配回复",
Help: "- 切换[kimo|傲娇|可爱]词库",
Help: "- 切换[kimo|傲娇|可爱]词库\n- 设置词库触发概率0.x (0<x<9)",
PublicDataFolder: "Chat",
})
engine.OnRegex(`^切换(kimo|傲娇|可爱)词库$`, zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
Expand All @@ -34,15 +34,40 @@ func init() {
if gid == 0 {
gid = -ctx.Event.UserID
}
var err error
d := c.GetData(gid)
t := int64(0)
switch ctx.State["regex_matched"].([]string)[1] {
case "kimo":
err = c.SetData(gid, tKIMO)
t = tKIMO
case "傲娇":
err = c.SetData(gid, tDERE)
t = tDERE
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 {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down Expand Up @@ -142,6 +167,9 @@ const (

func canmatch(typ int64) zero.Rule {
return func(ctx *zero.Ctx) bool {
if zero.HasPicture(ctx) {
return false
}
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if !ok {
return false
Expand All @@ -150,7 +178,8 @@ func canmatch(typ int64) zero.Rule {
if gid == 0 {
gid = -ctx.Event.UserID
}
return c.GetData(gid) == typ
d := c.GetData(gid)
return d&3 == typ && rand.Int63n(10) <= d>>59
}
}

Expand Down

0 comments on commit 3b1ad22

Please sign in to comment.