From 1112ff432b8df03578376d32deb1a0cbdd9e2110 Mon Sep 17 00:00:00 2001 From: Cindy-Master <2606440373@qq.com> Date: Thu, 13 Mar 2025 02:49:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E7=94=A8=E6=88=B7=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=9C=A8=E5=85=B7=E4=BD=93=E6=97=B6=E9=97=B4=E6=AE=B5?= =?UTF-8?q?=E5=86=85=E9=80=90=E6=AD=A5=E9=99=8D=E4=BD=8E=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E6=84=8F=E6=84=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 模拟人类的睡眠/工作等情况 (bot太能bb了,又因为日程表的原因导致他会一直喊着要睡觉 让他们别吵了) --- src/plugins/chat/config.py | 33 +++++++++++++- src/plugins/chat/willing_manager.py | 69 +++++++++++++++++++++++++++++ template/bot_config_template.toml | 19 +++++++- 3 files changed, 118 insertions(+), 3 deletions(-) diff --git a/src/plugins/chat/config.py b/src/plugins/chat/config.py index 891c4e93..5a9cef84 100644 --- a/src/plugins/chat/config.py +++ b/src/plugins/chat/config.py @@ -1,7 +1,7 @@ import os import sys from dataclasses import dataclass, field -from typing import Dict, List, Optional +from typing import Dict, List, Optional, Any import tomli from loguru import logger @@ -35,6 +35,16 @@ class BotConfig: response_interested_rate_amplifier: float = 1.0 # 回复兴趣度放大系数 down_frequency_rate: float = 3.5 # 降低回复频率的群组回复意愿降低系数 + # UTC时间段内的回复意愿控制 + enable_utc_time_control: bool = False # 是否启用基于UTC时间的回复意愿控制 + time_periods: List[Dict[str, Any]] = field( + default_factory=lambda: [ + {"start_hour": 0, "end_hour": 6, "mode": "decrease"}, + {"start_hour": 7, "end_hour": 9, "mode": "increase"}, + {"start_hour": 10, "end_hour": 11, "mode": "decrease"} + ] + ) # 时间段配置列表,控制不同时间段的回复意愿变化 + ban_user_id = set() @@ -296,6 +306,27 @@ class BotConfig: if config.INNER_VERSION in SpecifierSet(">=0.0.6"): config.ban_msgs_regex = msg_config.get("ban_msgs_regex", config.ban_msgs_regex) + + if config.INNER_VERSION in SpecifierSet(">=0.0.10"): + config.enable_utc_time_control = msg_config.get("enable_utc_time_control", config.enable_utc_time_control) + config.time_periods = msg_config.get("time_periods", config.time_periods) + + # 为了兼容旧版本,在0.0.10版本保留旧的配置项,但在0.0.11版本后移除 + if config.INNER_VERSION in SpecifierSet(">=0.0.10,<0.0.11") and "silent_time_start" in msg_config: + # 将旧的配置项转换为新的格式 + config.time_periods = [ + { + "start_hour": msg_config.get("silent_time_start", 0), + "end_hour": msg_config.get("silent_time_end", 6), + "mode": "decrease" + }, + { + "start_hour": msg_config.get("recovery_time_start", 6), + "end_hour": msg_config.get("recovery_time_end", 12), + "mode": "increase" + } + ] + logger.warning("检测到旧版本的时间配置格式,已自动转换为新格式。请更新配置文件到最新版本。") def memory(parent: dict): memory_config = parent["memory"] diff --git a/src/plugins/chat/willing_manager.py b/src/plugins/chat/willing_manager.py index 773d40c6..05f8e23f 100644 --- a/src/plugins/chat/willing_manager.py +++ b/src/plugins/chat/willing_manager.py @@ -1,5 +1,6 @@ import asyncio from typing import Dict +import datetime from .config import global_config @@ -68,6 +69,74 @@ class WillingManager: current_willing *= global_config.response_willing_amplifier # 放大回复意愿 # print(f"放大系数_willing: {global_config.response_willing_amplifier}, 当前意愿: {current_willing}") + # 基于UTC时间调整回复意愿 + if global_config.enable_utc_time_control: + current_utc_hour = datetime.datetime.utcnow().hour + + # 查找当前时间所在的时间段 + current_period = None + for period in global_config.time_periods: + start_hour = period["start_hour"] + end_hour = period["end_hour"] + + # 检查当前时间是否在这个时间段内 + if start_hour <= current_utc_hour < end_hour: + current_period = period + break + + # 如果找到当前时间所在的时间段,应用相应的回复意愿调整 + if current_period: + start_hour = current_period["start_hour"] + end_hour = current_period["end_hour"] + mode = current_period["mode"] + + # 计算在时间段内的位置(0-1之间) + time_range = end_hour - start_hour + if time_range <= 0: # 处理跨夜的情况 + time_range += 24 + position = (current_utc_hour - start_hour) / time_range + + if mode == "decrease": + # 逐渐降低意愿至0 + willing_factor = max(0, 1 - position) + original_willing = current_willing + current_willing *= willing_factor + logger.debug(f"UTC时间{current_utc_hour}在降低时间段{start_hour}-{end_hour}内,位置{position:.2f},意愿因子{willing_factor:.2f},调整前意愿: {original_willing:.2f},调整后意愿: {current_willing:.2f}") + + elif mode == "increase": + # 从0逐渐恢复到配置值 + willing_factor = min(1, position) + original_willing = current_willing + # 这里可能需要根据上一个时间段的终点调整起始值 + # 如果上一个时间段结束于0,则从0开始提高 + current_willing *= willing_factor + logger.debug(f"UTC时间{current_utc_hour}在提高时间段{start_hour}-{end_hour}内,位置{position:.2f},意愿因子{willing_factor:.2f},调整前意愿: {original_willing:.2f},调整后意愿: {current_willing:.2f}") + else: + # 未找到当前时间的配置,需要确定延续哪个时间点的值 + + # 找到最接近当前时间的时间段结束点 + closest_period_end = None + min_hours_diff = 24 + + for period in global_config.time_periods: + end_hour = period["end_hour"] + # 计算结束时间与当前时间的差距(考虑跨天) + hours_diff = (current_utc_hour - end_hour) % 24 + + if hours_diff < min_hours_diff: + min_hours_diff = hours_diff + closest_period_end = period + + if closest_period_end: + mode = closest_period_end["mode"] + # 如果最近的时间段是降低模式,那么当前回复意愿应为0 + if mode == "decrease": + current_willing = 0 + logger.debug(f"UTC时间{current_utc_hour}不在任何配置时间段内,延续最近的降低时间段结束值,意愿设为0") + # 如果最近的时间段是提高模式,保持原值不变(已经是global_config.response_willing_amplifier的值) + else: + logger.debug(f"UTC时间{current_utc_hour}不在任何配置时间段内,延续最近的提高时间段结束值,保持原意愿{current_willing:.2f}") + reply_probability = max((current_willing - 0.45) * 2, 0) # 检查群组权限(如果是群聊) diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 49f3a191..98519fb6 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "0.0.9" +version = "0.0.11" #如果你想要修改配置文件,请在修改后将version的值进行变更 #如果新增项目,请在BotConfig类下新增相应的变量 @@ -37,6 +37,21 @@ thinking_timeout = 120 # 麦麦思考时间 response_willing_amplifier = 1 # 麦麦回复意愿放大系数,一般为1 response_interested_rate_amplifier = 1 # 麦麦回复兴趣度放大系数,听到记忆里的内容时放大系数 down_frequency_rate = 3.5 # 降低回复频率的群组回复意愿降低系数 + +# UTC时间段内的回复意愿控制 +enable_utc_time_control = true # 是否启用基于UTC时间的回复意愿控制 +# 时间段配置,用于控制不同时间段的回复意愿变化 +# 每个时间段包含start_hour(起始小时0-23)、end_hour(结束小时0-23)、mode(模式: "decrease"降低到0, "increase"提高到配置值) +# 未配置的时间段将延续上一个配置时间点的回复意愿值 +# 例如配置 0-6点降低到0,7-9点提高到正常值,10-11点降低到0 +# 那么11点之后直到第二天7点之前,机器人都将保持回复意愿为0 +time_periods = [ + { start_hour = 0, end_hour = 2, mode = "decrease" }, # 0-6点逐渐降低回复意愿到0 + { start_hour = 7, end_hour = 9, mode = "increase" }, # 7-9点逐渐提高回复意愿到配置值 + { start_hour = 12, end_hour = 13, mode = "decrease" } # 10-11点逐渐降低回复意愿到0 + { start_hour = 14, end_hour = 15, mode = "increase" } +] + ban_words = [ # "403","张三" ] @@ -104,7 +119,7 @@ reaction = "有人说你是人机或者机器人,否定这一事实,攻击 [[keywords_reaction.rules]] # 就像这样复制 enable = false # 仅作示例,不会触发 keywords = ["测试关键词回复","test",""] -reaction = "回答“测试成功”" +reaction = "回答"测试成功"" [chinese_typo] enable = true # 是否启用中文错别字生成器