允许用户配置在具体时间段内逐步降低回复意愿

模拟人类的睡眠/工作等情况
(bot太能bb了,又因为日程表的原因导致他会一直喊着要睡觉 让他们别吵了)
pull/273/head
Cindy-Master 2025-03-13 02:49:01 +08:00
parent 1e3ebdcdf0
commit 1112ff432b
3 changed files with 118 additions and 3 deletions

View File

@ -1,7 +1,7 @@
import os import os
import sys import sys
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Dict, List, Optional from typing import Dict, List, Optional, Any
import tomli import tomli
from loguru import logger from loguru import logger
@ -35,6 +35,16 @@ class BotConfig:
response_interested_rate_amplifier: float = 1.0 # 回复兴趣度放大系数 response_interested_rate_amplifier: float = 1.0 # 回复兴趣度放大系数
down_frequency_rate: float = 3.5 # 降低回复频率的群组回复意愿降低系数 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() ban_user_id = set()
@ -297,6 +307,27 @@ class BotConfig:
if config.INNER_VERSION in SpecifierSet(">=0.0.6"): if config.INNER_VERSION in SpecifierSet(">=0.0.6"):
config.ban_msgs_regex = msg_config.get("ban_msgs_regex", config.ban_msgs_regex) 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): def memory(parent: dict):
memory_config = parent["memory"] memory_config = parent["memory"]
config.build_memory_interval = memory_config.get("build_memory_interval", config.build_memory_interval) config.build_memory_interval = memory_config.get("build_memory_interval", config.build_memory_interval)

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
from typing import Dict from typing import Dict
import datetime
from .config import global_config from .config import global_config
@ -68,6 +69,74 @@ class WillingManager:
current_willing *= global_config.response_willing_amplifier # 放大回复意愿 current_willing *= global_config.response_willing_amplifier # 放大回复意愿
# print(f"放大系数_willing: {global_config.response_willing_amplifier}, 当前意愿: {current_willing}") # 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) reply_probability = max((current_willing - 0.45) * 2, 0)
# 检查群组权限(如果是群聊) # 检查群组权限(如果是群聊)

View File

@ -1,5 +1,5 @@
[inner] [inner]
version = "0.0.9" version = "0.0.11"
#如果你想要修改配置文件请在修改后将version的值进行变更 #如果你想要修改配置文件请在修改后将version的值进行变更
#如果新增项目请在BotConfig类下新增相应的变量 #如果新增项目请在BotConfig类下新增相应的变量
@ -37,6 +37,21 @@ thinking_timeout = 120 # 麦麦思考时间
response_willing_amplifier = 1 # 麦麦回复意愿放大系数一般为1 response_willing_amplifier = 1 # 麦麦回复意愿放大系数一般为1
response_interested_rate_amplifier = 1 # 麦麦回复兴趣度放大系数,听到记忆里的内容时放大系数 response_interested_rate_amplifier = 1 # 麦麦回复兴趣度放大系数,听到记忆里的内容时放大系数
down_frequency_rate = 3.5 # 降低回复频率的群组回复意愿降低系数 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点降低到07-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 = [ ban_words = [
# "403","张三" # "403","张三"
] ]
@ -104,7 +119,7 @@ reaction = "有人说你是人机或者机器人,否定这一事实,攻击
[[keywords_reaction.rules]] # 就像这样复制 [[keywords_reaction.rules]] # 就像这样复制
enable = false # 仅作示例,不会触发 enable = false # 仅作示例,不会触发
keywords = ["测试关键词回复","test",""] keywords = ["测试关键词回复","test",""]
reaction = "回答“测试成功”" reaction = "回答"""
[chinese_typo] [chinese_typo]
enable = true # 是否启用中文错别字生成器 enable = true # 是否启用中文错别字生成器