koishi-plugin-filter-pro 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.mjs +1 -1
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`koishi`),t=require(`@koishijs/plugin-console`),n=require(`node:path`),r=require(`node:fs/promises`);const i=e.Schema.object({filename:e.Schema.string().default(`rules.json`).description(`持久化文件名(位于 data/filterpro/ 下)。`)}),a=Symbol.for(`koishi.loader.record`);var o=class extends t.DataService{constructor(e,t){super(e,`filter-pro`),this.state=t}async get(){return u(this.state.rules).map(c)}};function s(){return{type:`group`,operator:`and`,children:[{type:`compare`,field:`guildId`,operator:`eq`,value:``}]}}function c(e){return{id:e.id,name:e.name,enabled:e.enabled,priority:e.priority,action:e.action,target:{...e.target},condition:l(e.condition),response:e.response}}function l(e){return e.type===`group`?{type:`group`,operator:e.operator,children:e.children.map(l)}:e.type===`not`?{type:`not`,child:l(e.child)}:{type:`compare`,field:e.field,operator:e.operator,value:e.value}}function u(e){return[...e].sort((e,t)=>e.priority-t.priority||e.id.localeCompare(t.id))}function d(e){if(!e||typeof e!=`object`)return s();let t=e;if(t.type===`group`)return{type:`group`,operator:t.operator===`or`?`or`:`and`,children:Array.isArray(t.children)?t.children.map(d):[]};if(t.type===`not`)return{type:`not`,child:d(t.child)};let n=[`eq`,`ne`,`includes`,`regex`,`gt`,`gte`,`lt`,`lte`,`exists`].includes(t.operator)?t.operator:`eq`;return{type:`compare`,field:typeof t.field==`string`?t.field:`content`,operator:n,value:t.value}}function f(t){let n=t.target||{type:`global`},r=typeof n.value==`string`?n.value.trim():``,i=n.type===`plugin`?r:``,a={type:n.type===`plugin`?`plugin`:`global`,value:i};return{id:t.id||e.Random.id(8),name:t.name||`new-rule`,enabled:t.enabled??!0,priority:t.priority??0,action:t.action||`block`,target:a,condition:d(t.condition),response:t.response??``}}function p(e,t){if(!e||e.type===`global`)return!0;let n=typeof t.pluginKey==`string`?t.pluginKey:``,r=e.value?.trim();return r?n===r:!1}function m(e){let t=[],n=new Set,r=new Set,i=e=>{let o=e?.scope?.[a];if(!(!o||r.has(o))){r.add(o);for(let[e,r]of Object.entries(o)){let a=String(e).replace(/^~/,``),[o,s=``]=a.split(`:`,2);o&&o!==`group`&&!n.has(a)&&(n.add(a),t.push({key:a,name:o,ident:s,label:s?`${o}:${s}`:o})),i(r?.ctx)}}};return i(e.root),t.sort((e,t)=>e.key.localeCompare(t.key))}function h(e){let t=[],n=new Map;return{rebuild:()=>{t=m(e),n.clear();let r=new Set,i=e=>{let o=e?.scope?.[a];if(!(!o||r.has(o))){r.add(o);for(let[e,r]of Object.entries(o)){let a=String(e).replace(/^~/,``),o=t.find(e=>e.key===a);o&&r?.ctx?.scope&&n.set(r.ctx.scope,o),i(r?.ctx)}}};i(e.root)},list:()=>t,resolveByCommand:e=>{let t=e?.caller?.scope,r=new Set;for(;t&&!r.has(t);){r.add(t);let e=n.get(t);if(e)return e;t=t.parent?.scope}}}}function g(e,t){if(!t)return;let n=t.split(`.`),r=e;for(let e of n){if(!r||typeof r!=`object`)return;r=r[e]}return r}function _(e){if(typeof e==`number`)return Number.isFinite(e)?e:null;if(typeof e==`string`&&e.trim()){let t=Number(e);return Number.isFinite(t)?t:null}return null}function v(e){return e==null||typeof e==`object`?e:String(e)}function y(e,t){if(t.operator===`exists`)return e!=null;let n=t.value,r=v(e),i=v(n);if(t.operator===`eq`)return r===i;if(t.operator===`ne`)return r!==i;if(t.operator===`includes`)return typeof r==`string`?r.includes(String(i??``)):Array.isArray(e)?e.map(e=>v(e)).includes(i):!1;if(t.operator===`regex`){if(typeof r!=`string`)return!1;try{return new RegExp(String(i??``)).test(r)}catch{return!1}}let a=_(e),o=_(n);return a===null||o===null?!1:t.operator===`gt`?a>o:t.operator===`gte`?a>=o:t.operator===`lt`?a<o:t.operator===`lte`?a<=o:!1}function b(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>b(e,t)):e.children.some(e=>b(e,t)):!0:e.type===`not`?!b(e.child,t):y(g(t,e.field),e)}async function x(e){try{let t=await(0,r.readFile)(e,`utf8`),n=JSON.parse(t);return Array.isArray(n)?n.map(e=>f(e)):[]}catch{return null}}function S(e){let t=Promise.resolve();return async n=>{t=t.catch(()=>{}).then(async()=>{await(0,r.writeFile)(e,JSON.stringify(n.map(c),null,2),`utf8`)}),await t}}function C(e,t={}){let i=(0,n.join)(e.baseDir||process.cwd(),`data`,`filterpro`),a=(0,n.join)(i,t.filename||`rules.json`),s={rules:[]},l=S(a),d=h(e),m=(async()=>{await(0,r.mkdir)(i,{recursive:!0});let e=await x(a);if(e){s.rules=e;return}s.rules=[],await l(s.rules)})().catch(t=>{e.logger(`filter-pro`).warn(`failed to initialize persistent rules: %s`,String(t)),s.rules=[]}),
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`koishi`),t=require(`@koishijs/plugin-console`),n=require(`node:path`),r=require(`node:fs/promises`);const i=e.Schema.object({filename:e.Schema.string().default(`rules.json`).description(`持久化文件名(位于 data/filterpro/ 下)。`),debug:e.Schema.boolean().default(!1).description(`输出规则匹配调试日志。`)}),a=Symbol.for(`koishi.loader.record`);var o=class extends t.DataService{constructor(e,t){super(e,`filter-pro`),this.state=t}async get(){return u(this.state.rules).map(c)}};function s(){return{type:`group`,operator:`and`,children:[{type:`compare`,field:`guildId`,operator:`eq`,value:``}]}}function c(e){return{id:e.id,name:e.name,enabled:e.enabled,priority:e.priority,action:e.action,target:{...e.target},condition:l(e.condition),response:e.response}}function l(e){return e.type===`group`?{type:`group`,operator:e.operator,children:e.children.map(l)}:e.type===`not`?{type:`not`,child:l(e.child)}:{type:`compare`,field:e.field,operator:e.operator,value:e.value}}function u(e){return[...e].sort((e,t)=>e.priority-t.priority||e.id.localeCompare(t.id))}function d(e){if(!e||typeof e!=`object`)return s();let t=e;if(t.type===`group`)return{type:`group`,operator:t.operator===`or`?`or`:`and`,children:Array.isArray(t.children)?t.children.map(d):[]};if(t.type===`not`)return{type:`not`,child:d(t.child)};let n=[`eq`,`ne`,`includes`,`regex`,`gt`,`gte`,`lt`,`lte`,`exists`].includes(t.operator)?t.operator:`eq`;return{type:`compare`,field:typeof t.field==`string`?t.field:`content`,operator:n,value:t.value}}function f(t){let n=t.target||{type:`global`},r=typeof n.value==`string`?n.value.trim():``,i=n.type===`plugin`?r:``,a={type:n.type===`plugin`?`plugin`:`global`,value:i};return{id:t.id||e.Random.id(8),name:t.name||`new-rule`,enabled:t.enabled??!0,priority:t.priority??0,action:t.action||`block`,target:a,condition:d(t.condition),response:t.response??``}}function p(e,t){if(!e||e.type===`global`)return!0;let n=typeof t.pluginKey==`string`?t.pluginKey:``,r=e.value?.trim();return r?n===r:!1}function m(e){let t=[],n=new Set,r=new Set,i=e=>{let o=e?.scope?.[a];if(!(!o||r.has(o))){r.add(o);for(let[e,r]of Object.entries(o)){let a=String(e).replace(/^~/,``),[o,s=``]=a.split(`:`,2);o&&o!==`group`&&!n.has(a)&&(n.add(a),t.push({key:a,name:o,ident:s,label:s?`${o}:${s}`:o})),i(r?.ctx)}}};return i(e.root),t.sort((e,t)=>e.key.localeCompare(t.key))}function h(e){let t=[],n=new Map;return{rebuild:()=>{t=m(e),n.clear();let r=new Set,i=e=>{let o=e?.scope?.[a];if(!(!o||r.has(o))){r.add(o);for(let[e,r]of Object.entries(o)){let a=String(e).replace(/^~/,``),o=t.find(e=>e.key===a);o&&r?.ctx?.scope&&n.set(r.ctx.scope,o),i(r?.ctx)}}};i(e.root)},list:()=>t,resolveByCommand:e=>{let t=e?.caller?.scope,r=new Set;for(;t&&!r.has(t);){r.add(t);let e=n.get(t);if(e)return e;t=t.parent?.scope}}}}function g(e,t){if(!t)return;let n=t.split(`.`),r=e;for(let e of n){if(!r||typeof r!=`object`)return;r=r[e]}return r}function _(e){if(typeof e==`number`)return Number.isFinite(e)?e:null;if(typeof e==`string`&&e.trim()){let t=Number(e);return Number.isFinite(t)?t:null}return null}function v(e){return e==null||typeof e==`object`?e:String(e)}function y(e,t){if(t.operator===`exists`)return e!=null;let n=t.value,r=v(e),i=v(n);if(t.operator===`eq`)return r===i;if(t.operator===`ne`)return r!==i;if(t.operator===`includes`)return typeof r==`string`?r.includes(String(i??``)):Array.isArray(e)?e.map(e=>v(e)).includes(i):!1;if(t.operator===`regex`){if(typeof r!=`string`)return!1;try{return new RegExp(String(i??``)).test(r)}catch{return!1}}let a=_(e),o=_(n);return a===null||o===null?!1:t.operator===`gt`?a>o:t.operator===`gte`?a>=o:t.operator===`lt`?a<o:t.operator===`lte`?a<=o:!1}function b(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>b(e,t)):e.children.some(e=>b(e,t)):!0:e.type===`not`?!b(e.child,t):y(g(t,e.field),e)}async function x(e){try{let t=await(0,r.readFile)(e,`utf8`),n=JSON.parse(t);return Array.isArray(n)?n.map(e=>f(e)):[]}catch{return null}}function S(e){let t=Promise.resolve();return async n=>{t=t.catch(()=>{}).then(async()=>{await(0,r.writeFile)(e,JSON.stringify(n.map(c),null,2),`utf8`)}),await t}}function C(e,t={}){let i=(0,n.join)(e.baseDir||process.cwd(),`data`,`filterpro`),a=(0,n.join)(i,t.filename||`rules.json`),s={rules:[]},l=S(a),d=h(e),m=e.logger(`filter-pro`),g=!!t.debug,_=(e,t)=>{g&&m.info(`[trace:%s] %s`,e,JSON.stringify(t))},v=(async()=>{await(0,r.mkdir)(i,{recursive:!0});let e=await x(a);if(e){s.rules=e;return}s.rules=[],await l(s.rules)})().catch(t=>{e.logger(`filter-pro`).warn(`failed to initialize persistent rules: %s`,String(t)),s.rules=[]}),y=async()=>{await v,d.rebuild()};y(),e.on(`ready`,y),e.on(`internal/fork`,y),e.on(`internal/update`,y),e.on(`internal/runtime`,y),e.middleware(async(e,t)=>{await v;let n={platform:String(e.platform??``),selfId:String(e.selfId??``),userId:String(e.userId??``),channelId:String(e.channelId??``),guildId:String(e.guildId??``),isDirect:e.isDirect,content:String(e.content??``),type:String(e.type??``),event:e.event,author:e.author,quote:e.quote};_(`message:incoming`,{platform:n.platform,userId:n.userId,channelId:n.channelId,guildId:n.guildId,isDirect:n.isDirect,content:n.content,ruleCount:s.rules.length});for(let e of u(s.rules)){if(!e.enabled||e.target.type!==`global`)continue;if(!p(e.target,n)){_(`message:skip-target`,{ruleId:e.id,ruleName:e.name,target:e.target,pluginKey:n.pluginKey});continue}let r=b(e.condition,n);if(_(`message:evaluate`,{ruleId:e.id,ruleName:e.name,action:e.action,matched:r,expr:e.condition}),r)return e.action===`bypass`?(_(`message:action`,{ruleId:e.id,action:`bypass`}),t()):e.response?(_(`message:action`,{ruleId:e.id,action:`block`,response:e.response}),e.response):(_(`message:action`,{ruleId:e.id,action:`block`,response:``}),``)}return _(`message:pass`,{reason:`no-rule-matched`}),t()},!0),e.before(`command/execute`,async e=>{await v;let t=d.resolveByCommand(e.command),n={platform:String(e.session.platform??``),selfId:String(e.session.selfId??``),userId:String(e.session.userId??``),channelId:String(e.session.channelId??``),guildId:String(e.session.guildId??``),isDirect:e.session.isDirect,content:String(e.session.content??``),type:String(e.session.type??``),event:e.session.event,author:e.session.author,quote:e.session.quote,commandName:String(e.command.name??``),pluginKey:t?.key,pluginName:t?.name};_(`command:incoming`,{command:n.commandName,pluginKey:n.pluginKey,pluginName:n.pluginName,isDirect:n.isDirect,guildId:n.guildId,content:n.content,ruleCount:s.rules.length});for(let e of u(s.rules)){if(!e.enabled||e.target.type===`global`)continue;if(!p(e.target,n)){_(`command:skip-target`,{ruleId:e.id,ruleName:e.name,target:e.target,pluginKey:n.pluginKey});continue}let t=b(e.condition,n);if(_(`command:evaluate`,{ruleId:e.id,ruleName:e.name,action:e.action,matched:t,expr:e.condition}),t){if(e.action===`bypass`){_(`command:action`,{ruleId:e.id,action:`bypass`});return}return e.response?(_(`command:action`,{ruleId:e.id,action:`block`,response:e.response}),e.response):(_(`command:action`,{ruleId:e.id,action:`block`,response:``}),``)}}_(`command:pass`,{reason:`no-rule-matched`})}),e.inject([`console`],e=>{e.console.addEntry({dev:(0,n.resolve)(__dirname,`../client/index.ts`),prod:(0,n.resolve)(__dirname,`../dist`)});let t=new o(e,s),r=e.console.addListener.bind(e.console),i=async()=>{await l(s.rules),t.refresh()};r(`filter-pro/list`,async()=>(await v,t.get()),{authority:3}),r(`filter-pro/targets`,async()=>(await y(),d.list()),{authority:3}),r(`filter-pro/create`,async e=>{await v;let t=f(e||{});return s.rules.push(t),await i(),c(t)},{authority:3}),r(`filter-pro/update`,async e=>{if(await v,!e?.id)return null;let t=s.rules.findIndex(t=>t.id===e.id);if(t<0)return null;let n=s.rules[t];return s.rules[t]=f({...n,...e,id:n.id}),await i(),c(s.rules[t])},{authority:3}),r(`filter-pro/delete`,async e=>{await v;let t=s.rules.findIndex(t=>t.id===e);return t<0?!1:(s.rules.splice(t,1),await i(),!0)},{authority:3}),r(`filter-pro/reorder`,async e=>{if(await v,!Array.isArray(e))return t.get();let n=new Map(e.map((e,t)=>[e,t]));s.rules.sort((e,t)=>(n.has(e.id)?n.get(e.id):2**53-1)-(n.has(t.id)?n.get(t.id):2**53-1));for(let e=0;e<s.rules.length;e++)s.rules[e].priority=e;return await i(),t.get()},{authority:3}),r(`filter-pro/toggle`,async e=>{if(await v,!e?.id)return null;let t=s.rules.find(t=>t.id===e.id);return t?(t.enabled=!!e.enabled,await i(),c(t)):null},{authority:3})})}exports.Config=i,exports.apply=C,exports.name=`filter-pro`;
|
package/lib/index.d.ts
CHANGED
package/lib/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Random as e,Schema as t}from"koishi";import{DataService as n}from"@koishijs/plugin-console";import{join as r,resolve as i}from"node:path";import{mkdir as a,readFile as o,writeFile as s}from"node:fs/promises";const c=`filter-pro`,l=t.object({filename:t.string().default(`rules.json`).description(`持久化文件名(位于 data/filterpro/ 下)。`)}),u=Symbol.for(`koishi.loader.record`);var d=class extends n{constructor(e,t){super(e,`filter-pro`),this.state=t}async get(){return h(this.state.rules).map(p)}};function f(){return{type:`group`,operator:`and`,children:[{type:`compare`,field:`guildId`,operator:`eq`,value:``}]}}function p(e){return{id:e.id,name:e.name,enabled:e.enabled,priority:e.priority,action:e.action,target:{...e.target},condition:m(e.condition),response:e.response}}function m(e){return e.type===`group`?{type:`group`,operator:e.operator,children:e.children.map(m)}:e.type===`not`?{type:`not`,child:m(e.child)}:{type:`compare`,field:e.field,operator:e.operator,value:e.value}}function h(e){return[...e].sort((e,t)=>e.priority-t.priority||e.id.localeCompare(t.id))}function g(e){if(!e||typeof e!=`object`)return f();let t=e;if(t.type===`group`)return{type:`group`,operator:t.operator===`or`?`or`:`and`,children:Array.isArray(t.children)?t.children.map(g):[]};if(t.type===`not`)return{type:`not`,child:g(t.child)};let n=[`eq`,`ne`,`includes`,`regex`,`gt`,`gte`,`lt`,`lte`,`exists`].includes(t.operator)?t.operator:`eq`;return{type:`compare`,field:typeof t.field==`string`?t.field:`content`,operator:n,value:t.value}}function _(t){let n=t.target||{type:`global`},r=typeof n.value==`string`?n.value.trim():``,i=n.type===`plugin`?r:``,a={type:n.type===`plugin`?`plugin`:`global`,value:i};return{id:t.id||e.id(8),name:t.name||`new-rule`,enabled:t.enabled??!0,priority:t.priority??0,action:t.action||`block`,target:a,condition:g(t.condition),response:t.response??``}}function v(e,t){if(!e||e.type===`global`)return!0;let n=typeof t.pluginKey==`string`?t.pluginKey:``,r=e.value?.trim();return r?n===r:!1}function y(e){let t=[],n=new Set,r=new Set,i=e=>{let a=e?.scope?.[u];if(!(!a||r.has(a))){r.add(a);for(let[e,r]of Object.entries(a)){let a=String(e).replace(/^~/,``),[o,s=``]=a.split(`:`,2);o&&o!==`group`&&!n.has(a)&&(n.add(a),t.push({key:a,name:o,ident:s,label:s?`${o}:${s}`:o})),i(r?.ctx)}}};return i(e.root),t.sort((e,t)=>e.key.localeCompare(t.key))}function b(e){let t=[],n=new Map;return{rebuild:()=>{t=y(e),n.clear();let r=new Set,i=e=>{let a=e?.scope?.[u];if(!(!a||r.has(a))){r.add(a);for(let[e,r]of Object.entries(a)){let a=String(e).replace(/^~/,``),o=t.find(e=>e.key===a);o&&r?.ctx?.scope&&n.set(r.ctx.scope,o),i(r?.ctx)}}};i(e.root)},list:()=>t,resolveByCommand:e=>{let t=e?.caller?.scope,r=new Set;for(;t&&!r.has(t);){r.add(t);let e=n.get(t);if(e)return e;t=t.parent?.scope}}}}function x(e,t){if(!t)return;let n=t.split(`.`),r=e;for(let e of n){if(!r||typeof r!=`object`)return;r=r[e]}return r}function S(e){if(typeof e==`number`)return Number.isFinite(e)?e:null;if(typeof e==`string`&&e.trim()){let t=Number(e);return Number.isFinite(t)?t:null}return null}function C(e){return e==null||typeof e==`object`?e:String(e)}function w(e,t){if(t.operator===`exists`)return e!=null;let n=t.value,r=C(e),i=C(n);if(t.operator===`eq`)return r===i;if(t.operator===`ne`)return r!==i;if(t.operator===`includes`)return typeof r==`string`?r.includes(String(i??``)):Array.isArray(e)?e.map(e=>C(e)).includes(i):!1;if(t.operator===`regex`){if(typeof r!=`string`)return!1;try{return new RegExp(String(i??``)).test(r)}catch{return!1}}let a=S(e),o=S(n);return a===null||o===null?!1:t.operator===`gt`?a>o:t.operator===`gte`?a>=o:t.operator===`lt`?a<o:t.operator===`lte`?a<=o:!1}function T(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>T(e,t)):e.children.some(e=>T(e,t)):!0:e.type===`not`?!T(e.child,t):w(x(t,e.field),e)}async function E(e){try{let t=await o(e,`utf8`),n=JSON.parse(t);return Array.isArray(n)?n.map(e=>_(e)):[]}catch{return null}}function D(e){let t=Promise.resolve();return async n=>{t=t.catch(()=>{}).then(async()=>{await s(e,JSON.stringify(n.map(p),null,2),`utf8`)}),await t}}function O(e,t={}){let n=r(e.baseDir||process.cwd(),`data`,`filterpro`),o=r(n,t.filename||`rules.json`),s={rules:[]},c=D(o),l=b(e),u=(async()=>{await a(n,{recursive:!0});let e=await E(o);if(e){s.rules=e;return}s.rules=[],await c(s.rules)})().catch(t=>{e.logger(`filter-pro`).warn(`failed to initialize persistent rules: %s`,String(t)),s.rules=[]}),
|
|
1
|
+
import{Random as e,Schema as t}from"koishi";import{DataService as n}from"@koishijs/plugin-console";import{join as r,resolve as i}from"node:path";import{mkdir as a,readFile as o,writeFile as s}from"node:fs/promises";const c=`filter-pro`,l=t.object({filename:t.string().default(`rules.json`).description(`持久化文件名(位于 data/filterpro/ 下)。`),debug:t.boolean().default(!1).description(`输出规则匹配调试日志。`)}),u=Symbol.for(`koishi.loader.record`);var d=class extends n{constructor(e,t){super(e,`filter-pro`),this.state=t}async get(){return h(this.state.rules).map(p)}};function f(){return{type:`group`,operator:`and`,children:[{type:`compare`,field:`guildId`,operator:`eq`,value:``}]}}function p(e){return{id:e.id,name:e.name,enabled:e.enabled,priority:e.priority,action:e.action,target:{...e.target},condition:m(e.condition),response:e.response}}function m(e){return e.type===`group`?{type:`group`,operator:e.operator,children:e.children.map(m)}:e.type===`not`?{type:`not`,child:m(e.child)}:{type:`compare`,field:e.field,operator:e.operator,value:e.value}}function h(e){return[...e].sort((e,t)=>e.priority-t.priority||e.id.localeCompare(t.id))}function g(e){if(!e||typeof e!=`object`)return f();let t=e;if(t.type===`group`)return{type:`group`,operator:t.operator===`or`?`or`:`and`,children:Array.isArray(t.children)?t.children.map(g):[]};if(t.type===`not`)return{type:`not`,child:g(t.child)};let n=[`eq`,`ne`,`includes`,`regex`,`gt`,`gte`,`lt`,`lte`,`exists`].includes(t.operator)?t.operator:`eq`;return{type:`compare`,field:typeof t.field==`string`?t.field:`content`,operator:n,value:t.value}}function _(t){let n=t.target||{type:`global`},r=typeof n.value==`string`?n.value.trim():``,i=n.type===`plugin`?r:``,a={type:n.type===`plugin`?`plugin`:`global`,value:i};return{id:t.id||e.id(8),name:t.name||`new-rule`,enabled:t.enabled??!0,priority:t.priority??0,action:t.action||`block`,target:a,condition:g(t.condition),response:t.response??``}}function v(e,t){if(!e||e.type===`global`)return!0;let n=typeof t.pluginKey==`string`?t.pluginKey:``,r=e.value?.trim();return r?n===r:!1}function y(e){let t=[],n=new Set,r=new Set,i=e=>{let a=e?.scope?.[u];if(!(!a||r.has(a))){r.add(a);for(let[e,r]of Object.entries(a)){let a=String(e).replace(/^~/,``),[o,s=``]=a.split(`:`,2);o&&o!==`group`&&!n.has(a)&&(n.add(a),t.push({key:a,name:o,ident:s,label:s?`${o}:${s}`:o})),i(r?.ctx)}}};return i(e.root),t.sort((e,t)=>e.key.localeCompare(t.key))}function b(e){let t=[],n=new Map;return{rebuild:()=>{t=y(e),n.clear();let r=new Set,i=e=>{let a=e?.scope?.[u];if(!(!a||r.has(a))){r.add(a);for(let[e,r]of Object.entries(a)){let a=String(e).replace(/^~/,``),o=t.find(e=>e.key===a);o&&r?.ctx?.scope&&n.set(r.ctx.scope,o),i(r?.ctx)}}};i(e.root)},list:()=>t,resolveByCommand:e=>{let t=e?.caller?.scope,r=new Set;for(;t&&!r.has(t);){r.add(t);let e=n.get(t);if(e)return e;t=t.parent?.scope}}}}function x(e,t){if(!t)return;let n=t.split(`.`),r=e;for(let e of n){if(!r||typeof r!=`object`)return;r=r[e]}return r}function S(e){if(typeof e==`number`)return Number.isFinite(e)?e:null;if(typeof e==`string`&&e.trim()){let t=Number(e);return Number.isFinite(t)?t:null}return null}function C(e){return e==null||typeof e==`object`?e:String(e)}function w(e,t){if(t.operator===`exists`)return e!=null;let n=t.value,r=C(e),i=C(n);if(t.operator===`eq`)return r===i;if(t.operator===`ne`)return r!==i;if(t.operator===`includes`)return typeof r==`string`?r.includes(String(i??``)):Array.isArray(e)?e.map(e=>C(e)).includes(i):!1;if(t.operator===`regex`){if(typeof r!=`string`)return!1;try{return new RegExp(String(i??``)).test(r)}catch{return!1}}let a=S(e),o=S(n);return a===null||o===null?!1:t.operator===`gt`?a>o:t.operator===`gte`?a>=o:t.operator===`lt`?a<o:t.operator===`lte`?a<=o:!1}function T(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>T(e,t)):e.children.some(e=>T(e,t)):!0:e.type===`not`?!T(e.child,t):w(x(t,e.field),e)}async function E(e){try{let t=await o(e,`utf8`),n=JSON.parse(t);return Array.isArray(n)?n.map(e=>_(e)):[]}catch{return null}}function D(e){let t=Promise.resolve();return async n=>{t=t.catch(()=>{}).then(async()=>{await s(e,JSON.stringify(n.map(p),null,2),`utf8`)}),await t}}function O(e,t={}){let n=r(e.baseDir||process.cwd(),`data`,`filterpro`),o=r(n,t.filename||`rules.json`),s={rules:[]},c=D(o),l=b(e),u=e.logger(`filter-pro`),f=!!t.debug,m=(e,t)=>{f&&u.info(`[trace:%s] %s`,e,JSON.stringify(t))},g=(async()=>{await a(n,{recursive:!0});let e=await E(o);if(e){s.rules=e;return}s.rules=[],await c(s.rules)})().catch(t=>{e.logger(`filter-pro`).warn(`failed to initialize persistent rules: %s`,String(t)),s.rules=[]}),y=async()=>{await g,l.rebuild()};y(),e.on(`ready`,y),e.on(`internal/fork`,y),e.on(`internal/update`,y),e.on(`internal/runtime`,y),e.middleware(async(e,t)=>{await g;let n={platform:String(e.platform??``),selfId:String(e.selfId??``),userId:String(e.userId??``),channelId:String(e.channelId??``),guildId:String(e.guildId??``),isDirect:e.isDirect,content:String(e.content??``),type:String(e.type??``),event:e.event,author:e.author,quote:e.quote};m(`message:incoming`,{platform:n.platform,userId:n.userId,channelId:n.channelId,guildId:n.guildId,isDirect:n.isDirect,content:n.content,ruleCount:s.rules.length});for(let e of h(s.rules)){if(!e.enabled||e.target.type!==`global`)continue;if(!v(e.target,n)){m(`message:skip-target`,{ruleId:e.id,ruleName:e.name,target:e.target,pluginKey:n.pluginKey});continue}let r=T(e.condition,n);if(m(`message:evaluate`,{ruleId:e.id,ruleName:e.name,action:e.action,matched:r,expr:e.condition}),r)return e.action===`bypass`?(m(`message:action`,{ruleId:e.id,action:`bypass`}),t()):e.response?(m(`message:action`,{ruleId:e.id,action:`block`,response:e.response}),e.response):(m(`message:action`,{ruleId:e.id,action:`block`,response:``}),``)}return m(`message:pass`,{reason:`no-rule-matched`}),t()},!0),e.before(`command/execute`,async e=>{await g;let t=l.resolveByCommand(e.command),n={platform:String(e.session.platform??``),selfId:String(e.session.selfId??``),userId:String(e.session.userId??``),channelId:String(e.session.channelId??``),guildId:String(e.session.guildId??``),isDirect:e.session.isDirect,content:String(e.session.content??``),type:String(e.session.type??``),event:e.session.event,author:e.session.author,quote:e.session.quote,commandName:String(e.command.name??``),pluginKey:t?.key,pluginName:t?.name};m(`command:incoming`,{command:n.commandName,pluginKey:n.pluginKey,pluginName:n.pluginName,isDirect:n.isDirect,guildId:n.guildId,content:n.content,ruleCount:s.rules.length});for(let e of h(s.rules)){if(!e.enabled||e.target.type===`global`)continue;if(!v(e.target,n)){m(`command:skip-target`,{ruleId:e.id,ruleName:e.name,target:e.target,pluginKey:n.pluginKey});continue}let t=T(e.condition,n);if(m(`command:evaluate`,{ruleId:e.id,ruleName:e.name,action:e.action,matched:t,expr:e.condition}),t){if(e.action===`bypass`){m(`command:action`,{ruleId:e.id,action:`bypass`});return}return e.response?(m(`command:action`,{ruleId:e.id,action:`block`,response:e.response}),e.response):(m(`command:action`,{ruleId:e.id,action:`block`,response:``}),``)}}m(`command:pass`,{reason:`no-rule-matched`})}),e.inject([`console`],e=>{e.console.addEntry({dev:i(__dirname,`../client/index.ts`),prod:i(__dirname,`../dist`)});let t=new d(e,s),n=e.console.addListener.bind(e.console),r=async()=>{await c(s.rules),t.refresh()};n(`filter-pro/list`,async()=>(await g,t.get()),{authority:3}),n(`filter-pro/targets`,async()=>(await y(),l.list()),{authority:3}),n(`filter-pro/create`,async e=>{await g;let t=_(e||{});return s.rules.push(t),await r(),p(t)},{authority:3}),n(`filter-pro/update`,async e=>{if(await g,!e?.id)return null;let t=s.rules.findIndex(t=>t.id===e.id);if(t<0)return null;let n=s.rules[t];return s.rules[t]=_({...n,...e,id:n.id}),await r(),p(s.rules[t])},{authority:3}),n(`filter-pro/delete`,async e=>{await g;let t=s.rules.findIndex(t=>t.id===e);return t<0?!1:(s.rules.splice(t,1),await r(),!0)},{authority:3}),n(`filter-pro/reorder`,async e=>{if(await g,!Array.isArray(e))return t.get();let n=new Map(e.map((e,t)=>[e,t]));s.rules.sort((e,t)=>(n.has(e.id)?n.get(e.id):2**53-1)-(n.has(t.id)?n.get(t.id):2**53-1));for(let e=0;e<s.rules.length;e++)s.rules[e].priority=e;return await r(),t.get()},{authority:3}),n(`filter-pro/toggle`,async e=>{if(await g,!e?.id)return null;let t=s.rules.find(t=>t.id===e.id);return t?(t.enabled=!!e.enabled,await r(),p(t)):null},{authority:3})})}export{l as Config,O as apply,c as name};
|