Skip to content

Commit 42b1861

Browse files
authored
Feature jptingroom (#448)
1 parent b0c57df commit 42b1861

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,20 @@ print("run[CQ:image,file="+j["img"]+"]")
797797

798798
- [x] [查梗|小鸡词典][]
799799

800+
</details>
801+
<details>
802+
<summary>日语听力学习材料</summary>
803+
804+
`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/jptingroom"`
805+
806+
- [x] 随机日语听力
807+
808+
- [x] 随机日语歌曲
809+
810+
- [x] 日语听力 xxx
811+
812+
- [x] 日语歌曲 xxx
813+
800814
</details>
801815
<details>
802816
<summary>绝绝子</summary>

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import (
9595
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/inject" // 注入指令
9696
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jandan" // 煎蛋网无聊图
9797
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jikipedia" // 小鸡词典
98+
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jptingroom" // 日语听力学习材料
9899
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/juejuezi" // 绝绝子生成器
99100
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/lolicon" // lolicon 随机图片
100101
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/midicreate" // 简易midi音乐制作

plugin/jptingroom/jptingroom.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

plugin/jptingroom/model.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package jptingroom
2+
3+
import (
4+
"time"
5+
6+
sql "github.com/FloatTech/sqlite"
7+
)
8+
9+
type item struct {
10+
ID int64 `db:"id"`
11+
Title string `db:"title"`
12+
PageURL string `db:"page_url"`
13+
Category string `db:"category"`
14+
Intro string `db:"intro"`
15+
AudioURL string `db:"audio_url"`
16+
Content string `db:"content"`
17+
Datetime time.Time `db:"datetime"`
18+
}
19+
20+
var db = &sql.Sqlite{}
21+
22+
func getRandomAudioByCategory(category string) (t item) {
23+
_ = db.Find("item", &t, "where category = '"+category+"' ORDER BY RANDOM() limit 1")
24+
return
25+
}
26+
27+
func getRandomAudioByCategoryAndKeyword(category string, keyword string) (t item) {
28+
_ = db.Find("item", &t, "where category = '"+category+"' and (title like '%"+keyword+"%' or content like '%"+keyword+"%') ORDER BY RANDOM() limit 1")
29+
return
30+
}

0 commit comments

Comments
 (0)