Skip to content

Commit 5e2c4e6

Browse files
committed
feat: chuni music search
1 parent 3d5e568 commit 5e2c4e6

File tree

10 files changed

+4226
-2197
lines changed

10 files changed

+4226
-2197
lines changed

apps/tools/src/updateSongListChu.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const skipWhenExist = process.argv[3] === 'skip';
1111
const optionDirs = await fsP.readdir(path.join(baseDir, 'option'));
1212

1313
for (const opt of [path.join(baseDir, 'data', 'A000'), ...optionDirs.map(d => path.join(baseDir, 'option', d))]) {
14-
if(!fs.existsSync(path.join(opt, 'music'))) continue;
15-
14+
if (!fs.existsSync(path.join(opt, 'music'))) continue;
15+
1616
for (const f of await fsP.readdir(path.join(opt, 'music'))) {
1717
if (!f.startsWith('music')) continue;
1818
if (!fs.existsSync(path.join(opt, 'music', f, 'Music.xml'))) continue;
@@ -28,10 +28,11 @@ for (const opt of [path.join(baseDir, 'data', 'A000'), ...optionDirs.map(d => pa
2828
}
2929

3030
music.name = meta.MusicData.name.str.toString();
31-
console.log(music.name);
31+
// console.log(music.name);
3232
music.ver = meta.MusicData.releaseTagName.str.toString();
3333
music.composer = meta.MusicData.artistName.str.toString();
34-
// music.genre = meta.MusicData.genreName.str.toString();
34+
music.genre = meta.MusicData.genreNames.list.StringID.str.toString();
35+
if (!music.genre) console.log(meta.MusicData.genreNames);
3536
music.worldsEndStars = meta.MusicData.starDifType.toString();
3637
music.worldsEndTag = meta.MusicData.worldsEndTagName.str.toString();
3738

packages/botcore/src/botBuilder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import b50 from './modules/b50';
1111
import musicSearch from './modules/musicSearch';
1212
import admin from './modules/admin';
1313
import exportUserMusic from './modules/exportUserMusic';
14+
import musicSearchChu from './modules/musicSearchChu';
1415

1516
interface BuilderEnvBase<T extends BotTypes> {
1617
bot: Bot<T>,
@@ -35,7 +36,7 @@ export const buildBot = <T extends BotTypes>(env: BuilderEnvBase<T>) => {
3536
)
3637
} satisfies BuilderEnv<T>;
3738

38-
for (const attachHandlers of [callbackQuery, help, bind, scoreQuery, plateProgress, levelProgress, /* levelConstTable, */ b50, exportUserMusic, musicSearch, admin]) {
39+
for (const attachHandlers of [callbackQuery, help, bind, scoreQuery, plateProgress, levelProgress, /* levelConstTable, */ b50, exportUserMusic, musicSearchChu, musicSearch, admin]) {
3940
attachHandlers(passEnv);
4041
}
4142
};
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { ChuniSong as Song } from '@clansty/maibot-types';
2+
import { BotTypes, BundledMessageBase, MessageButtonSwitchInline, MessageButtonUrl, MessageEventBase, SendMessageAction } from '@clansty/maibot-firm';
3+
import { BuilderEnv } from '../botBuilder';
4+
import { MESSAGE_TEMPLATE } from '../MessageTemplate';
5+
6+
export default <T extends BotTypes>({ bot, env, getContext, musicToFile }: BuilderEnv<T>) => {
7+
bot.registerInlineQuery(/chu (.+)/, async (event) => {
8+
if (!event.match[1].trim()) {
9+
await event.answer()
10+
.withCacheTime(86400)
11+
.dispatch();
12+
return true;
13+
}
14+
const results = Song.search(event.match[1].trim().toLowerCase());
15+
console.log(results);
16+
const answer = event.answer()
17+
.withCacheTime(60);
18+
await Promise.all(results.map(async song => {
19+
if (musicToFile[song.id]) {
20+
answer.addAudioResult(`chu:${song.id}` || song.title, musicToFile[song.id])
21+
.setText(song.display);
22+
} else if (song.coverUrl) {
23+
answer.addPhotoResult(`chu:${song.id}` || song.title, song.coverUrl)
24+
.setTitle(song.title)
25+
.setText(song.display);
26+
} else {
27+
answer.addTextResult(`chu:${song.id}` || song.title, song.title)
28+
.setText(song.display);
29+
}
30+
}
31+
));
32+
await answer.dispatch();
33+
});
34+
35+
const sendSong = async (req: SendMessageAction<T>, song: Song) => {
36+
if (!song) return;
37+
38+
const msgTitle = song.display.substring(0, song.display.indexOf('\n'));
39+
const msgText = song.display.substring(song.display.indexOf('\n') + 1).trim();
40+
if (musicToFile[song.id]) {
41+
req.addAudio(musicToFile[song.id]);
42+
} else if (song.coverUrl) {
43+
req
44+
.addPhoto(song.coverUrl)
45+
.setTemplatedMessage(MESSAGE_TEMPLATE.MusicInfo, {
46+
title: msgTitle,
47+
content: msgText,
48+
image: song.coverUrl
49+
});
50+
}
51+
52+
await req.setText(song.display).dispatch();
53+
};
54+
55+
bot.registerCommand('start', async (event) => {
56+
if (!event.params[0].startsWith('chu-')) return false;
57+
const song = Song.fromId(parseInt(event.params[0].substring(5)));
58+
await sendSong(event.reply(), song);
59+
return true;
60+
});
61+
62+
const handleSearch = async (event: MessageEventBase<T>, kw: string) => {
63+
if (!kw) {
64+
await event.reply()
65+
.setText('请输入要搜索的歌曲名')
66+
.dispatch();
67+
return true;
68+
}
69+
const results = Song.search(kw.toLowerCase());
70+
if (!results.length) {
71+
await event.reply()
72+
.setText('找不到匹配的歌')
73+
.dispatch();
74+
return true;
75+
}
76+
if (results.length > 1) {
77+
const req = event.reply()
78+
.setText(`共找到 ${results.length} 个结果:\n\n` + results.map(song => (song.id ? song.id + '. ' : '') + song.title).join('\n'))
79+
.addButtons(new MessageButtonSwitchInline('选择结果', `chu ${kw}`));
80+
81+
const bundle = req.addBundledMessage();
82+
bundle.setTitle(`共找到 ${results.length} 个结果`).setDescription(results.map(song => (song.id ? song.id + '. ' : '') + song.title).join('\n')).setSummary('点击展开').setPrompt(`歌曲搜索:${results.length} 个结果`);
83+
for (const result of results) {
84+
bundle.addNode().addPhoto(result.coverUrl);
85+
bundle.addNode().setText(result.display);
86+
}
87+
88+
await req.dispatch();
89+
return true;
90+
}
91+
92+
const song = results[0];
93+
await sendSong(event.reply(), song);
94+
return true;
95+
};
96+
97+
bot.registerCommand(['chu', 'chuni'], async (event) => {
98+
return handleSearch(event, event.params.join(' ').trim());
99+
});
100+
}

0 commit comments

Comments
 (0)