From a166d06096e7e2c90fde89871a1c60a9e4cc82d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 5 May 2022 14:19:12 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=8A=BDn=E5=BC=A0=E5=A1=94?= =?UTF-8?q?=E7=BD=97=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/tarot/tarot.go | 74 ++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/plugin/tarot/tarot.go b/plugin/tarot/tarot.go index cd138323..6a5c9d2d 100644 --- a/plugin/tarot/tarot.go +++ b/plugin/tarot/tarot.go @@ -33,12 +33,13 @@ func init() { engine := control.Register("tarot", &control.Options{ DisableOnDefault: false, Help: "塔罗牌\n" + - "- 抽塔罗牌\n", + "- 抽塔罗牌\n" + + "- 抽n张塔罗牌", // TODO 抽X张塔罗牌 解塔罗牌[牌名] PublicDataFolder: "Tarot", }).ApplySingle(ctxext.DefaultSingle) - engine.OnFullMatch("抽塔罗牌", ctxext.DoOnceOnSuccess( + engine.OnRegex(`^抽(\d{1,3}张)?塔罗牌$`, ctxext.DoOnceOnSuccess( func(ctx *zero.Ctx) bool { data, err := engine.GetLazyData("tarots.json", true) if err != nil { @@ -47,29 +48,64 @@ func init() { } err = json.Unmarshal(data, &cardMap) if err != nil { - panic(err) + ctx.SendChain(message.Text("ERROR:", err)) + return false } logrus.Infof("[tarot]读取%d张塔罗牌", len(cardMap)) return true }, )).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) { - i := ctxext.RandSenderPerDayN(ctx, 22) - p := ctxext.RandSenderPerDayN(ctx, 2) - card := cardMap[(strconv.Itoa(i))] - name := card.Name - var info string - if p == 0 { - info = card.Info.Description - } else { - info = card.Info.ReverseDescription + match := ctx.State["regex_matched"].([]string)[1] + n := 1 + if match != "" { + var err error + n, err = strconv.Atoi(match[:len(match)-3]) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return + } + if n <= 0 { + ctx.SendChain(message.Text("ERROR:张数必须为正")) + return + } + if n > 1 && !zero.OnlyGroup(ctx) { + ctx.SendChain(message.Text("ERROR:抽取多张仅支持群聊")) + return + } + if n > 20 { + ctx.SendChain(message.Text("ERROR:抽取张数过多")) + return + } } - if id := ctx.SendChain( - message.At(ctx.Event.UserID), - message.Text(reasons[rand.Intn(len(reasons))], position[p], " 的 ", name, "\n"), - message.Image(fmt.Sprintf(bed+"MajorArcana/%d.png", i)), - message.Text("\n其意义为: ", info), - ); id.ID() == 0 { - ctx.SendChain(message.Text("ERROR:可能被风控了")) + if n == 1 { + if id := ctx.Send(randTarot()); id.ID() == 0 { + ctx.SendChain(message.Text("ERROR:可能被风控了")) + } + return } + msg := make([]message.MessageSegment, n) + for i := range msg { + msg[i] = ctxext.FakeSenderForwardNode(ctx, randTarot()...) + } + ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg) + return }) } + +func randTarot() []message.MessageSegment { + i := rand.Intn(22) + p := rand.Intn(2) + card := cardMap[(strconv.Itoa(i))] + name := card.Name + var info string + if p == 0 { + info = card.Info.Description + } else { + info = card.Info.ReverseDescription + } + return []message.MessageSegment{ + message.Text(reasons[rand.Intn(len(reasons))], position[p], " 的 ", name, "\n"), + message.Image(fmt.Sprintf(bed+"MajorArcana/%d.png", i)), + message.Text("\n其意义为: ", info), + } +}