strfry-policies/src/policies/keyword-policy.ts
2023-03-30 14:22:51 -05:00

31 lines
636 B
TypeScript

import { Policy } from '../types.ts';
/**
* Reject events containing any of the strings in its content.
*
* @example
* ```ts
* // Reject events with bad words.
* keywordPolicy(msg, ['moo', 'oink', 'honk']);
* ```
*/
const keywordPolicy: Policy<Iterable<string>> = ({ event: { id, content } }, words = []) => {
for (const word of words) {
if (content.toLowerCase().includes(word.toLowerCase())) {
return {
id,
action: 'reject',
msg: 'blocked: contains a banned word or phrase.',
};
}
}
return {
id,
action: 'accept',
msg: '',
};
};
export default keywordPolicy;