Skip to content

Commit

Permalink
fix: 事件过滤器错误修复
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-cn committed Oct 16, 2023
1 parent b6aee20 commit e4d895c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && cp -r src/config.sample.yaml lib/config.sample.yaml",
"dev": "ts-node-dev -r tsconfig-paths/register ./src/bin.ts -c config.yaml",
"pub": "npm publish --access public",
"test": "ts-node-dev -r tsconfig-paths/register ./src/test.ts",
"docs:dev": "vitepress dev docs --port 8989",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"test": "echo \"Error: no test specified\" && exit 1"
"docs:preview": "vitepress preview docs"
},
"repository": {
"type": "git",
Expand Down
37 changes: 26 additions & 11 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {Dict} from "@zhinjs/shared";
export interface Service<V extends OneBot.Version>{
filterFn(event:Dict):boolean
}
export abstract class Service<V extends OneBot.Version> extends EventEmitter{
export class Service<V extends OneBot.Version> extends EventEmitter{

protected constructor(public config:OneBot.Config<V>) {
constructor(public config:OneBot.Config<V>) {
super();
this.filterFn=Service.createFilterFunction(config.filters||{})
}
Expand All @@ -15,7 +15,7 @@ export namespace Service{

type MaybeArray<T = any> = T | T[];
type AttrFilter = {
[P in keyof Dict]?: MaybeArray<P> | boolean;
[P in keyof Dict]?: MaybeArray | boolean
};
export type Filters = AttrFilter | WithFilter | UnionFilter | ExcludeFilter;
export type WithFilter = {
Expand All @@ -29,6 +29,21 @@ export namespace Service{
};

export function createFilterFunction(filters: Filters) {
const isLogicKey=(key: string) => {
return [
'$and',
'$or',
'$not',
'$nor',
'$regexp',
'$like',
'$gt',
'$gte',
'$lt',
'$lte',
'$between',
].includes(key)
}
const filterFn = (
event: Dict,
key: string,
Expand All @@ -54,25 +69,25 @@ export namespace Service{
return value;
}
if (typeof value !== "object") {
if(key==='$regex' && typeof value==='string') return new RegExp(value).test(event[key]);
if(key==='$like' && typeof value==='string') return event[key].includes(value);
if(key==='$gt' && typeof value==='number') return event[key]>value;
if(key==='$gte' && typeof value==='number') return event[key]>=value;
if(key==='$lt' && typeof value==='number') return event[key]<value;
if(key==='$lte' && typeof value==='number') return event[key]<=value;
if(key==='$regex' && typeof value==='string') return new RegExp(value).test(String(event));
if(key==='$like' && typeof value==='string') return String(event).includes(value);
if(key==='$gt' && typeof value==='number') return Number(event)>value;
if(key==='$gte' && typeof value==='number') return Number(event)>=value;
if(key==='$lt' && typeof value==='number') return Number(event)<value;
if(key==='$lte' && typeof value==='number') return Number(event)<=value;
return value === event[key];
}
if(key==='$between' &&
Array.isArray(value) &&
value.length === 2 &&
value.every((item) => typeof item === 'number')){
const [start,end]=value;
return event[key]>=start && event[key]<=end;
return event>=start && event<=end;
}
if(Array.isArray(value)){
return value.includes(event[key]);
}
return createFilterFunction(value)(event);
return createFilterFunction(value)(isLogicKey(key)?event:event[key]);
};
return (event:Dict)=>{
return Object.entries(filters).every(([key, value]) =>
Expand Down

0 comments on commit e4d895c

Please sign in to comment.