koishi-plugin-kbot
Version:
A muti-function qq bot for koishi
117 lines (116 loc) • 6.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.apply = exports.Config = void 0;
const jsx_runtime_1 = require("@satorijs/element/jsx-runtime");
const koishi_1 = require("koishi");
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const { version: pVersion } = require('../../../package.json');
exports.Config = koishi_1.Schema.object({
platform: koishi_1.Schema.union([
koishi_1.Schema.const('netease').description('网易云音乐'),
koishi_1.Schema.const('qq').description('QQ音乐'),
]).default('qq').description('默认的点歌平台'),
showWarning: koishi_1.Schema.boolean().default(false).description('点歌失败时是否发送提示。'),
useImage: koishi_1.Schema.boolean().default(false).description('是否使用图片模式 (需要 puppeteer 支持!)'),
authority: koishi_1.Schema.number()
.default(2)
.min(1)
.description('设定指令的最低权限, 默认 2 级'),
});
const platforms = {
async netease(keyword) {
const data = await this.http.get('http://music.163.com/api/cloudsearch/pc', {
params: { s: keyword, type: 1, offset: 0, limit: 5 },
});
if (data.code !== 200 || data.result.songCount === 0)
return;
return data.result.songs.map(song => ({
type: '163',
id: song.id,
name: song.name,
artist: song.ar.map(artist => artist.name).join('/'),
url: `https://music.163.com/#/song?id=${song.id}`,
album: song.al.name,
}));
},
async qq(keyword) {
const data = await this.http.get('https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg', {
params: { key: keyword, format: 'json' },
});
if (data.code || data.data.song.count === 0)
return;
return data.data.song.itemlist.map(song => ({
type: 'qq',
id: song.id,
name: song.name,
artist: song.singer,
url: `https://y.qq.com/n/ryqq/songDetail/${song.mid}`,
album: '',
}));
},
};
function apply(ctx, config) {
const { showWarning, platform, useImage } = config;
function list(results) {
if (ctx.puppeteer && useImage) {
return (0, jsx_runtime_1.jsxs)("html", { style: {
color: '#ffffff',
background: '#333333',
padding: '1rem',
fontSize: '14px',
lineHeight: '1.5',
fontFamily: 'sans-serif',
fontWeight: 'normal',
}, children: [(0, jsx_runtime_1.jsx)("style", { children: `
th, td {
padding: 0.25rem 0.5rem;
}
.index, .title {
text-align: center;
}
` }), (0, jsx_runtime_1.jsx)("p", { children: "\u6211\u4EEC\u627E\u5230\u4E86\u591A\u4E2A\u53EF\u80FD\u7B26\u5408\u6761\u4EF6\u7684\u6B4C\u66F2\uFF0C\u8BF7\u8F93\u5165\u5E8F\u53F7\u8FDB\u884C\u9009\u62E9\uFF1A" }), (0, jsx_runtime_1.jsxs)("table", { children: [(0, jsx_runtime_1.jsxs)("tr", { children: [(0, jsx_runtime_1.jsx)("th", { class: "index", children: "\u7F16\u53F7" }), (0, jsx_runtime_1.jsx)("th", { class: "title", children: "\u540D\u79F0" }), (0, jsx_runtime_1.jsx)("th", { children: "\u6B4C\u624B" })] }), results.map((t, i) => (0, jsx_runtime_1.jsxs)("tr", { children: [(0, jsx_runtime_1.jsx)("td", { children: i + 1 }), (0, jsx_runtime_1.jsx)("td", { children: t.name }), (0, jsx_runtime_1.jsx)("td", { children: t.artist })] }))] }), (0, jsx_runtime_1.jsxs)("p", { children: ["Generated by Koishi v", koishi_1.version, " / koishi-plugin-kbot v", pVersion] })] });
}
else {
return (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("p", { children: "\u6211\u4EEC\u627E\u5230\u4E86\u591A\u4E2A\u53EF\u80FD\u7B26\u5408\u6761\u4EF6\u7684\u6B4C\u66F2\uFF0C\u8BF7\u8F93\u5165\u5E8F\u53F7\u8FDB\u884C\u9009\u62E9\uFF1A" }), results.map((t, i) => (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("p", { children: [i + 1, ". ", t.name] }), (0, jsx_runtime_1.jsx)("p", { children: t.artist }), (0, jsx_runtime_1.jsx)("p", { children: t.album }), (0, jsx_runtime_1.jsx)("br", {})] }))] });
}
}
ctx.command('kbot/music <name:text>', '点歌')
// typescript cannot infer type from string templates
.option('platform', `-p <platform> 点歌平台,目前支持 qq, netease, 默认为 ${platform}`, { type: Object.keys(platforms), authority: config.authority })
.alias('点歌')
.shortcut('来一首', { fuzzy: true })
.shortcut('点一首', { fuzzy: true })
.shortcut('整一首', { fuzzy: true })
.action(async ({ session, options }, keyword) => {
if (!options.platform)
options.platform = platform;
const search = platforms[options.platform];
if (!search)
return `目前不支持平台 ${options.platform}。`;
if (!keyword)
return '请输入歌曲相关信息.';
try {
const result = await search.call(ctx, keyword);
if (Array.isArray(result) && result.length > 0) {
if (result.length > 1) {
await session.send(list(result));
const input = await session.prompt();
if (!input || Number.isNaN(+input))
return '请输入正确的序号。';
const index = +input - 1;
if (!result[index])
return '请输入正确的序号。';
return (0, koishi_1.segment)('onebot:music', result[index], `${result[index].name}\n${result[index].artist}\n${result[index].url}`);
}
const { name, artist, url } = result[0];
return (0, koishi_1.segment)('onebot:music', result, `${name}\n${artist}\n${url}`);
}
}
catch (e) {
console.warn(e);
}
if (showWarning)
return '点歌失败,请尝试更换平台。';
});
}
exports.apply = apply;