diff --git a/src/plugins/chat/willing_manager.py b/src/plugins/chat/willing_manager.py index 05f8e23f..623df841 100644 --- a/src/plugins/chat/willing_manager.py +++ b/src/plugins/chat/willing_manager.py @@ -14,6 +14,88 @@ class WillingManager: self.chat_reply_willing: Dict[str, float] = {} # 存储每个聊天流的回复意愿 self._decay_task = None self._started = False + self.default_willing: float = global_config.response_willing_amplifier # 默认回复意愿 + + # 根据当前UTC时间和time_periods初始化默认回复意愿状态 + self._init_willing_based_on_utc_time() + + def _init_willing_based_on_utc_time(self): + """根据当前UTC时间初始化回复意愿状态""" + if not global_config.enable_utc_time_control: + return + + current_utc_hour = datetime.datetime.utcnow().hour + logger.debug(f"机器人启动时UTC时间为{current_utc_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) + # 设置全局默认回复意愿 + default_willing = global_config.response_willing_amplifier * willing_factor + logger.info(f"启动时UTC时间{current_utc_hour}在降低时间段{start_hour}-{end_hour}内,位置{position:.2f},意愿因子{willing_factor:.2f},初始意愿设为: {default_willing:.2f}") + + elif mode == "increase": + # 从0逐渐恢复到配置值,根据位置计算当前应有的意愿 + willing_factor = min(1, position) + # 设置全局默认回复意愿 + default_willing = global_config.response_willing_amplifier * willing_factor + logger.info(f"启动时UTC时间{current_utc_hour}在提高时间段{start_hour}-{end_hour}内,位置{position:.2f},意愿因子{willing_factor:.2f},初始意愿设为: {default_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": + default_willing = 0 + logger.info(f"启动时UTC时间{current_utc_hour}不在任何配置时间段内,延续最近的降低时间段结束值,初始意愿设为0") + # 如果最近的时间段是提高模式,设为最大值 + else: + default_willing = global_config.response_willing_amplifier + logger.info(f"启动时UTC时间{current_utc_hour}不在任何配置时间段内,延续最近的提高时间段结束值,初始意愿设为{default_willing:.2f}") + else: + # 没有任何时间段配置,使用默认值 + default_willing = global_config.response_willing_amplifier + logger.info(f"没有找到任何有效的时间段配置,初始意愿设为默认值{default_willing:.2f}") + + # 保存计算出的默认回复意愿 + self.default_willing = default_willing async def _decay_reply_willing(self): """定期衰减回复意愿""" @@ -26,7 +108,8 @@ class WillingManager: """获取指定聊天流的回复意愿""" stream = chat_stream if stream: - return self.chat_reply_willing.get(stream.stream_id, 0) + # 如果该聊天流没有回复意愿记录,使用默认回复意愿 + return self.chat_reply_willing.get(stream.stream_id, self.default_willing) return 0 def set_willing(self, chat_id: str, willing: float): @@ -47,7 +130,8 @@ class WillingManager: stream = chat_stream chat_id = stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) + # 获取当前聊天流的回复意愿,如果不存在则使用默认回复意愿 + current_willing = self.chat_reply_willing.get(chat_id, self.default_willing) if is_mentioned_bot and current_willing < 1.0: current_willing += 0.9 diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 2016684e..6eed754b 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "0.0.11" +version = "0.0.12" #如果你想要修改配置文件,请在修改后将version的值进行变更 #如果新增项目,请在BotConfig类下新增相应的变量