koishi-plugin-verifier
Version:
Verify Friend / Guild Requests for Koishi
69 lines (68 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.apply = exports.Config = exports.name = void 0;
const koishi_1 = require("koishi");
const RequestHandler = koishi_1.Schema.union([
koishi_1.Schema.const(undefined).description('无操作'),
koishi_1.Schema.const(true).description('全部通过'),
koishi_1.Schema.const(false).description('全部拒绝'),
koishi_1.Schema.natural().description('权限等级').default(0),
koishi_1.Schema.string().hidden(),
koishi_1.Schema.function().hidden(),
]);
async function useGeneralHandler(handler, session, prefer) {
const result = typeof handler === 'function' ? await handler(session) : handler;
if (typeof result === 'string') {
return [prefer, result];
}
else if (typeof result === 'boolean') {
return [result];
}
}
async function checkUserAuthority(session, authority) {
const user = await session.observeUser(['authority']);
if (user.authority >= authority)
return [true];
}
async function checkChannelAuthority(session, authority) {
const channel = await session.observeChannel(['assignee']);
if (channel.assignee)
return [true];
const user = await session.observeUser(['authority']);
if (user.authority >= authority) {
channel.assignee = session.selfId;
await channel.$update();
return [true];
}
}
exports.name = 'verifier';
exports.Config = koishi_1.Schema.object({
onFriendRequest: RequestHandler.description('如何响应好友请求?'),
onGuildMemberRequest: RequestHandler.description('如何响应入群申请?'),
onGuildRequest: RequestHandler.description('如何响应入群邀请?'),
});
function apply(ctx, config = {}) {
const { onFriendRequest, onGuildRequest, onGuildMemberRequest } = config;
ctx.on('friend-request', async (session) => {
const result = typeof onFriendRequest === 'number'
? await checkUserAuthority(session, onFriendRequest)
: await useGeneralHandler(onFriendRequest, session, true);
if (result)
return session.bot.handleFriendRequest(session.messageId, ...result);
});
ctx.on('guild-request', async (session) => {
const result = typeof onGuildRequest === 'number'
? await checkChannelAuthority(session, onGuildRequest)
: await useGeneralHandler(onGuildRequest, session, false);
if (result)
return session.bot.handleGuildRequest(session.messageId, ...result);
});
ctx.on('guild-member-request', async (session) => {
const result = typeof onGuildMemberRequest === 'number'
? await checkUserAuthority(session, onGuildMemberRequest)
: await useGeneralHandler(onGuildMemberRequest, session, false);
if (result)
return session.bot.handleGuildMemberRequest(session.messageId, ...result);
});
}
exports.apply = apply;