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