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