kotori-plugin-requester
Version:
Listen delete message and request event call to master
207 lines (204 loc) • 9.37 kB
JavaScript
/**
* @Package kotori-plugin-requester
* @Version 1.1.2
* @Author Himeno <me@hotaru.icu>
* @Copyright 2024 Hotaru. All rights reserved.
* @License GPL-3.0
* @Link https://github.com/kotorijs/kotori
* @Date 2024/8/8 20:54:13
*/
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var src_exports = {};
__export(src_exports, {
config: () => config,
inject: () => inject,
lang: () => lang,
main: () => main
});
module.exports = __toCommonJS(src_exports);
var import_kotori_bot = require("kotori-bot");
const lang = [__dirname, "../locales"];
const config = import_kotori_bot.Tsu.Object({
print: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to print the log"),
notify: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to send message to master"),
filterCmd: import_kotori_bot.Tsu.Boolean().default(true).describe("Filter message about command"),
onHttpRequest: import_kotori_bot.Tsu.Union(
import_kotori_bot.Tsu.Boolean(),
import_kotori_bot.Tsu.Custom(
(input) => Array.isArray(input) && input.every((el) => ["get", "post", "put", "delete", "patch", "head", "options", "ws"].includes(String(el)))
)
).default(["get", "post", "put", "delete", "patch"]).describe("Whether to listen to http request (only print)"),
onRegexp: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to regexp (only print)"),
onCommand: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to command (only print)"),
onBotGroupIncrease: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to bot group increase"),
onBotGroupDecrease: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to bot group decrease"),
onBotGroupAdmin: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to bot group admin"),
onBotGroupBan: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to bot group ban"),
onGroupRecall: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to group recall"),
onPrivateRecall: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to private recall"),
onGroupRequest: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to group request"),
onPrivateRequest: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to private request"),
onGroupMsg: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to group message"),
onPrivateMsg: import_kotori_bot.Tsu.Boolean().default(true).describe("Whether to listen to private message")
});
const inject = ["cache"];
function main(ctx, cfg) {
const log = (identity, ...args) => {
if (cfg.print) ctx.logger.label(identity).record(...args);
};
const send = (session) => (msg) => {
if (cfg.filterCmd && session.api.adapter.platform === "cmd") return;
const isString = typeof msg === "string";
session.api.sendPrivateMsg(
isString ? session.i18n.locale(msg) : session.format(msg[0], msg[1]),
session.api.adapter.config.master
);
log(
`${session.api.adapter.platform}/${session.api.adapter.identity}`,
isString ? session.i18n.locale(msg.replace(".msg.", ".log.")) : session.format(msg[0].replace(".msg.", ".log."), msg[1]).toString()
);
};
if (cfg.onBotGroupIncrease) {
ctx.on("on_group_increase", (session) => {
if (session.userId !== session.api.adapter.selfId) return;
send(session)(["requester.msg.increase", [session.groupId]]);
});
}
if (cfg.onBotGroupDecrease) {
ctx.on("on_group_decrease", (session) => {
if (session.userId !== session.api.adapter.selfId) return;
const equaled = session.operatorId === session.userId;
send(session)([
`requester.msg.decrease.${equaled ? "leave" : "kick"}`,
equaled ? [session.groupId] : [session.operatorId, session.groupId]
]);
});
}
if (cfg.onBotGroupAdmin) {
ctx.on("on_group_admin", (session) => {
if (session.userId !== session.api.adapter.selfId) return;
send(session)([`requester.msg.admin.${session.operation}`, [session.groupId]]);
});
}
if (cfg.onBotGroupBan) {
ctx.on("on_group_ban", (session) => {
if (session.userId !== session.api.adapter.selfId) return;
send(session)([
`requester.msg.ban.${session.time > 0 ? "ban" : "lift_ban"}`,
[session.operatorId, session.groupId, session.time ? session.time / 60 : 0]
]);
});
}
if (cfg.onPrivateRecall || cfg.onGroupRecall) {
ctx.on("on_message_delete", (session) => {
if (session.userId === session.api.adapter.selfId || session.operatorId === session.api.adapter.selfId) return;
if (cfg.onPrivateRecall && session.type === import_kotori_bot.MessageScope.PRIVATE) {
const message = ctx.cache.get(`${session.api.adapter.identity}${session.messageId}`);
if (!message) return;
send(session)(["requester.msg.recall.private", [session.userId, message]]);
} else if (cfg.onGroupRecall && session.type === import_kotori_bot.MessageScope.GROUP) {
const message = ctx.cache.get(`${session.api.adapter.identity}${session.messageId}`);
if (!message) return;
const equaled = session.operatorId === session.userId;
send(session)([
`requester.msg.recall.group.${equaled ? "self" : "other"}`,
[session.userId, ...equaled ? [session.groupId, message] : [session.operatorId, session.groupId, message]]
]);
}
});
}
if (cfg.onPrivateRequest || cfg.onGroupRequest) {
ctx.on("on_request", (session) => {
if (cfg.onPrivateRequest && session.type === import_kotori_bot.MessageScope.PRIVATE) {
send(session)(["requester.msg.request.private", [session.userId]]);
} else if (cfg.onGroupMsg && session.type === import_kotori_bot.MessageScope.GROUP) {
send(session)(["requester.msg.request.group", [session.userId, session.groupId]]);
}
});
}
if (cfg.onGroupMsg || cfg.onPrivateMsg) {
ctx.on("on_message", (session) => {
if (session.userId === session.api.adapter.selfId) return;
if (session.type === import_kotori_bot.MessageScope.GROUP && cfg.onGroupRecall) {
ctx.cache.set(`${session.api.adapter.identity}${session.messageId}`, session.message);
} else if (session.type === import_kotori_bot.MessageScope.PRIVATE && cfg.onPrivateRecall) {
ctx.cache.set(`${session.api.adapter.identity}${session.messageId}`, session.message);
}
if (session.userId !== session.api.adapter.config.master) return;
if (cfg.onPrivateMsg && session.type === import_kotori_bot.MessageScope.PRIVATE) {
send(session)(["requester.msg.msg.private", [session.userId, session.message]]);
}
});
}
if (cfg.onCommand) {
ctx.on(
"command",
({ session, raw }) => log(
`${session.api.adapter.platform}/${session.api.adapter.identity}`,
session.format(`requester.log.command.${session.type === import_kotori_bot.MessageScope.GROUP ? "group" : "private"}`, [
session.userId,
raw,
session.groupId
])
)
);
}
if (cfg.onRegexp) {
ctx.on(
"regexp",
({ session, raw }) => log(
`${session.api.adapter.platform}/${session.api.adapter.identity}`,
session.format(`requester.log.regexp.${session.type === import_kotori_bot.MessageScope.GROUP ? "group" : "private"}`, [
session.userId,
raw,
session.groupId
])
)
);
}
if (cfg.onHttpRequest) {
const originHttp = ctx.root.http;
const http = ctx.root.http;
for (const method of cfg.onHttpRequest === true ? ["get", "post", "put", "delete", "patch", "head", "options", "ws"] : cfg.onHttpRequest) {
if (method === "ws") {
http.ws = new Proxy(http.ws, {
apply(target, thisArg, argArray) {
log(method, (0, import_kotori_bot.stringFormat)(ctx.i18n.locale("requester.log.http"), [argArray[0]]));
return Reflect.apply(target, thisArg, argArray);
}
});
return;
}
http[method] = new Proxy(http[method], {
apply(target, thisArg, argArray) {
log(method, (0, import_kotori_bot.stringFormat)(ctx.i18n.locale("requester.log.http"), [argArray[0]]));
return Reflect.apply(target, thisArg, argArray);
}
});
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
config,
inject,
lang,
main
});