如何为您的 OpenClaw 添加脏话过滤 | Moltbot Clawdbot Agent
Source: Dev.to
目录
- 问题:基于 AI 的脏话检查成本高昂
- 为什么选择
openclaw‑profanity? - 集成选项
- 选项 1:Profanity Guard Hook
- 选项 2:自定义技能
- 选项 3:直接集成
- 平台特定示例
- 高级:混合 AI + 本地过滤
- 配置选项
- 快速开始
- 常见问题
问题:基于 AI 的脏话检测成本高
| 每日消息数 | 每月成本(仅 AI) | 使用 openclaw‑profanity |
|---|---|---|
| 500 | $5 – $15 | $0 |
| 1,000 | $10 – $30 | $0 |
| 5,000 | $50 – $150 | $0 |
| 10,000 | $100 – $300 | $0 |
这仅仅是脏话检测的费用——在你的代理执行任何有用操作之前。
仅 AI 审核的其他问题
| 问题 | 仅 AI | openclaw‑profanity |
|---|---|---|
| 延迟 | 每次检查 200 – 500 毫秒 | — |
agent.useHook(
profanityGuardHook({
action: "censor",
onViolation: (msg, result) => {
console.log(`Filtered: ${result.profaneWords.join(", ")}`);
}
})
);
agent.start();
集成选项
选项 1:Profanity Guard Hook
添加一个钩子来过滤每条传入的消息。
选项 2:Custom Skill
创建一个调用 checkProfanity 并决定如何处理结果的技能。
选项 3:Direct Integration
在消息处理管道中直接使用 Filter 类。
平台特定示例
Telegram、Discord、Slack
相同的钩子可以使用;只需在 OpenClawAgent 构造函数中更换适配器(TelegramAdapter、DiscordAdapter、SlackAdapter)。
高级:混合 AI + 本地过滤
您可以将廉价的本地过滤器与高精度的 AI 模型相结合,以处理边缘情况:
import { checkProfanity, censorText } from "openclaw-profanity";
async function handleIncomingMessage(message, context) {
// 1️⃣ Fast local check
const result = await checkProfanity(message.text);
if (result.isProfane && result.confidence > 0.9) {
// High confidence → block immediately
return context.reply("Please keep the conversation respectful.");
}
// Medium confidence → run a more expensive AI check (optional)
const aiResult = await aiProfanityCheck(message.text); // your AI call
if (aiResult.isProfane) {
return context.reply("Please keep the conversation respectful.");
}
// Otherwise, censor and continue
const censored = censorText(message.text);
message.text = censored.processedText;
// 3️⃣ Forward to the main LLM
return context.next(message);
}
配置选项
| Option | Type | Default | Description |
|---|---|---|---|
action | "censor" | "block" | "censor" | 检测到脏话时的处理方式 |
languages | string[] | ["en"] | 要检查的 ISO‑639‑1 语言代码 |
replacement | string | "***" | 用于替换脏话的文本(当 action: "censor" 时) |
detectLeetspeak | boolean | true | 启用 1337 语言检测 |
detectUnicode | boolean | true | 启用 Unicode 替换检测 |
onViolation | (msg, result) => void | null | 在每次违规时调用的回调函数 |
onError | (err) => void | null | 处理意外错误的回调函数 |
所有选项都是可选的;该钩子开箱即用,拥有合理的默认值。
快速开始
# 1️⃣ Install the package
npm i openclaw-profanity
// 2️⃣ Add the hook (3 lines of code)
import { OpenClawAgent } from "openclaw";
import { profanityGuardHook } from "openclaw-profanity/hooks";
const agent = new OpenClawAgent({ /* your config */ });
agent.useHook(profanityGuardHook({ action: "censor", languages: ["en"] }));
agent.start();
就这样 — 你现在拥有 完整的脏话保护,零延迟,且 无需经常性费用。
FAQ
Q: 我的机器人已经在使用 GPT/Claude/Gemini。为什么不让大模型来检查脏话?
A: 每次检查都会产生 API 延迟和费用。本地过滤器可以即时且免费地处理绝大多数情况。
Q: 我可以阻止消息而不是对其进行审查吗?
A: 可以。在 hook 或 skill 配置中将 action: "block" 设置即可。
Q: 我该如何为新语言添加支持?
A: 向 glin-profanity 仓库提交语言文件或打开 issue;该库设计为社区可扩展的形式。
Q: 过滤器能处理表情符号或图片吗?
A: 核心库仅处理文本。对于媒体,需要先进行 OCR 识别,然后将提取出的文本传递给 checkProfanity。
Q: 这个库适合生产环境使用吗?
A: 已在多个高并发机器人中经受实战考验,提供确定性的结果且没有外部依赖。
📦 安装
npm install openclaw-profanity
🔧 快速设置 – 添加 Profanity Guard Hook
import { profanityGuardHook } from "openclaw-profanity/hooks";
agent.useHook(
profanityGuardHook({
action: "censor" // or "block"
})
);
每条传入的消息现在将自动过滤。
📱 平台特定示例
Telegram
import { OpenClawAgent } from "openclaw";
import { TelegramAdapter } from "openclaw/adapters/telegram";
import { profanityGuardHook } from "openclaw-profanity/hooks";
const agent = new OpenClawAgent({
adapter: new TelegramAdapter({
token: process.env.TELEGRAM_BOT_TOKEN
})
});
agent.useHook(
profanityGuardHook({
action: "block",
warningMessage: "Please keep the chat friendly!"
})
);
Discord
import { OpenClawAgent } from "openclaw";
import { DiscordAdapter } from "openclaw/adapters/discord";
import { profanityGuardHook } from "openclaw-profanity/hooks";
const agent = new OpenClawAgent({
adapter: new DiscordAdapter({
token: process.env.DISCORD_BOT_TOKEN
})
});
agent.useHook(
profanityGuardHook({
action: "censor",
// Discord‑specific: delete original and repost censored
onViolation: async (msg, result, context) => {
await msg.delete();
await context.reply(`${msg.author}: ${result.censoredText}`);
}
})
);
Slack
import { OpenClawAgent } from "openclaw";
import { SlackAdapter } from "openclaw/adapters/slack";
import { profanityGuardHook } from "openclaw-profanity/hooks";
const agent = new OpenClawAgent({
adapter: new SlackAdapter({
token: process.env.SLACK_BOT_TOKEN
})
});
agent.useHook(
profanityGuardHook({
action: "censor",
onViolation: async (msg, result) => {
// Notify workspace admins
await notifyAdmins(msg.channel, result);
}
})
);
⚡ 混合审查 – 需要时快速本地 + AI
import { checkProfanity } from "openclaw-profanity";
async function smartModeration(message, agent) {
// 1️⃣ Fast local check
const local = await checkProfanity(message.text);
if (local.isProfane && local.confidence > 0.95) {
return { action: "block" };
}
// 2️⃣ Uncertain → ask the LLM for context (rare)
const aiAnalysis = await agent.analyze({
prompt: `Is this message inappropriate? Consider context and intent: "${message.text}"`,
format: "json"
});
return aiAnalysis.inappropriate ? { action: "block" } : { action: "allow" };
}
结果
| 指标 | 数值 |
|---|---|
| 即时决策(本地) | 95 % of messages |
| 升级至 AI | 5 % (only ambiguous cases) |
| 相较于仅使用 AI 的成本降低 | 90 %+ |
| 本地平均延迟 | sub‑ms |
🛠️ 高级过滤器配置
import { Filter } from "openclaw-profanity";
const filter = new Filter({
// Languages (default = all 24)
languages: ["en", "es", "fr"],
// Evasion detection
detectLeetspeak: true, // f4ck, sh1t
detectUnicode: true, // Cyrillic/Greek substitutions
detectSpacing: true, // f u c k
// Censorship options
replaceWith: "*", // character used for replacement
preserveLength: true, // "****" vs "***"
// Whitelist (words to ignore)
whitelist: ["assistant", "class"],
// Custom profanity list
customWords: ["badword1", "badword2"]
});
❓ 常见问题
| 问题 | 答案 |
|---|---|
| 库的速度足够用于实时机器人吗? | 是的 – 响应时间在毫秒以下(比远程 API 快 200‑500 倍)。 |
| “Scunthorpe”会被误判吗? | 不会。引擎使用智能词边界检测并内置白名单。 |
| 我可以添加自己的词汇吗? | 当然可以。通过 customWords 添加,它们会继承所有规避检测(leet 语、Unicode、间隔等)。 |
| 这和旧的 Moltbot/Clawdbot 库是同一个吗? | 是的。OpenClaw 是新名称,集成方式完全相同。 |
| 核心引擎是什么? | glin-profanity – 经过实战检验的多语言脏话检测引擎。 |
| 它免费吗? | 100 % 开源,采用 MIT 许可证。没有 API 费用。 |
| 支持哪些平台? | Telegram、Discord、Slack、WhatsApp,以及任何兼容 OpenClaw 的渠道。 |
| 覆盖了多少种语言? | 开箱即用 24 种语言,完整的 Unicode 支持。 |
| 可以在其他框架中使用吗? | 核心 Filter 类可以直接在任何 Node.js 项目中调用。 |
🌟 关键优势
- 零成本 – 完全本地运行,无外部 API 费用。
- 亚毫秒延迟 – 适用于高吞吐量机器人。
- 支持 24 种语言的多语言。
- 防规避 – 支持俚语、Unicode 技巧、间隔字符等。
- 原生 OpenClaw 集成 – 简单的钩子、技能或直接 API 使用。
- MIT 许可证 – 免费使用、修改和再分发。
📚 资源
| 资源 | 链接 |
|---|---|
| npm 包 | npm install openclaw-profanity |
| GitHub 仓库 | |
| 文档 / 集成指南 | |
| 在线演示 | |
| 相关项目 | - glin-profanity-mcp (Claude Desktop、Cursor、Windsurf 的 MCP 服务器) - glin-profanity (Flask/Django 的 Python 版) |
🙋♀️ 联系我们
- 有问题吗? 在下方留言或在 GitHub 上打开 issue。
- 使用场景? 告诉我们您正在为哪个消息平台构建——我们喜欢了解真实的部署案例!
您的机器人值得拥有比每月支付 $100+/month 更好的脏话检测方式。只需添加一行代码,永久解决它。