@kotori-bot/kotori-plugin-filter
Version:
filter plugin for kotori-bot loader
102 lines (99 loc) • 4.48 kB
JavaScript
/**
* @Package @kotori-bot/kotori-plugin-filter
* @Version 1.1.0
* @Author Hotaru <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:08
*/
;
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 __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
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 __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
if (kind && result) __defProp(target, key, result);
return result;
};
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var src_exports = {};
__export(src_exports, {
FilterPlugin: () => FilterPlugin
});
module.exports = __toCommonJS(src_exports);
var import_kotori_bot = require("kotori-bot");
var import_picomatch = __toESM(require("picomatch"));
const plugin = (0, import_kotori_bot.plugins)([__dirname, "../"]);
let FilterPlugin = class extends import_kotori_bot.KotoriPlugin {
constructor(ctx, config) {
super(ctx, config);
this.filterModules();
this.filterSet();
}
filterModules() {
const { mode, list } = this.config;
this.ctx.logger.record(`get loader successfully, filter mode: ${mode}`);
for (const [key, [ModuleMeta]] of this.ctx[import_kotori_bot.Symbols.modules]) {
if (import_kotori_bot.CORE_MODULES.includes(key)) return;
const result = list.some((pattern) => (0, import_picomatch.default)(pattern)(key).valueOf());
if (result && mode === "exclude" || !result && mode === "include") {
this.ctx[import_kotori_bot.Symbols.modules].delete(key);
ModuleMeta.main = void 0;
}
}
}
filterSet() {
for (const [name, [, { filter }]] of this.ctx[import_kotori_bot.Symbols.modules]) {
if (!filter) continue;
const result = import_kotori_bot.filterOptionSchema.parseSafe(filter);
if (!result.value) {
this.ctx.emit("error", new import_kotori_bot.KotoriError(`filter option of module ${name} is invalid: ${result.error.message}`));
continue;
}
this.ctx[import_kotori_bot.Symbols.filter].set(name, new import_kotori_bot.Filter(result.data));
}
}
};
__publicField(FilterPlugin, "schema", import_kotori_bot.Tsu.Object({
mode: import_kotori_bot.Tsu.Union(import_kotori_bot.Tsu.Literal("include"), import_kotori_bot.Tsu.Literal("exclude")).default("exclude").describe("Filter mode"),
list: import_kotori_bot.Tsu.Array(import_kotori_bot.Tsu.String()).default([]).describe("Filter list (supports string match)")
}));
__decorateClass([
plugin.schema
], FilterPlugin, "schema", 2);
FilterPlugin = __decorateClass([
plugin.import
], FilterPlugin);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FilterPlugin
});