|
| 1 | +import _ from 'lodash'; |
| 2 | +import { CHU_LEVEL_EMOJI, FC, LEVEL_EMOJI, Song } from '@clansty/maibot-types/src'; |
| 3 | +import { chusanRating, computeRa } from '@clansty/maibot-utils'; |
| 4 | +import { BotTypes, MessageButtonSwitchInline } from '@clansty/maibot-firm'; |
| 5 | +import { BuilderEnv } from '../botBuilder'; |
| 6 | +import { ChuniSong } from '@clansty/maibot-types'; |
| 7 | + |
| 8 | +export default <T extends BotTypes>({ bot, env, getContext, musicToFile }: BuilderEnv<T>) => { |
| 9 | + bot.registerInlineQuery(/^ ?query-?chu (.+)/i, async (event) => { |
| 10 | + const ctx = getContext(event); |
| 11 | + const profile = await ctx.getCurrentProfile(false); |
| 12 | + if (!profile) { |
| 13 | + await event.answer() |
| 14 | + .withStartButton('请绑定用户', 'bind') |
| 15 | + .isPersonal() |
| 16 | + .dispatch(); |
| 17 | + return true; |
| 18 | + } |
| 19 | + |
| 20 | + const query = event.match[1].trim().toLowerCase(); |
| 21 | + if (query === '') { |
| 22 | + await event.answer() |
| 23 | + .withCacheTime(86400) |
| 24 | + .dispatch(); |
| 25 | + } |
| 26 | + const results = ChuniSong.search(query); |
| 27 | + const userMusic = await profile.getChuniUserMusic(results); |
| 28 | + const answer = event.answer() |
| 29 | + .isPersonal(); |
| 30 | + for (const song of results) { |
| 31 | + const userScores = userMusic.filter(it => it.musicId === song.id); |
| 32 | + if (!userScores.length) continue; |
| 33 | + _.sortBy(userScores, it => it.level); |
| 34 | + |
| 35 | + const message = [song.id + '. ' + song.title, '']; |
| 36 | + for (const userScore of userScores) { |
| 37 | + const chart = song.notes[userScore.level]; |
| 38 | + message.push(`${CHU_LEVEL_EMOJI[userScore.level]} ${chart.lv.toFixed(1)} ` + |
| 39 | + `${userScore.scoreMax.toLocaleString('zh-CN')} = ${chusanRating(chart.lv, userScore.scoreMax)} ${userScore.isAllJustice ? 'AJ' : ''} ${userScore.isFullCombo ? 'FC' : ''}`); |
| 40 | + } |
| 41 | + |
| 42 | + // if (musicToFile[song.id]) { |
| 43 | + // // 有音频 |
| 44 | + // answer.addAudioResult(song.dxId?.toString() || song.title, musicToFile[song.id]) |
| 45 | + // .setText(message.join('\n')) |
| 46 | + // .addButtons(new MessageButtonSwitchInline('歌曲详情', song.id.toString())); |
| 47 | + // } else { |
| 48 | + answer.addPhotoResult(song.id?.toString() || song.title, song.coverUrl) |
| 49 | + .setTitle(song.title) |
| 50 | + .setText(message.join('\n')) |
| 51 | + .addButtons(new MessageButtonSwitchInline('歌曲详情', 'chu ' + song.id.toString())); |
| 52 | + // } |
| 53 | + } |
| 54 | + |
| 55 | + await answer.dispatch(); |
| 56 | + return true; |
| 57 | + }); |
| 58 | + |
| 59 | + bot.registerCommand(['querychu', 'qchu'], async (event) => { |
| 60 | + const ctx = getContext(event); |
| 61 | + const profile = await ctx.getCurrentProfile(); |
| 62 | + const kw = event.params.join(' ').trim(); |
| 63 | + if (!kw) { |
| 64 | + await event.reply() |
| 65 | + .setText('请输入关键词') |
| 66 | + .dispatch(); |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + const results = ChuniSong.search(kw); |
| 71 | + |
| 72 | + if (!results.length) { |
| 73 | + await event.reply() |
| 74 | + .setText('找不到匹配的歌') |
| 75 | + .dispatch(); |
| 76 | + return true; |
| 77 | + } |
| 78 | + const userMusic = await profile.getChuniUserMusic(results); |
| 79 | + for (const song of results) { |
| 80 | + const userScores = userMusic.filter(it => it.musicId === song.id || it.musicId === song.id + 1e4); |
| 81 | + if (!userScores.length) continue; |
| 82 | + _.sortBy(userScores, it => it.level); |
| 83 | + |
| 84 | + const message = [song.id + '. ' + song.title, '']; |
| 85 | + for (const userScore of userScores) { |
| 86 | + const chart = song.notes[userScore.level]; |
| 87 | + message.push(`${CHU_LEVEL_EMOJI[userScore.level]} ${chart.lv.toFixed(1)} ` + |
| 88 | + `${userScore.scoreMax.toLocaleString('zh-CN')} = ${chusanRating(chart.lv, userScore.scoreMax)} ${userScore.isAllJustice ? 'AJ' : ''} ${userScore.isFullCombo ? 'FC' : ''}`); |
| 89 | + } |
| 90 | + |
| 91 | + const reply = event.reply() |
| 92 | + .setText(message.join('\n')) |
| 93 | + .addButtons(new MessageButtonSwitchInline('歌曲详情', 'chu ' + song.id.toString())); |
| 94 | + // if (musicToFile[song.id]) { |
| 95 | + // reply.addAudio(musicToFile[song.id]); |
| 96 | + // } else { |
| 97 | + reply.addPhoto(song.coverUrl); |
| 98 | + // } |
| 99 | + await reply.dispatch(); |
| 100 | + return true; |
| 101 | + } |
| 102 | + await event.reply() |
| 103 | + .setText(`共找到 ${results.length} 个结果:\n\n` + |
| 104 | + results.map(song => `${song.title} ${song.id ? '' : '(ID 缺失)'}`).join('\n') + |
| 105 | + // 如果有 ID 缺失就不一定没玩过了 |
| 106 | + (results.some(it => !it.id) ? '' : '\n\n可惜你都没玩过')) |
| 107 | + .dispatch(); |
| 108 | + }); |
| 109 | +} |
0 commit comments