From 31c300c47c610f656bc3e9bed0588130e8851678 Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 21 Mar 2023 11:32:32 +0800 Subject: [PATCH 1/2] plugin: avoid mess after session expiration --- plugins/dungeon/dungeon.py | 8 +++++++- plugins/role/role.py | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/plugins/dungeon/dungeon.py b/plugins/dungeon/dungeon.py index 955840fe1..9df723054 100644 --- a/plugins/dungeon/dungeon.py +++ b/plugins/dungeon/dungeon.py @@ -3,6 +3,8 @@ from bridge.bridge import Bridge from bridge.context import ContextType from bridge.reply import Reply, ReplyType +from common.expired_dict import ExpiredDict +from config import conf import plugins from plugins import * from common.log import logger @@ -38,7 +40,11 @@ def __init__(self): super().__init__() self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context logger.info("[Dungeon] inited") - self.games = {} + # 目前没有设计session过期事件,这里先暂时使用过期字典 + if conf().get('expires_in_seconds'): + self.games = ExpiredDict(conf().get('expires_in_seconds')) + else: + self.games = dict() def on_handle_context(self, e_context: EventContext): diff --git a/plugins/role/role.py b/plugins/role/role.py index 91c09be5a..e0929149c 100644 --- a/plugins/role/role.py +++ b/plugins/role/role.py @@ -14,14 +14,17 @@ class RolePlay(): def __init__(self, bot, sessionid, desc, wrapper=None): self.bot = bot self.sessionid = sessionid - bot.sessions.clear_session(sessionid) - bot.sessions.build_session(sessionid, desc) self.wrapper = wrapper or "%s" # 用于包装用户输入 + self.desc = desc def reset(self): self.bot.sessions.clear_session(self.sessionid) def action(self, user_action): + session = self.bot.sessions.build_session(self.sessionid, self.desc) + if session[0]['role'] == 'system' and session[0]['content'] != self.desc: # 目前没有触发session过期事件,这里先简单判断,然后重置 + self.reset() + self.bot.sessions.build_session(self.sessionid, self.desc) prompt = self.wrapper % user_action return prompt @@ -105,7 +108,7 @@ def on_handle_context(self, e_context: EventContext): e_context.action = EventAction.BREAK_PASS return else: - self.roleplays[sessionid] = RolePlay(bot, sessionid, self.roles[role][desckey],self.roles[role].get("wrapper","%s")) + self.roleplays[sessionid] = RolePlay(bot, sessionid, self.roles[role][desckey], self.roles[role].get("wrapper","%s")) reply = Reply(ReplyType.INFO, f"角色设定为 {role} :\n"+self.roles[role][desckey]) e_context['reply'] = reply e_context.action = EventAction.BREAK_PASS From 53c11c43417ccb1f1f11c31346071bfa680da66b Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 21 Mar 2023 12:13:57 +0800 Subject: [PATCH 2/2] role: modify help text --- plugins/role/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/role/role.py b/plugins/role/role.py index e0929149c..648a4c600 100644 --- a/plugins/role/role.py +++ b/plugins/role/role.py @@ -119,7 +119,7 @@ def on_handle_context(self, e_context: EventContext): e_context.action = EventAction.CONTINUE def get_help_text(self): - help_text = "输入\"$角色 (角色名)\"或\"$role (角色名)\"为我设定角色吧,#reset 可以清除设定的角色。\n目前可用角色列表:\n" + help_text = "输入\"$角色 (角色名)\"或\"$role (角色名)\"为我设定角色吧,$停止扮演 可以清除设定的角色。\n目前可用角色列表:\n" for role in self.roles: help_text += f"[{role}]: {self.roles[role]['remark']}\n" return help_text