diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index b77ad94bc..b62f839ae 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -47,6 +47,7 @@ DEFAULT_CONFIG = { "web_search_link": False, "identifier": False, "datetime_system_prompt": True, + "timezone": "Asia/Shanghai", "default_personality": "default", "prompt_prefix": "", "max_context_length": -1, @@ -977,6 +978,12 @@ CONFIG_METADATA_2 = { "obvious_hint": True, "hint": "启用后,会在系统提示词中加上当前机器的日期时间。", }, + "timezone": { + "description": "时区设置", + "type": "string", + "obvious_hint": True, + "hint": "时区设置。请填写时区名称,如 Asia/Shanghai。", + }, "default_personality": { "description": "默认采用的人格情景的名称", "type": "string", diff --git a/packages/astrbot/main.py b/packages/astrbot/main.py index 3887fc929..207067a9d 100644 --- a/packages/astrbot/main.py +++ b/packages/astrbot/main.py @@ -22,8 +22,8 @@ from astrbot.core.config.default import VERSION from .long_term_memory import LongTermMemory from astrbot.core import logger from astrbot.api.message_components import Plain, Image, Reply - from typing import Union +import pytz @star.register( @@ -39,6 +39,7 @@ class Main(star.Star): self.prompt_prefix = cfg["provider_settings"]["prompt_prefix"] self.identifier = cfg["provider_settings"]["identifier"] self.enable_datetime = cfg["provider_settings"]["datetime_system_prompt"] + self.timezone = cfg["provider_settings"]["timezone"] self.ltm = None if ( @@ -969,7 +970,8 @@ UID: {user_id} 此 ID 可用于设置管理员。 if len(l) == 1: message.set_result( MessageEventResult() - .message(f"""[Persona] + .message( + f"""[Persona] - 人格情景列表: `/persona list` - 设置人格情景: `/persona 人格` @@ -980,7 +982,8 @@ UID: {user_id} 此 ID 可用于设置管理员。 当前对话 {curr_cid_title} 的人格情景: {curr_persona_name} 配置人格情景请前往管理面板-配置页 -""") +""" + ) .use_t2i(False) ) elif l[1] == "list": @@ -1190,11 +1193,26 @@ UID: {user_id} 此 ID 可用于设置管理员。 user_info = f"\n[User ID: {user_id}, Nickname: {user_nickname}]\n" req.prompt = user_info + req.prompt + # 启用附加时间戳 if self.enable_datetime: - # Including timezone - current_time = ( - datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M (%Z)") - ) + # 启用时区 + if self.timezone: + try: + tz = pytz.timezone(self.timezone) + now = datetime.datetime.now(tz) + current_time = now.strftime("%Y-%m-%d %H:%M (%Z)") + except Exception as e: + logger.error(f"时区设置错误: {e}, 使用本地时区") + current_time = ( + datetime.datetime.now() + .astimezone() + .strftime("%Y-%m-%d %H:%M (%Z)") + ) + # 未启用时区 + else: + current_time = ( + datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M (%Z)") + ) req.system_prompt += f"\nCurrent datetime: {current_time}\n" if req.conversation: