koishi-plugin-filter-pro 1.0.5 → 1.0.6

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 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}}}}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,t){if(t.operator===`exists`)return e!=null;let n=t.value;if(t.operator===`eq`)return e===n;if(t.operator===`ne`)return e!==n;if(t.operator===`includes`)return typeof e==`string`?e.includes(String(n??``)):Array.isArray(e)?e.includes(n):!1;if(t.operator===`regex`){if(typeof e!=`string`)return!1;try{return new RegExp(String(n??``)).test(e)}catch{return!1}}let r=_(e),i=_(n);return r===null||i===null?!1:t.operator===`gt`?r>i:t.operator===`gte`?r>=i:t.operator===`lt`?r<i:t.operator===`lte`?r<=i:!1}function y(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>y(e,t)):e.children.some(e=>y(e,t)):!0:e.type===`not`?!y(e.child,t):v(g(t,e.field),e)}async function b(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 x(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 S(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=x(a),d=h(e),m=(async()=>{await(0,r.mkdir)(i,{recursive:!0});let e=await b(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=[]}),g=async()=>{await m,d.rebuild()};g(),e.on(`ready`,g),e.on(`internal/fork`,g),e.on(`internal/update`,g),e.on(`internal/runtime`,g),e.middleware(async(e,t)=>{await m;let n={platform:e.platform,selfId:e.selfId,userId:e.userId,channelId:e.channelId,guildId:e.guildId,isDirect:e.isDirect,content:e.content,subtype:e.subtype,event:e.event,author:e.author,quote:e.quote};for(let e of u(s.rules))if(e.enabled&&e.target.type===`global`&&p(e.target,n)&&y(e.condition,n))return e.action===`bypass`?t():e.response?e.response:``;return t()},!0),e.before(`command/execute`,async e=>{await m;let t=d.resolveByCommand(e.command),n={platform:e.session.platform,selfId:e.session.selfId,userId:e.session.userId,channelId:e.session.channelId,guildId:e.session.guildId,isDirect:e.session.isDirect,content:e.session.content,subtype:e.session.subtype,event:e.session.event,author:e.session.author,quote:e.session.quote,commandName:e.command.name,pluginKey:t?.key,pluginName:t?.name};for(let e of u(s.rules))if(e.enabled&&e.target.type!==`global`&&p(e.target,n)&&y(e.condition,n))return e.action===`bypass`?void 0:e.response?e.response:``}),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 m,t.get()),{authority:3}),r(`filter-pro/targets`,async()=>(await g(),d.list()),{authority:3}),r(`filter-pro/create`,async e=>{await m;let t=f(e||{});return s.rules.push(t),await i(),c(t)},{authority:3}),r(`filter-pro/update`,async e=>{if(await m,!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 m;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 m,!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 m,!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=S,exports.name=`filter-pro`;
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,t){if(t.operator===`exists`)return e!=null;let n=t.value;if(t.operator===`eq`)return e===n;if(t.operator===`ne`)return e!==n;if(t.operator===`includes`)return typeof e==`string`?e.includes(String(n??``)):Array.isArray(e)?e.includes(n):!1;if(t.operator===`regex`){if(typeof e!=`string`)return!1;try{return new RegExp(String(n??``)).test(e)}catch{return!1}}let r=_(e),i=_(n);return r===null||i===null?!1:t.operator===`gt`?r>i:t.operator===`gte`?r>=i:t.operator===`lt`?r<i:t.operator===`lte`?r<=i:!1}function y(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>y(e,t)):e.children.some(e=>y(e,t)):!0:e.type===`not`?!y(e.child,t):v(g(t,e.field),e)}async function b(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 x(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 S(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=x(a),d=h(e),m=(async()=>{await(0,r.mkdir)(i,{recursive:!0});let e=await b(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=[]}),g=async()=>{await m,d.rebuild()};g(),e.on(`ready`,g),e.on(`internal/fork`,g),e.on(`internal/update`,g),e.on(`internal/runtime`,g),e.middleware(async(e,t)=>{await m;let n={platform:e.platform,selfId:e.selfId,userId:e.userId,channelId:e.channelId,guildId:e.guildId,isDirect:e.isDirect,content:e.content,subtype:e.subtype,event:e.event,author:e.author,quote:e.quote};for(let e of u(s.rules))if(e.enabled&&e.target.type===`global`&&p(e.target,n)&&y(e.condition,n))return e.action===`bypass`?t():e.response?e.response:``;return t()},!0),e.before(`command/execute`,async e=>{await m;let t=d.resolveByCommand(e.command),n={platform:e.session.platform,selfId:e.session.selfId,userId:e.session.userId,channelId:e.session.channelId,guildId:e.session.guildId,isDirect:e.session.isDirect,content:e.session.content,subtype:e.session.subtype,event:e.session.event,author:e.session.author,quote:e.session.quote,commandName:e.command.name,pluginKey:t?.key,pluginName:t?.name};for(let e of u(s.rules))if(e.enabled&&e.target.type!==`global`&&p(e.target,n)&&y(e.condition,n))return e.action===`bypass`?void 0:e.response?e.response:``}),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 m,t.get()),{authority:3}),r(`filter-pro/targets`,async()=>(await g(),d.list()),{authority:3}),r(`filter-pro/create`,async e=>{await m;let t=f(e||{});return s.rules.push(t),await i(),c(t)},{authority:3}),r(`filter-pro/update`,async e=>{if(await m,!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 m;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 m,!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 m,!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=S,exports.name=`filter-pro`;
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}}}}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,t){if(t.operator===`exists`)return e!=null;let n=t.value;if(t.operator===`eq`)return e===n;if(t.operator===`ne`)return e!==n;if(t.operator===`includes`)return typeof e==`string`?e.includes(String(n??``)):Array.isArray(e)?e.includes(n):!1;if(t.operator===`regex`){if(typeof e!=`string`)return!1;try{return new RegExp(String(n??``)).test(e)}catch{return!1}}let r=S(e),i=S(n);return r===null||i===null?!1:t.operator===`gt`?r>i:t.operator===`gte`?r>=i:t.operator===`lt`?r<i:t.operator===`lte`?r<=i:!1}function w(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>w(e,t)):e.children.some(e=>w(e,t)):!0:e.type===`not`?!w(e.child,t):C(x(t,e.field),e)}async function T(e){try{let t=await o(e,`utf8`),n=JSON.parse(t);return Array.isArray(n)?n.map(e=>_(e)):[]}catch{return null}}function E(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 D(e,t={}){let n=r(e.baseDir||process.cwd(),`data`,`filterpro`),o=r(n,t.filename||`rules.json`),s={rules:[]},c=E(o),l=b(e),u=(async()=>{await a(n,{recursive:!0});let e=await T(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=[]}),f=async()=>{await u,l.rebuild()};f(),e.on(`ready`,f),e.on(`internal/fork`,f),e.on(`internal/update`,f),e.on(`internal/runtime`,f),e.middleware(async(e,t)=>{await u;let n={platform:e.platform,selfId:e.selfId,userId:e.userId,channelId:e.channelId,guildId:e.guildId,isDirect:e.isDirect,content:e.content,subtype:e.subtype,event:e.event,author:e.author,quote:e.quote};for(let e of h(s.rules))if(e.enabled&&e.target.type===`global`&&v(e.target,n)&&w(e.condition,n))return e.action===`bypass`?t():e.response?e.response:``;return t()},!0),e.before(`command/execute`,async e=>{await u;let t=l.resolveByCommand(e.command),n={platform:e.session.platform,selfId:e.session.selfId,userId:e.session.userId,channelId:e.session.channelId,guildId:e.session.guildId,isDirect:e.session.isDirect,content:e.session.content,subtype:e.session.subtype,event:e.session.event,author:e.session.author,quote:e.session.quote,commandName:e.command.name,pluginKey:t?.key,pluginName:t?.name};for(let e of h(s.rules))if(e.enabled&&e.target.type!==`global`&&v(e.target,n)&&w(e.condition,n))return e.action===`bypass`?void 0:e.response?e.response:``}),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 u,t.get()),{authority:3}),n(`filter-pro/targets`,async()=>(await f(),l.list()),{authority:3}),n(`filter-pro/create`,async e=>{await u;let t=_(e||{});return s.rules.push(t),await r(),p(t)},{authority:3}),n(`filter-pro/update`,async e=>{if(await u,!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 u;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 u,!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 u,!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,D as apply,c as name};
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,t){if(t.operator===`exists`)return e!=null;let n=t.value;if(t.operator===`eq`)return e===n;if(t.operator===`ne`)return e!==n;if(t.operator===`includes`)return typeof e==`string`?e.includes(String(n??``)):Array.isArray(e)?e.includes(n):!1;if(t.operator===`regex`){if(typeof e!=`string`)return!1;try{return new RegExp(String(n??``)).test(e)}catch{return!1}}let r=S(e),i=S(n);return r===null||i===null?!1:t.operator===`gt`?r>i:t.operator===`gte`?r>=i:t.operator===`lt`?r<i:t.operator===`lte`?r<=i:!1}function w(e,t){return e.type===`group`?e.children.length?e.operator===`and`?e.children.every(e=>w(e,t)):e.children.some(e=>w(e,t)):!0:e.type===`not`?!w(e.child,t):C(x(t,e.field),e)}async function T(e){try{let t=await o(e,`utf8`),n=JSON.parse(t);return Array.isArray(n)?n.map(e=>_(e)):[]}catch{return null}}function E(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 D(e,t={}){let n=r(e.baseDir||process.cwd(),`data`,`filterpro`),o=r(n,t.filename||`rules.json`),s={rules:[]},c=E(o),l=b(e),u=(async()=>{await a(n,{recursive:!0});let e=await T(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=[]}),f=async()=>{await u,l.rebuild()};f(),e.on(`ready`,f),e.on(`internal/fork`,f),e.on(`internal/update`,f),e.on(`internal/runtime`,f),e.middleware(async(e,t)=>{await u;let n={platform:e.platform,selfId:e.selfId,userId:e.userId,channelId:e.channelId,guildId:e.guildId,isDirect:e.isDirect,content:e.content,subtype:e.subtype,event:e.event,author:e.author,quote:e.quote};for(let e of h(s.rules))if(e.enabled&&e.target.type===`global`&&v(e.target,n)&&w(e.condition,n))return e.action===`bypass`?t():e.response?e.response:``;return t()},!0),e.before(`command/execute`,async e=>{await u;let t=l.resolveByCommand(e.command),n={platform:e.session.platform,selfId:e.session.selfId,userId:e.session.userId,channelId:e.session.channelId,guildId:e.session.guildId,isDirect:e.session.isDirect,content:e.session.content,subtype:e.session.subtype,event:e.session.event,author:e.session.author,quote:e.session.quote,commandName:e.command.name,pluginKey:t?.key,pluginName:t?.name};for(let e of h(s.rules))if(e.enabled&&e.target.type!==`global`&&v(e.target,n)&&w(e.condition,n))return e.action===`bypass`?void 0:e.response?e.response:``}),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 u,t.get()),{authority:3}),n(`filter-pro/targets`,async()=>(await f(),l.list()),{authority:3}),n(`filter-pro/create`,async e=>{await u;let t=_(e||{});return s.rules.push(t),await r(),p(t)},{authority:3}),n(`filter-pro/update`,async e=>{if(await u,!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 u;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 u,!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 u,!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,D as apply,c as name};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-filter-pro",
3
3
  "description": "A powerful filter plugin for Koishi",
4
- "version": "1.0.5",
4
+ "version": "1.0.6",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "types": "lib/index.d.ts",