koishi-plugin-beatsaber-bot
Version:
一个 用于 BeatSaber 的 koishi Bot 插件
62 lines (61 loc) • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BeatSaverWS = void 0;
const koishi_1 = require("koishi");
function BeatSaverWS(ctx, config, render, logger) {
const ws = ctx.http.ws(config.beatSaverWSURL ?? "wss://ws.beatsaver.com/maps");
ws.on('open', (evt) => {
logger.info("BeatsaverWS opened");
});
ws.on("message", async (event) => {
// const data = MockBeatsaverWsEvent
const data = JSON.parse(event.toString());
logger.info("Beatsaver message received", data.type, data?.msg?.id);
if (data.type == "MAP_UPDATE") {
const bsmap = data.msg;
if (!bsmap.versions.some(it => it.state == "Published")) {
return;
}
// if(bsmap.declaredAi != "None") {
// return
// }
const userId = bsmap.uploader.id;
const selection = ctx.database.join(['BSBotSubscribe', 'BSRelateOAuthAccount', 'BSSubscribeMember', 'user']);
const subscribe = await selection.where(row => koishi_1.$.and(koishi_1.$.eq(row.BSBotSubscribe.enable, true), koishi_1.$.eq(row.BSBotSubscribe.id, row.BSSubscribeMember.subscribeId), koishi_1.$.eq(row.BSBotSubscribe.type, "beatsaver"), koishi_1.$.eq(row.BSRelateOAuthAccount.platform, 'beatsaver'), koishi_1.$.eq(row.user.id, row.BSRelateOAuthAccount.uid), koishi_1.$.eq(row.user.id, row.BSSubscribeMember.memberUid), koishi_1.$.eq(row.BSRelateOAuthAccount.platformUid, userId.toString()))).execute();
const subscription = subscribe.map(item => ({
account: item.BSRelateOAuthAccount,
subscribe: item.BSBotSubscribe,
member: item.BSSubscribeMember,
user: item.user
}));
for (const item of subscription) {
logger.info('send msg to', item.subscribe);
const bot = ctx.bots[`${item.subscribe.platform}:${item.subscribe.selfId}`];
if (!bot) {
continue;
}
let texts = [];
const res = await ctx.database.get('binding', {
platform: item.subscribe.platform,
aid: item.member.memberUid
});
if (res.length > 0) {
logger.info('mapperId in platform', res[0].platform);
texts = [`本群谱师`, koishi_1.h.at(res[0].pid), ` 刚刚发布了新谱面,「${bsmap.name}」`];
}
else {
texts = [`谱师「${bsmap.uploader.name}」刚刚发布了新谱面,「${bsmap.name}」`];
}
let image = render.renderMap(bsmap);
await bot.sendMessage(item.subscribe.channelId, (0, koishi_1.h)('message', texts));
await bot.sendMessage(item.subscribe.channelId, await image);
await bot.sendMessage(item.subscribe.channelId, koishi_1.h.audio(bsmap.versions[0].previewURL));
}
}
});
ws.addEventListener('close', (evt) => {
logger.info("BeatsaverWS closed");
});
return ws;
}
exports.BeatSaverWS = BeatSaverWS;