@q78kg/koishi-plugin-text-censor 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.d.ts +9 -0
- package/lib/index.js +88 -0
- package/package.json +50 -0
- package/readme.md +9 -0
package/lib/index.d.ts
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
2
|
+
export declare const name = "text-censor";
|
3
|
+
export interface Config {
|
4
|
+
filename: string;
|
5
|
+
removeWords: boolean;
|
6
|
+
transformToUpper: boolean;
|
7
|
+
}
|
8
|
+
export declare const Config: Schema<Config>;
|
9
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
var __create = Object.create;
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name2 in all)
|
10
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// src/index.ts
|
31
|
+
var src_exports = {};
|
32
|
+
__export(src_exports, {
|
33
|
+
Config: () => Config,
|
34
|
+
apply: () => apply,
|
35
|
+
name: () => name
|
36
|
+
});
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
38
|
+
var import_koishi = require("koishi");
|
39
|
+
var import_node_fs = require("node:fs");
|
40
|
+
var import_node_path = require("node:path");
|
41
|
+
var import_censor = __toESM(require("@koishijs/censor"));
|
42
|
+
var import_mint_filter = __toESM(require("mint-filter"));
|
43
|
+
var name = "text-censor";
|
44
|
+
var Config = import_koishi.Schema.object({
|
45
|
+
filename: import_koishi.Schema.string().description("存储敏感词的文件路径。").default("data/censor.txt"),
|
46
|
+
removeWords: import_koishi.Schema.boolean().description("是否直接删除敏感词。").default(false),
|
47
|
+
// 默认不删除敏感词
|
48
|
+
transformToUpper: import_koishi.Schema.boolean().description("是否将字符转换为大写。").default(false)
|
49
|
+
// 默认不转换字符为大写
|
50
|
+
});
|
51
|
+
function apply(ctx, config) {
|
52
|
+
const filename = (0, import_node_path.resolve)(ctx.baseDir, config.filename);
|
53
|
+
if (!(0, import_node_fs.existsSync)(filename)) {
|
54
|
+
ctx.logger.warn("dictionary file not found");
|
55
|
+
return;
|
56
|
+
}
|
57
|
+
const source = (0, import_node_fs.readFileSync)(filename, "utf8");
|
58
|
+
const words = source.split("\n").map((word) => word.trim()).filter((word) => word && !word.startsWith("//") && !word.startsWith("#"));
|
59
|
+
const mintOptions = {
|
60
|
+
transform: config.transformToUpper ? "capital" : "none"
|
61
|
+
// 这里我们将值设置为可能的类型
|
62
|
+
};
|
63
|
+
const filter = new import_mint_filter.default(words, mintOptions);
|
64
|
+
ctx.plugin(import_censor.default);
|
65
|
+
ctx.get("censor").intercept({
|
66
|
+
async text(attrs) {
|
67
|
+
const result = await filter.filter(attrs.content);
|
68
|
+
if (typeof result.text !== "string")
|
69
|
+
return [];
|
70
|
+
if (config.removeWords) {
|
71
|
+
const filteredText = words.reduce((text, word) => {
|
72
|
+
const regex = new RegExp(`\\b${word}\\b`, "gi");
|
73
|
+
return text.replace(regex, "");
|
74
|
+
}, result.text);
|
75
|
+
return [filteredText.trim()];
|
76
|
+
} else {
|
77
|
+
return [result.text];
|
78
|
+
}
|
79
|
+
}
|
80
|
+
});
|
81
|
+
}
|
82
|
+
__name(apply, "apply");
|
83
|
+
// Annotate the CommonJS export names for ESM import in node:
|
84
|
+
0 && (module.exports = {
|
85
|
+
Config,
|
86
|
+
apply,
|
87
|
+
name
|
88
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
{
|
2
|
+
"name": "@q78kg/koishi-plugin-text-censor",
|
3
|
+
"description": "A text censor provider for Koishi",
|
4
|
+
"version": "1.0.2",
|
5
|
+
"main": "lib/index.js",
|
6
|
+
"typings": "lib/index.d.ts",
|
7
|
+
"files": [
|
8
|
+
"lib"
|
9
|
+
],
|
10
|
+
"license": "MIT",
|
11
|
+
"repository": {
|
12
|
+
"type": "git",
|
13
|
+
"url": "git+https://github.com/Hoshino-Yumetsuki/koishi-plugin-text-censor.git"
|
14
|
+
},
|
15
|
+
"bugs": {
|
16
|
+
"url": "https://github.com/Hoshino-Yumetsuki/koishi-plugin-text-censor/issues"
|
17
|
+
},
|
18
|
+
"homepage": "https://censor.koishi.chat/plugins/text.html",
|
19
|
+
"keywords": [
|
20
|
+
"bot",
|
21
|
+
"chatbot",
|
22
|
+
"koishi",
|
23
|
+
"plugin",
|
24
|
+
"censor",
|
25
|
+
"filter",
|
26
|
+
"security",
|
27
|
+
"text"
|
28
|
+
],
|
29
|
+
"koishi": {
|
30
|
+
"service": {
|
31
|
+
"implements": [
|
32
|
+
"censor"
|
33
|
+
]
|
34
|
+
},
|
35
|
+
"description": {
|
36
|
+
"en": "Filter out sensitive words in text messages",
|
37
|
+
"zh": "文本敏感词过滤"
|
38
|
+
}
|
39
|
+
},
|
40
|
+
"peerDependencies": {
|
41
|
+
"koishi": "^4.17.9"
|
42
|
+
},
|
43
|
+
"devDependencies": {
|
44
|
+
"koishi": "^4.17.9"
|
45
|
+
},
|
46
|
+
"dependencies": {
|
47
|
+
"@koishijs/censor": "^1.0.1",
|
48
|
+
"mint-filter": "^3.0.1"
|
49
|
+
}
|
50
|
+
}
|
package/readme.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# @q78kg/koishi-plugin-text-censor
|
2
|
+
|
3
|
+
[![npm](https://img.shields.io/npm/v/@q78kg/koishi-plugin-text-censor?style=flat-square)](https://www.npmjs.com/package/@q78kg/koishi-plugin-text-censor)
|
4
|
+
|
5
|
+
此插件基于 Aho–Corasick 算法,对输入的文本内容进行过滤,并将所有的敏感词替换为 `*`。
|
6
|
+
|
7
|
+
## 文档
|
8
|
+
|
9
|
+
<https://censor.koishi.chat/plugins/text.html>
|