@kotori-bot/core
Version:
Kotori Core
252 lines (251 loc) • 10.5 kB
JavaScript
/**
* @Package @kotori-bot/core
* @Version 1.7.0
* @Author Arimura Sena <me@hotaru.icu>
* @Copyright 2024-2025 Hotaru. All rights reserved.
* @License BAN-ZHINESE-USING
* @Link https://github.com/kotorijs/kotori
* @Date 17:26:09
*/
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var message_exports = {};
__export(message_exports, {
Message: () => Message,
default: () => message_default
});
module.exports = __toCommonJS(message_exports);
var import_cron = require("cron");
var import_types = require("../types");
var import_internal = require("../utils/internal");
var import_components = require("../components");
var import_error = require("../utils/error");
var import_global = require("../global");
var import_node_crypto = require("node:crypto");
var import_utils = __toESM(require("../decorators/utils"));
class Message {
[import_global.Symbols.midware] = /* @__PURE__ */ new Set();
[import_global.Symbols.command] = /* @__PURE__ */ new Set();
[import_global.Symbols.regexp] = /* @__PURE__ */ new Set();
[import_global.Symbols.task] = /* @__PURE__ */ new Set();
[import_global.Symbols.filter] = /* @__PURE__ */ new Map();
[import_global.Symbols.promise] = /* @__PURE__ */ new Map();
handleMidware(session) {
const { api } = session;
api.adapter.status.receivedMsg += 1;
const setup = Array.from(this[import_global.Symbols.midware].values()).sort((first, last) => first.priority - last.priority).map(({ callback }) => callback).reverse().reduce(
(first, last) => {
const next = () => {
first(() => {
}, session);
};
return () => {
last(next, session);
};
},
(_, session2) => {
this.handleCommand(session2);
this.handleRegexp(session2);
}
);
setup(() => {
}, session);
}
async handleRegexp(session) {
for (const data of this[import_global.Symbols.regexp]) {
const result = session.message.match(data.match);
if (!result) continue;
const cancel = (0, import_internal.cancelFactory)();
this.ctx.emit("before_regexp", {
session,
regexp: data.match,
raw: session.message.toString(),
cancel: cancel.get()
});
if (cancel.value) continue;
session.quick(data.callback(result, session));
this.ctx.emit("regexp", { result, session, regexp: data.match, raw: session.message.toString() });
}
}
async handleCommand(session) {
const prefix = session.api.adapter.config.commandPrefix ?? this.ctx.config.global.commandPrefix;
for (const cmd of this[import_global.Symbols.command]) {
for (const shortcut of cmd.meta.shortcut) {
if (!session.message.startsWith(shortcut)) continue;
session.message = session.message.replace(shortcut, `${prefix}${cmd.meta.root}`);
}
}
if (!session.message.startsWith(prefix)) return;
const raw = session.message.slice(prefix.length);
if (!raw) return;
let matched;
for (const cmd of this[import_global.Symbols.command]) {
if (matched || !cmd.meta.action) continue;
const result2 = import_components.Command.run(raw, cmd.meta);
if (result2 instanceof import_error.CommandError && result2.value.type === "unknown") continue;
matched = cmd;
const cancel = (0, import_internal.cancelFactory)();
this.ctx.emit("before_command", { command: cmd, result: result2, raw, session, cancel: cancel.get() });
if (cancel.value) continue;
if (result2 instanceof import_error.CommandError) continue;
try {
const executed = await cmd.meta.action({ args: result2.args, options: result2.options }, session);
if (executed !== void 0) session.quick(executed);
this.ctx.emit("command", {
command: cmd,
result: executed,
raw,
session
});
} catch (error) {
this.ctx.emit("command", {
command: matched,
result: error instanceof import_error.CommandError ? error : new import_error.CommandError({ type: "error", error }),
raw,
session
});
}
}
if (matched) return;
const result = new import_error.CommandError({ type: "unknown", input: raw });
this.ctx.emit("command", { command: new import_components.Command(""), result, raw, session });
}
ctx;
constructor(ctx) {
this.ctx = ctx;
this.ctx.on("ready", () => {
for (const bots of this.ctx[import_global.Symbols.bot].values()) {
for (const bot of bots) {
for (const key of ["sendPrivateMsg", "sendGroupMsg", "sendChannelMsg"]) {
;
bot[key] = new Proxy(bot[key], {
apply: (target, thisArg, argArray) => {
const [message, id1, id2] = argArray;
const cancel = (0, import_internal.cancelFactory)();
this.ctx.emit("before_send", {
api: bot,
message,
cancel: cancel.get(),
target: key === "sendPrivateMsg" ? { type: import_types.MessageScope.PRIVATE, userId: id1 } : key === "sendGroupMsg" ? { type: import_types.MessageScope.GROUP, groupId: id1 } : { type: import_types.MessageScope.CHANNEL, guildId: id1, channelId: id2 }
});
return cancel.value ? { messageId: "", time: 0 } : Reflect.apply(target, thisArg, argArray);
}
});
}
}
}
});
this.ctx.on("on_message", (session) => this.handleMidware(session));
this.ctx.on("before_send", (data) => {
const { api } = data;
api.adapter.status.sentMsg += 1;
api.adapter.status.lastMsgTime = /* @__PURE__ */ new Date();
});
const test = (identity, session) => {
for (const [key, filter] of this[import_global.Symbols.filter].entries()) {
if (identity.toString().includes(key)) return filter.test(session);
}
return true;
};
this.ctx.on("before_command", (data) => {
const { identity } = (0, import_internal.getCommandMeta)(data.command) ?? {};
if (identity && !test(identity, data.session)) {
data.cancel();
const result = new import_error.CommandError({ type: "unknown", input: data.raw });
this.ctx.emit("command", { command: new import_components.Command(""), result, raw: data.raw, session: data.session });
}
});
this.ctx.on("before_regexp", (data) => {
const { callback } = Array.from(this[import_global.Symbols.regexp]).find(({ match }) => match === data.regexp) ?? {};
if (!callback) return;
const { identity } = (0, import_internal.getRegExpMeta)(callback) ?? {};
if (identity && !test(identity, data.session)) data.cancel();
});
this.midware((next, session) => {
const resolves = this[import_global.Symbols.promise].get(session.id);
if (resolves) {
for (const resolve of resolves) resolve(session.message);
return;
}
next();
}, 10);
import_utils.default.setup(this.ctx);
}
midware(callback, priority = 100) {
(0, import_internal.setMidwareMeta)(callback, { identity: this.ctx.identity, priority });
const data = { callback, priority };
this[import_global.Symbols.midware].add(data);
return () => this[import_global.Symbols.midware].delete(data);
}
command(template, config) {
const command = new import_components.Command(template, config);
this[import_global.Symbols.command].add(command);
(0, import_internal.setCommandMeta)(command, { identity: this.ctx.identity, ...command.meta });
return command;
}
regexp(match, callback) {
(0, import_internal.setRegExpMeta)(callback, { identity: this.ctx.identity, match });
const data = { match, callback };
this[import_global.Symbols.regexp].add(data);
return () => this[import_global.Symbols.regexp].delete(data);
}
boardcast() {
}
notify() {
const mainAdapterIdentity = Object.keys(this.ctx.config.adapter)[0];
for (const apis of this.ctx[import_global.Symbols.bot].values()) {
for (const api of apis) {
if (api.adapter.identity !== mainAdapterIdentity) continue;
}
}
}
task(options, callback) {
const [baseOption, extraOptions] = typeof options === "string" ? [options, {}] : [options.cron, options];
const task = new import_cron.CronJob(
baseOption,
() => callback(this.ctx),
null,
extraOptions.start ?? true,
extraOptions.timeZone
);
(0, import_internal.setTaskMeta)(task, { identity: this.ctx.identity, task, options });
this[import_global.Symbols.task].add(task);
return () => this[import_global.Symbols.task].delete(task);
}
filter(option) {
const filterId = (0, import_node_crypto.randomUUID)().slice(0, 12).replaceAll("-", "");
this[import_global.Symbols.filter].set(filterId, new import_components.Filter(option));
return this.ctx.extends(void 0, `${this.ctx.identity?.toString()}_${filterId}`);
}
}
var message_default = Message;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Message
});