mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 22:00:11 +08:00
优化 breakrepeat
This commit is contained in:
parent
295c50a0ae
commit
f9319c2a27
@ -2,7 +2,7 @@
|
||||
package breakrepeat
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strconv"
|
||||
|
||||
ctrl "github.com/FloatTech/zbpctrl"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
@ -10,53 +10,30 @@ import (
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
)
|
||||
|
||||
const (
|
||||
maxLimit = 3
|
||||
)
|
||||
const throttle = 3 // 不可超过 9
|
||||
|
||||
type result struct {
|
||||
Limit int64
|
||||
RawMsg string
|
||||
}
|
||||
|
||||
var (
|
||||
sm syncx.Map[int64, *result]
|
||||
)
|
||||
var sm syncx.Map[int64, string]
|
||||
|
||||
func init() {
|
||||
engine := control.Register("breakrepeat", &ctrl.Options[*zero.Ctx]{
|
||||
DisableOnDefault: false,
|
||||
Help: "打断复读,打断3次以上复读\n",
|
||||
Help: "打断复读\n- 打断" + strconv.Itoa(throttle) + "次以上复读\n",
|
||||
})
|
||||
engine.On(`message/group`, zero.OnlyGroup).SetBlock(false).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
gid := ctx.Event.GroupID
|
||||
raw := ctx.Event.RawMessage
|
||||
if r, ok := sm.Load(gid); !ok || r.RawMsg != raw {
|
||||
sm.Store(gid, &result{
|
||||
Limit: 0,
|
||||
RawMsg: raw,
|
||||
})
|
||||
r, ok := sm.Load(gid)
|
||||
if !ok || len(r) <= 3 || r[3:] != raw {
|
||||
sm.Store(gid, "0: "+raw)
|
||||
return
|
||||
}
|
||||
if r, ok := sm.Load(gid); ok {
|
||||
sm.Store(gid, &result{
|
||||
Limit: r.Limit + 1,
|
||||
RawMsg: raw,
|
||||
})
|
||||
}
|
||||
if res, ok := sm.Load(gid); ok && res.Limit >= maxLimit {
|
||||
r := []rune(res.RawMsg)
|
||||
if len(r) > 2 {
|
||||
rand.Shuffle(len(r), func(i, j int) {
|
||||
r[i], r[j] = r[j], r[i]
|
||||
})
|
||||
ctx.Send(string(r))
|
||||
}
|
||||
sm.Store(gid, &result{
|
||||
Limit: 0,
|
||||
RawMsg: res.RawMsg,
|
||||
})
|
||||
c := int(r[0] - '0')
|
||||
if c < throttle {
|
||||
sm.Store(gid, strconv.Itoa(c+1)+": "+raw)
|
||||
return
|
||||
}
|
||||
sm.Delete(gid)
|
||||
ctx.Send(r)
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user