|
| 1 | +// Package jptingroom 日语听力学习材料 |
| 2 | +package jptingroom |
| 3 | + |
| 4 | +import ( |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/FloatTech/floatbox/binary" |
| 8 | + fcext "github.com/FloatTech/floatbox/ctxext" |
| 9 | + ctrl "github.com/FloatTech/zbpctrl" |
| 10 | + "github.com/FloatTech/zbputils/control" |
| 11 | + "github.com/FloatTech/zbputils/img/text" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 14 | + "github.com/wdvxdr1123/ZeroBot/message" |
| 15 | +) |
| 16 | + |
| 17 | +func init() { // 插件主体 |
| 18 | + engine := control.Register("jptingroom", &ctrl.Options[*zero.Ctx]{ |
| 19 | + DisableOnDefault: false, |
| 20 | + Help: "日语听力学习材料\n" + |
| 21 | + "- 随机日语听力\n" + |
| 22 | + "- 随机日语歌曲\n" + |
| 23 | + "- 日语听力 xxx\n" + |
| 24 | + "- 日语歌曲 xxx\n", |
| 25 | + PublicDataFolder: "Jptingroom", |
| 26 | + }) |
| 27 | + |
| 28 | + getdb := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool { |
| 29 | + db.DBPath = engine.DataFolder() + "item.db" |
| 30 | + _, err := engine.GetLazyData("item.db", true) |
| 31 | + if err != nil { |
| 32 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 33 | + return false |
| 34 | + } |
| 35 | + err = db.Open(time.Hour * 24) |
| 36 | + if err != nil { |
| 37 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 38 | + return false |
| 39 | + } |
| 40 | + err = db.Create("item", &item{}) |
| 41 | + if err != nil { |
| 42 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 43 | + return false |
| 44 | + } |
| 45 | + n, err := db.Count("item") |
| 46 | + if err != nil { |
| 47 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 48 | + return false |
| 49 | + } |
| 50 | + log.Infof("[jptingroom]读取%d条日语听力材料", n) |
| 51 | + return true |
| 52 | + }) |
| 53 | + // 开启 |
| 54 | + engine.OnFullMatchGroup([]string{"随机日语听力", "随机日语歌曲"}, getdb).SetBlock(true). |
| 55 | + Handle(func(ctx *zero.Ctx) { |
| 56 | + matched := ctx.State["matched"].(string) |
| 57 | + var t item |
| 58 | + switch matched { |
| 59 | + case "随机日语听力": |
| 60 | + t = getRandomAudioByCategory("tingli") |
| 61 | + case "随机日语歌曲": |
| 62 | + t = getRandomAudioByCategory("gequ") |
| 63 | + default: |
| 64 | + } |
| 65 | + if t.AudioURL == "" { |
| 66 | + ctx.SendChain(message.Text("未能找到相关材料")) |
| 67 | + return |
| 68 | + } |
| 69 | + ctx.SendChain(message.Record(t.AudioURL)) |
| 70 | + content := t.Title + "\n\n" + t.Content |
| 71 | + data, err := text.RenderToBase64(content, text.FontFile, 400, 20) |
| 72 | + if err != nil { |
| 73 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 74 | + return |
| 75 | + } |
| 76 | + if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(data))); id.ID() == 0 { |
| 77 | + ctx.SendChain(message.Text("ERROR: 可能被风控了")) |
| 78 | + } |
| 79 | + }) |
| 80 | + engine.OnRegex(`日语(听力|歌曲)\s?([一-龥A-Za-z0-9ぁ-んァ-ヶ]{1,50})$`, getdb).SetBlock(true). |
| 81 | + Handle(func(ctx *zero.Ctx) { |
| 82 | + regexMatched := ctx.State["regex_matched"].([]string) |
| 83 | + var t item |
| 84 | + switch regexMatched[1] { |
| 85 | + case "听力": |
| 86 | + t = getRandomAudioByCategoryAndKeyword("tingli", regexMatched[2]) |
| 87 | + case "歌曲": |
| 88 | + t = getRandomAudioByCategoryAndKeyword("gequ", regexMatched[2]) |
| 89 | + default: |
| 90 | + } |
| 91 | + if t.AudioURL == "" { |
| 92 | + ctx.SendChain(message.Text("未能找到相关材料")) |
| 93 | + return |
| 94 | + } |
| 95 | + content := t.Title + "\n\n" + t.Content |
| 96 | + data, err := text.RenderToBase64(content, text.FontFile, 400, 20) |
| 97 | + if err != nil { |
| 98 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 99 | + return |
| 100 | + } |
| 101 | + if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(data))); id.ID() == 0 { |
| 102 | + ctx.SendChain(message.Text("ERROR: 可能被风控了")) |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
0 commit comments