Skip to content

Commit cb0ffa0

Browse files
authored
feat(music): 龙珠聚合搜索 (FloatTech#1179)
* 🐛 修改听歌问题 * ✨ 添加龙珠聚合搜索 * 🎨 优化聚合搜索 * 🐛 一定能点出歌 * 🎨 删除调试
1 parent 0615993 commit cb0ffa0

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,10 @@ print("run[CQ:image,file="+j["img"]+"]")
10261026
- [x] 酷我点歌[xxx]
10271027

10281028
- [x] 酷狗点歌[xxx]
1029+
1030+
- [x] qq点歌[xxx]
1031+
1032+
- [x] 咪咕点歌[xxx]
10291033

10301034
</details>
10311035
<details>

console/console_windows.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ func setConsoleTitle(title string) (err error) {
3838
}
3939

4040
func init() {
41+
debugMode := os.Getenv("DEBUG_MODE") == "1"
4142
stdin := windows.Handle(os.Stdin.Fd())
4243

4344
var mode uint32
4445
err := windows.GetConsoleMode(stdin, &mode)
4546
if err != nil {
46-
panic(err)
47+
if debugMode {
48+
logrus.Warnf("调试模式下忽略控制台模式获取失败: %v", err)
49+
return // 调试模式下直接返回,跳过后续配置
50+
} else {
51+
panic(err) // 非调试模式下 panic
52+
}
4753
}
4854

4955
mode &^= windows.ENABLE_QUICK_EDIT_MODE // 禁用快速编辑模式

plugin/music/selecter.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"github.com/wdvxdr1123/ZeroBot/message"
2222
)
2323

24+
var (
25+
longZhuURL = "https://www.hhlqilongzhu.cn/api/joox/juhe_music.php?msg=%v"
26+
)
27+
2428
func init() {
2529
control.AutoRegister(&ctrl.Options[*zero.Ctx]{
2630
DisableOnDefault: false,
@@ -29,7 +33,8 @@ func init() {
2933
"- 网易点歌[xxx]\n" +
3034
"- 酷我点歌[xxx]\n" +
3135
"- 酷狗点歌[xxx]\n" +
32-
"- 咪咕点歌[xxx]",
36+
"- 咪咕点歌[xxx]\n" +
37+
"- qq点歌[xxx]\n",
3338
}).OnRegex(`^(.{0,2})点歌\s?(.{1,25})$`).SetBlock(true).Limit(ctxext.LimitByUser).
3439
Handle(func(ctx *zero.Ctx) {
3540
// switch 平台
@@ -42,12 +47,37 @@ func init() {
4247
ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[2]))
4348
case "网易":
4449
ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[2]))
45-
default: // 默认 QQ音乐
50+
case "qq":
4651
ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[2]))
52+
default: // 默认聚合点歌
53+
ctx.SendChain(longzhu(ctx.State["regex_matched"].([]string)[2]))
4754
}
4855
})
4956
}
5057

58+
// longzhu 聚合平台
59+
func longzhu(keyword string) message.Segment {
60+
data, _ := web.GetData(fmt.Sprintf(longZhuURL, url.QueryEscape(keyword)))
61+
// 假设 data 是包含整个 JSON 数组的字节切片
62+
results := gjson.ParseBytes(data).Array()
63+
for _, result := range results {
64+
if strings.Contains(strings.ToLower(result.Get("title").String()), strings.ToLower(keyword)) {
65+
if musicURL := result.Get("full_track").String(); musicURL != "" {
66+
return message.Record(musicURL)
67+
}
68+
}
69+
}
70+
71+
results = gjson.GetBytes(data, "#.full_track").Array()
72+
if len(results) > 0 {
73+
if musicURL := results[0].String(); musicURL != "" {
74+
return message.Record(musicURL)
75+
}
76+
}
77+
78+
return message.Text("点歌失败, 找不到 ", keyword, " 的相关结果")
79+
}
80+
5181
// migu 返回咪咕音乐卡片
5282
func migu(keyword string) message.Segment {
5383
headers := http.Header{

plugin/score/model.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package score
22

33
import (
44
"os"
5+
"sync"
56
"time"
67

78
"github.com/jinzhu/gorm"
@@ -11,7 +12,10 @@ import (
1112
var sdb *scoredb
1213

1314
// scoredb 分数数据库
14-
type scoredb gorm.DB
15+
type scoredb struct {
16+
db *gorm.DB
17+
scoremu sync.Mutex
18+
}
1519

1620
// scoretable 分数结构体
1721
type scoretable struct {
@@ -52,25 +56,31 @@ func initialize(dbpath string) *scoredb {
5256
panic(err)
5357
}
5458
gdb.AutoMigrate(&scoretable{}).AutoMigrate(&signintable{})
55-
return (*scoredb)(gdb)
59+
return &scoredb{
60+
db: gdb,
61+
}
5662
}
5763

5864
// Close ...
5965
func (sdb *scoredb) Close() error {
60-
db := (*gorm.DB)(sdb)
66+
db := sdb.db
6167
return db.Close()
6268
}
6369

6470
// GetScoreByUID 取得分数
6571
func (sdb *scoredb) GetScoreByUID(uid int64) (s scoretable) {
66-
db := (*gorm.DB)(sdb)
72+
sdb.scoremu.Lock()
73+
defer sdb.scoremu.Unlock()
74+
db := sdb.db
6775
db.Model(&scoretable{}).FirstOrCreate(&s, "uid = ? ", uid)
6876
return s
6977
}
7078

7179
// InsertOrUpdateScoreByUID 插入或更新分数
7280
func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
73-
db := (*gorm.DB)(sdb)
81+
sdb.scoremu.Lock()
82+
defer sdb.scoremu.Unlock()
83+
db := sdb.db
7484
s := scoretable{
7585
UID: uid,
7686
Score: score,
@@ -91,14 +101,18 @@ func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
91101

92102
// GetSignInByUID 取得签到次数
93103
func (sdb *scoredb) GetSignInByUID(uid int64) (si signintable) {
94-
db := (*gorm.DB)(sdb)
104+
sdb.scoremu.Lock()
105+
defer sdb.scoremu.Unlock()
106+
db := sdb.db
95107
db.Model(&signintable{}).FirstOrCreate(&si, "uid = ? ", uid)
96108
return si
97109
}
98110

99111
// InsertOrUpdateSignInCountByUID 插入或更新签到次数
100112
func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err error) {
101-
db := (*gorm.DB)(sdb)
113+
sdb.scoremu.Lock()
114+
defer sdb.scoremu.Unlock()
115+
db := sdb.db
102116
si := signintable{
103117
UID: uid,
104118
Count: count,
@@ -118,7 +132,9 @@ func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err er
118132
}
119133

120134
func (sdb *scoredb) GetScoreRankByTopN(n int) (st []scoretable, err error) {
121-
db := (*gorm.DB)(sdb)
135+
sdb.scoremu.Lock()
136+
defer sdb.scoremu.Unlock()
137+
db := sdb.db
122138
err = db.Model(&scoretable{}).Order("score desc").Limit(n).Find(&st).Error
123139
return
124140
}

0 commit comments

Comments
 (0)