|
| 1 | +// Package hitokoto 一言 |
| 2 | +package hitokoto |
| 3 | + |
| 4 | +import ( |
| 5 | + "math/rand" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/FloatTech/floatbox/binary" |
| 11 | + fcext "github.com/FloatTech/floatbox/ctxext" |
| 12 | + ctrl "github.com/FloatTech/zbpctrl" |
| 13 | + "github.com/FloatTech/zbputils/control" |
| 14 | + "github.com/FloatTech/zbputils/ctxext" |
| 15 | + "github.com/FloatTech/zbputils/img/text" |
| 16 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 17 | + "github.com/wdvxdr1123/ZeroBot/message" |
| 18 | +) |
| 19 | + |
| 20 | +func init() { // 插件主体 |
| 21 | + engine := control.Register("hitokoto", &ctrl.Options[*zero.Ctx]{ |
| 22 | + DisableOnDefault: false, |
| 23 | + Brief: "一言", |
| 24 | + Help: "- 一言[xxx]\n" + |
| 25 | + "- 系列一言", |
| 26 | + PublicDataFolder: "Hitokoto", |
| 27 | + }) |
| 28 | + getdb := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool { |
| 29 | + dbfile := engine.DataFolder() + "hitokoto.db" |
| 30 | + _, err := engine.GetLazyData("hitokoto.db", false) |
| 31 | + if err != nil { |
| 32 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 33 | + return false |
| 34 | + } |
| 35 | + hdb, err = initialize(dbfile) |
| 36 | + if err != nil { |
| 37 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 38 | + return false |
| 39 | + } |
| 40 | + return true |
| 41 | + }) |
| 42 | + engine.OnPrefix(`一言`, getdb).SetBlock(true). |
| 43 | + Handle(func(ctx *zero.Ctx) { |
| 44 | + ctx.SendChain(message.Text("少女祈祷中...")) |
| 45 | + args := ctx.State["args"].(string) |
| 46 | + blist, err := hdb.getByKey(strings.TrimSpace(args)) |
| 47 | + if err != nil { |
| 48 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 49 | + return |
| 50 | + } |
| 51 | + if len(blist) == 0 { |
| 52 | + ctx.SendChain(message.Text("ERROR: hitokoto empty")) |
| 53 | + return |
| 54 | + } |
| 55 | + m := make(message.Message, 0, 10) |
| 56 | + text := strings.Builder{} |
| 57 | + if len(blist) <= 10 { |
| 58 | + for _, b := range blist { |
| 59 | + text.WriteString(b.Hitokoto) |
| 60 | + text.WriteString("\n——") |
| 61 | + text.WriteString(b.From) |
| 62 | + m = append(m, ctxext.FakeSenderForwardNode(ctx, message.Text(text.String()))) |
| 63 | + text.Reset() |
| 64 | + } |
| 65 | + } else { |
| 66 | + indexes := map[int]struct{}{} |
| 67 | + for i := 0; i < 10; i++ { |
| 68 | + ind := rand.Intn(len(blist)) |
| 69 | + if _, ok := indexes[ind]; ok { |
| 70 | + i-- |
| 71 | + continue |
| 72 | + } |
| 73 | + indexes[ind] = struct{}{} |
| 74 | + } |
| 75 | + for k := range indexes { |
| 76 | + b := blist[k] |
| 77 | + text.WriteString(b.Hitokoto) |
| 78 | + text.WriteString("\n——") |
| 79 | + text.WriteString(b.From) |
| 80 | + m = append(m, ctxext.FakeSenderForwardNode(ctx, message.Text(text.String()))) |
| 81 | + text.Reset() |
| 82 | + } |
| 83 | + } |
| 84 | + if id := ctx.Send(m).ID(); id == 0 { |
| 85 | + ctx.SendChain(message.Text("ERROR: 可能被风控或下载图片用时过长,请耐心等待")) |
| 86 | + } |
| 87 | + }) |
| 88 | + engine.OnFullMatch(`系列一言`, getdb).SetBlock(true). |
| 89 | + Handle(func(ctx *zero.Ctx) { |
| 90 | + next := zero.NewFutureEvent("message", 999, false, ctx.CheckSession()) |
| 91 | + recv, cancel := next.Repeat() |
| 92 | + defer cancel() |
| 93 | + results, err := hdb.getAllCategory() |
| 94 | + if err != nil { |
| 95 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 96 | + return |
| 97 | + } |
| 98 | + tex := strings.Builder{} |
| 99 | + tex.WriteString("请输入系列一言序号\n") |
| 100 | + for i, v := range results { |
| 101 | + tex.WriteString(strconv.Itoa(i)) |
| 102 | + tex.WriteString(". ") |
| 103 | + tex.WriteString(v.Category) |
| 104 | + tex.WriteString("\n") |
| 105 | + } |
| 106 | + base64Str, err := text.RenderToBase64(tex.String(), text.FontFile, 400, 20) |
| 107 | + if err != nil { |
| 108 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 109 | + return |
| 110 | + } |
| 111 | + ctx.SendChain(message.Image("base64://" + binary.BytesToString(base64Str))) |
| 112 | + for { |
| 113 | + select { |
| 114 | + case <-time.After(time.Second * 120): |
| 115 | + ctx.SendChain(message.Text("系列一言指令过期")) |
| 116 | + return |
| 117 | + case c := <-recv: |
| 118 | + msg := c.Event.Message.ExtractPlainText() |
| 119 | + num, err := strconv.Atoi(msg) |
| 120 | + if err != nil { |
| 121 | + ctx.SendChain(message.Text("请输入数字!")) |
| 122 | + continue |
| 123 | + } |
| 124 | + if num < 0 || num >= len(results) { |
| 125 | + ctx.SendChain(message.Text("序号非法!")) |
| 126 | + continue |
| 127 | + } |
| 128 | + ctx.SendChain(message.Text("请欣赏系列一言: ", results[num].Category)) |
| 129 | + hlist, err := hdb.getByCategory(results[num].Category) |
| 130 | + if err != nil { |
| 131 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 132 | + return |
| 133 | + } |
| 134 | + if len(hlist) == 0 { |
| 135 | + ctx.SendChain(message.Text("ERROR: hitokoto empty")) |
| 136 | + return |
| 137 | + } |
| 138 | + m := make(message.Message, 0, 10) |
| 139 | + text := strings.Builder{} |
| 140 | + if len(hlist) <= 10 { |
| 141 | + for _, b := range hlist { |
| 142 | + text.WriteString(b.Hitokoto) |
| 143 | + text.WriteString("\n——") |
| 144 | + text.WriteString(b.From) |
| 145 | + m = append(m, ctxext.FakeSenderForwardNode(ctx, message.Text(text.String()))) |
| 146 | + text.Reset() |
| 147 | + } |
| 148 | + } else { |
| 149 | + indexes := map[int]struct{}{} |
| 150 | + for i := 0; i < 10; i++ { |
| 151 | + ind := rand.Intn(len(hlist)) |
| 152 | + if _, ok := indexes[ind]; ok { |
| 153 | + i-- |
| 154 | + continue |
| 155 | + } |
| 156 | + indexes[ind] = struct{}{} |
| 157 | + } |
| 158 | + for k := range indexes { |
| 159 | + b := hlist[k] |
| 160 | + text.WriteString(b.Hitokoto) |
| 161 | + text.WriteString("\n——") |
| 162 | + text.WriteString(b.From) |
| 163 | + m = append(m, ctxext.FakeSenderForwardNode(ctx, message.Text(text.String()))) |
| 164 | + text.Reset() |
| 165 | + } |
| 166 | + } |
| 167 | + if id := ctx.Send(m).ID(); id == 0 { |
| 168 | + ctx.SendChain(message.Text("ERROR: 可能被风控或下载图片用时过长,请耐心等待")) |
| 169 | + } |
| 170 | + return |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | +} |
0 commit comments