diff --git a/config-template.py b/config-template.py index d801a371..3d5ab070 100644 --- a/config-template.py +++ b/config-template.py @@ -24,6 +24,11 @@ openai_config = { # 管理员QQ号,用于接收报错等通知,为0时不发送通知 admin_qq = 0 +# 敏感词过滤开关,以同样数量的*代替敏感词回复 +# 开启后可能会降低机器人的回复速度 +# 请在sensitive.json中添加敏感词 +sensitive_word_filter = True + # OpenAI的completion API的参数 # 不了解的话请不要修改,具体请查看OpenAI的文档 completion_api_params = { diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py new file mode 100644 index 00000000..2f622e74 --- /dev/null +++ b/pkg/qqbot/filter.py @@ -0,0 +1,18 @@ +import re + + +class ReplyFilter: + + sensitive_words = [] + + def __init__(self, sensitive_words: list): + self.sensitive_words = sensitive_words + + def process(self, message: str) -> str: + for word in self.sensitive_words: + match = re.findall(word, message) + if len(match) > 0: + for i in range(len(match)): + message = message.replace(match[i], "*" * len(match[i])) + + return message diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 3b1cb32f..3318ed10 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -1,4 +1,6 @@ import asyncio +import json +import os import threading import openai.error @@ -10,6 +12,8 @@ from func_timeout import func_set_timeout, FunctionTimedOut import datetime import logging +import pkg.qqbot.filter + help_text = config.help_message inst = None @@ -24,11 +28,21 @@ class QQBotManager: bot = None + reply_filter = None + def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3): self.timeout = timeout self.retry = retry + if os.path.exists("sensitive.json") \ + and config.sensitive_word_filter is not None \ + and config.sensitive_word_filter: + with open("sensitive.json", "r", encoding="utf-8") as f: + self.reply_filter = pkg.qqbot.filter.ReplyFilter(json.load(f)['words']) + else: + self.reply_filter = pkg.qqbot.filter.ReplyFilter([]) + bot = Mirai( qq=mirai_http_api_config['qq'], adapter=WebSocketAdapter( @@ -157,7 +171,7 @@ class QQBotManager: logging.info( "回复[{}]消息:{}".format(session_name, reply[:min(100, len(reply))] + ("..." if len(reply) > 100 else ""))) - + reply = self.reply_filter.process(reply) return reply # 私聊消息处理 diff --git a/sensitive.json b/sensitive.json new file mode 100644 index 00000000..435c9fac --- /dev/null +++ b/sensitive.json @@ -0,0 +1,70 @@ +{ + "words": [ + "习近平", + "胡锦涛", + "江泽民", + "温家宝", + "李克强", + "李长春", + "毛泽东", + "邓小平", + "周恩来", + "社会主义", + "共产党", + "共产主义", + "大陆官方", + "北京政权", + "中华帝国", + "中国政府", + "共狗", + "六四事件", + "天安门", + "六四", + "政治局常委", + "学潮", + "八九", + "二十大", + "民进党", + "台独", + "台湾独立", + "台湾国", + "国民党", + "台湾民国", + "中华民国", + "pornhub", + "Pornhub", + "youporn", + "porn", + "Porn", + "xvideos", + "Xvideos", + "[Rr]ed[Tt]ube", + "[Xx][Hh]amster", + "[Ss]pank[Ww]ire", + "[Tt]ube8", + "[Yy]ou[Jj]izz", + "[Bb]razzers", + "[Nn]aughty[ ]?[Aa]merica", + "作爱", + "做爱", + "性交", + "自慰", + "阴茎", + "淫妇", + "肛交", + "交配", + "性关系", + "性活动", + "色图", + "涩图", + "裸体", + "小穴", + "淫荡", + "性爱", + "翻墙", + "VPN", + "科学上网", + "挂梯子", + "GFW" + ] +} \ No newline at end of file