diff --git a/.gitignore b/.gitignore index c03c7d15..88bac91f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ config.py __pycache__/ database.db qchatgpt.log -config.py \ No newline at end of file +config.py +banlist.py \ No newline at end of file diff --git a/banlist-template.py b/banlist-template.py new file mode 100644 index 00000000..27f9aed5 --- /dev/null +++ b/banlist-template.py @@ -0,0 +1,11 @@ +# 禁用列表 +# person为个人,其中的QQ号会被禁止与机器人进行私聊或群聊交互 +# 示例: person = [2854196310, 1234567890, 9876543210] +# group为群组,其中的群号会被禁止与机器人进行交互 +# 示例: group = [123456789, 987654321, 1234567890] + +# 是否启用禁用列表 +enable = True + +person = [2854196310] # 2854196310是Q群管家机器人的QQ号,默认屏蔽以免出现循环 +group = [204785790] # 204785790是本项目交流群的群号,默认屏蔽,避免在交流群测试机器人 diff --git a/main.py b/main.py index 5a9866ed..61b94fe6 100644 --- a/main.py +++ b/main.py @@ -221,6 +221,10 @@ if __name__ == '__main__': print('请先在config.py中填写配置') sys.exit(0) + # 检查是否有banlist.py,如果没有就把banlist-template.py复制一份 + if not os.path.exists('banlist.py'): + shutil.copy('banlist-template.py', 'banlist.py') + if len(sys.argv) > 1 and sys.argv[1] == 'init_db': init_db() sys.exit(0) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 9e7de955..261558ef 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -55,11 +55,24 @@ class QQBotManager: reply_filter = None + enable_banlist = False + + ban_person = [] + ban_group = [] + def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, first_time_init=True): self.timeout = timeout self.retry = retry + # 加载禁用列表 + if os.path.exists("banlist.py"): + import banlist + self.enable_banlist = banlist.enable + self.ban_person = banlist.person + self.ban_group = banlist.group + logging.info("加载禁用列表: person: {}, group: {}".format(self.ban_person, self.ban_group)) + config = pkg.utils.context.get_config() if os.path.exists("sensitive.json") \ and config.sensitive_word_filter is not None \ diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index aee1d4f6..40ea5db3 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -100,12 +100,21 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes reply = [] session_name = "{}_{}".format(launcher_type, launcher_id) - # 检查是否被禁言 - if launcher_type == 'group': - result = mgr.bot.member_info(target=launcher_id, member_id=mgr.bot.qq).get() - result = asyncio.run(result) - if result.mute_time_remaining > 0: - logging.info("机器人被禁言,跳过消息处理(group_{},剩余{}s)".format(launcher_id, + # 检查发送方是否被禁用 + if pkg.utils.context.get_qqbot_manager().enable_banlist: + if sender_id in pkg.utils.context.get_qqbot_manager().ban_person: + logging.info("根据禁用列表忽略用户{}的消息".format(sender_id)) + return [] + if launcher_type == 'group' and launcher_id in pkg.utils.context.get_qqbot_manager().ban_group: + logging.info("根据禁用列表忽略群{}的消息".format(launcher_id)) + return [] + + # 检查是否被禁言 + if launcher_type == 'group': + result = mgr.bot.member_info(target=launcher_id, member_id=mgr.bot.qq).get() + result = asyncio.run(result) + if result.mute_time_remaining > 0: + logging.info("机器人被禁言,跳过消息处理(group_{},剩余{}s)".format(launcher_id, result.mute_time_remaining)) return reply @@ -229,11 +238,12 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes api_keys = pkg.utils.context.get_openai_manager().key_mgr.api_key for key_name in api_keys: - text_length = pkg.utils.context.get_openai_manager().audit_mgr\ + text_length = pkg.utils.context.get_openai_manager().audit_mgr \ .get_text_length_of_key(api_keys[key_name]) - image_count = pkg.utils.context.get_openai_manager().audit_mgr\ + image_count = pkg.utils.context.get_openai_manager().audit_mgr \ .get_image_count_of_key(api_keys[key_name]) - reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), int(image_count)) + reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), + int(image_count)) reply = [reply_str] elif cmd == 'draw': @@ -286,7 +296,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes params = [config_item] + params reply = config_operation("cfg", params) else: - reply = ["[bot]err:未知的指令或权限不足: "+cmd] + reply = ["[bot]err:未知的指令或权限不足: " + cmd] except Exception as e: mgr.notify_admin("{}指令执行失败:{}".format(session_name, e)) logging.exception(e) diff --git a/pkg/utils/reloader.py b/pkg/utils/reloader.py index fdc919e2..639c03da 100644 --- a/pkg/utils/reloader.py +++ b/pkg/utils/reloader.py @@ -31,6 +31,7 @@ def reload_all(notify=True): walk(pkg) importlib.reload(__import__('config')) importlib.reload(__import__('main')) + importlib.reload(__import__('banlist')) pkg.utils.context.context = context # 执行启动流程