refactor(mood_api): 为情绪API函数增加布尔返回值和错误处理
此重构修改了 `set_mood`, `lock_mood`, 和 `unlock_mood` 函数,使其返回布尔值以明确指示操作是否成功。 - 为 `set_mood` 和 `lock_mood` 添加了 `try...except` 块,以捕获异常并在失败时返回 `False`。 - `unlock_mood` 现在会在情绪本未锁定的情况下返回 `False`,提供了更清晰的状态反馈。 这些更改提高了API的健壮性,使插件等调用方能够更可靠地处理操作结果。 BREAKING CHANGE: `set_mood`, `lock_mood`, 和 `unlock_mood` 函数现在返回一个布尔值来表示操作是否成功,而不再是隐式地返回 `None`。
This commit is contained in:
committed by
Windpicker-owo
parent
c684b2a5a9
commit
fd24e7cb0f
@@ -47,16 +47,23 @@ def get_mood(chat_id: str) -> str:
|
|||||||
return chat_mood.mood_state
|
return chat_mood.mood_state
|
||||||
|
|
||||||
|
|
||||||
def set_mood(chat_id: str, new_mood: str):
|
def set_mood(chat_id: str, new_mood: str) -> bool:
|
||||||
"""强制设定指定聊天的新情绪状态
|
"""强制设定指定聊天的新情绪状态
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
chat_id (str): 聊天ID
|
chat_id (str): 聊天ID
|
||||||
new_mood (str): 新的情绪状态
|
new_mood (str): 新的情绪状态
|
||||||
|
Returns:
|
||||||
|
bool: 操作是否成功
|
||||||
"""
|
"""
|
||||||
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
|
try:
|
||||||
chat_mood.mood_state = new_mood
|
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
|
||||||
logger.info(f"[{chat_id}] 情绪状态被强制设置为: {new_mood}")
|
chat_mood.mood_state = new_mood
|
||||||
|
logger.info(f"[{chat_id}] 情绪状态被强制设置为: {new_mood}")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"强制设定指定聊天的新情绪状态时发生错误:{e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def lock_mood(chat_id: str, duration: float | None = None):
|
async def lock_mood(chat_id: str, duration: float | None = None):
|
||||||
@@ -66,30 +73,39 @@ async def lock_mood(chat_id: str, duration: float | None = None):
|
|||||||
Args:
|
Args:
|
||||||
chat_id (str): 聊天ID
|
chat_id (str): 聊天ID
|
||||||
duration (Optional[float]): 锁定时长(秒)。如果为 None,则永久锁定直到手动解锁。
|
duration (Optional[float]): 锁定时长(秒)。如果为 None,则永久锁定直到手动解锁。
|
||||||
|
Returns:
|
||||||
|
bool: 操作是否成功
|
||||||
"""
|
"""
|
||||||
if chat_id in _unlock_tasks:
|
try:
|
||||||
_unlock_tasks[chat_id].cancel()
|
if chat_id in _unlock_tasks:
|
||||||
del _unlock_tasks[chat_id]
|
_unlock_tasks[chat_id].cancel()
|
||||||
|
del _unlock_tasks[chat_id]
|
||||||
|
|
||||||
mood_manager.insomnia_chats.add(chat_id)
|
mood_manager.insomnia_chats.add(chat_id)
|
||||||
logger.info(f"[{chat_id}] 情绪已锁定。")
|
logger.info(f"[{chat_id}] 情绪已锁定。")
|
||||||
|
|
||||||
if duration:
|
if duration:
|
||||||
logger.info(f"[{chat_id}] 情绪将于 {duration} 秒后自动解锁。")
|
logger.info(f"[{chat_id}] 情绪将于 {duration} 秒后自动解锁。")
|
||||||
|
|
||||||
async def _unlock_after():
|
async def _unlock_after():
|
||||||
await asyncio.sleep(duration)
|
await asyncio.sleep(duration)
|
||||||
if chat_id in mood_manager.insomnia_chats:
|
if chat_id in mood_manager.insomnia_chats:
|
||||||
await unlock_mood(chat_id)
|
await unlock_mood(chat_id)
|
||||||
logger.info(f"[{chat_id}] 情绪已自动解锁。")
|
logger.info(f"[{chat_id}] 情绪已自动解锁。")
|
||||||
|
task = asyncio.create_task(_unlock_after())
|
||||||
task = asyncio.create_task(_unlock_after())
|
_unlock_tasks[chat_id] = task
|
||||||
_unlock_tasks[chat_id] = task
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"锁定指定聊天的情绪时发生错误:{e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def unlock_mood(chat_id: str):
|
async def unlock_mood(chat_id: str) -> bool:
|
||||||
"""
|
"""
|
||||||
立即解除情绪锁定。
|
立即解除情绪锁定。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 如果成功解锁则返回 True,如果情绪未被锁定则返回 False。
|
||||||
"""
|
"""
|
||||||
if chat_id in _unlock_tasks:
|
if chat_id in _unlock_tasks:
|
||||||
_unlock_tasks[chat_id].cancel()
|
_unlock_tasks[chat_id].cancel()
|
||||||
@@ -98,6 +114,8 @@ async def unlock_mood(chat_id: str):
|
|||||||
if chat_id in mood_manager.insomnia_chats:
|
if chat_id in mood_manager.insomnia_chats:
|
||||||
mood_manager.insomnia_chats.remove(chat_id)
|
mood_manager.insomnia_chats.remove(chat_id)
|
||||||
logger.info(f"[{chat_id}] 情绪已手动解锁。")
|
logger.info(f"[{chat_id}] 情绪已手动解锁。")
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def is_mood_locked(chat_id: str) -> bool:
|
def is_mood_locked(chat_id: str) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user