koishi-plugin-kbot
Version:
A muti-function qq bot for koishi
58 lines (57 loc) • 2.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGroupMemberRole = void 0;
/*
* @Author: Kabuda-czh
* @Date: 2023-02-01 10:36:24
* @LastEditors: Kabuda-czh
* @LastEditTime: 2023-06-28 17:22:38
* @FilePath: \KBot-App\plugins\kbot\src\plugins\guildManage\utils\index.ts
* @Description:
*
* Copyright (c) 2023 by Kabuda-czh, All Rights Reserved.
*/
const node_fs_1 = __importDefault(require("node:fs"));
const node_url_1 = require("node:url");
const __1 = require("..");
function handleFunction(context, functionName, ...args) {
return async (ctx) => {
await Promise.all(context.bots.flatMap(bot => (bot.platform === 'onebot' && bot.selfId === (ctx.query[args[0]] || ctx.request.body[args[0]]) && bot.status === 'online')
? functionName.includes('internal.')
? bot.internal?.[functionName.slice(functionName.indexOf('.') + 1)](...args.slice(1).map(arg => (arg = (ctx.query?.[arg] || ctx.request?.body?.[arg] || ''))))
: bot?.[functionName](...args.slice(1).map(arg => (arg = (ctx.query?.[arg] || ctx.request?.body?.[arg] || ''))))
: []))
.then((res) => {
if (['internal.sendGroupNotice', 'internal.setGroupPortrait'].includes(functionName)) {
setTimeout(() => {
deleteTempFile(ctx.query?.[args[args.length - 1]] || ctx.request.body?.[args[args.length - 1]]);
}, 60 * 1000);
}
ctx.body = res.flat();
})
.catch((err) => {
__1.logger.error(err);
ctx.body = null;
});
};
}
exports.default = handleFunction;
async function deleteTempFile(filePath) {
if (!filePath)
return;
try {
await node_fs_1.default.promises.rm((0, node_url_1.fileURLToPath)(filePath));
__1.logger.info(`已删除临时文件 ${filePath}`);
}
catch (err) {
__1.logger.error(`删除临时文件 ${filePath} 失败:${err}`);
}
}
async function getGroupMemberRole(bot, groupId, userId) {
const res = await bot.internal.getGroupMemberInfo(groupId, userId);
return res.role;
}
exports.getGroupMemberRole = getGroupMemberRole;