From c2be41eb4a0ff10c8f14788501eef16edd17021b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Sat, 3 Aug 2024 03:10:18 +0800 Subject: [PATCH] Update senderinfo --- bots/api/bot.py | 15 ++++++---- bots/discord/slash_message.py | 2 +- core/builtins/message/__init__.py | 4 +-- core/parser/message.py | 8 +++--- core/tos.py | 2 +- database/__init__.py | 48 +++++++++++++++++++++++++------ 6 files changed, 58 insertions(+), 21 deletions(-) diff --git a/bots/api/bot.py b/bots/api/bot.py index d4cf666437..836d0083d7 100644 --- a/bots/api/bot.py +++ b/bots/api/bot.py @@ -107,14 +107,19 @@ async def get_target(target_id: str): @app.get('/sender/{sender_id}') async def get_sender(sender_id: str): sender = BotDBUtil.SenderInfo(sender_id) + if not sender.query: + return JSONResponse(status_code=404, content={ + 'target_id': sender_id, + 'notFound': True, + }) return { 'senderId': sender_id, - 'isInBlockList': sender.query.isInBlockList, - 'isInAllowList': sender.query.isInAllowList, - 'isSuperUser': sender.query.isSuperUser, - 'warns': sender.query.warns, - 'disableTyping': sender.query.disable_typing + 'isInBlockList': sender.is_in_block_list, + 'isInAllowList': sender.is_in_allow_list, + 'isSuperUser': sender.is_super_user, + 'warns': sender.warns, + 'disableTyping': sender.disable_typing } diff --git a/bots/discord/slash_message.py b/bots/discord/slash_message.py index 31e3f1932c..98e4e292f4 100644 --- a/bots/discord/slash_message.py +++ b/bots/discord/slash_message.py @@ -84,7 +84,7 @@ async def send_message(self, message_chain, quote=True, disable_secret_check=Fal async def check_permission(self): if self.session.message.channel.permissions_for(self.session.message.author).administrator \ or isinstance(self.session.message.channel, discord.DMChannel) \ - or self.target.sender_info.query.isSuperUser \ + or self.target.sender_info.is_super_user \ or self.target.sender_info.check_TargetAdmin(self.target.target_id): return True return False diff --git a/core/builtins/message/__init__.py b/core/builtins/message/__init__.py index 6b4ef5b9e3..56236ef437 100644 --- a/core/builtins/message/__init__.py +++ b/core/builtins/message/__init__.py @@ -143,10 +143,10 @@ async def sleep(self, s): await asyncio.sleep(s) def check_super_user(self): - return True if self.target.sender_info.query.isSuperUser else False + return True if self.target.sender_info.is_super_user else False async def check_permission(self): - if self.target.sender_id in self.custom_admins or self.target.sender_info.query.isSuperUser: + if self.target.sender_id in self.custom_admins or self.target.sender_info.is_super_user: return True return await self.check_native_permission() diff --git a/core/parser/message.py b/core/parser/message.py index 63b56007c0..8d02b85bfb 100644 --- a/core/parser/message.py +++ b/core/parser/message.py @@ -173,7 +173,7 @@ async def parser(msg: Bot.MessageSession, require_enable_modules: bool = True, p if len(msg.trigger_msg) == 0: return msg.target.sender_info = BotDBUtil.SenderInfo(msg.target.sender_id) - if msg.target.sender_info.query.isInBlockList and not msg.target.sender_info.query.isInAllowList and not msg.target.sender_info.query.isSuperUser \ + if msg.target.sender_info.is_in_block_list and not msg.target.sender_info.is_in_allow_list and not msg.target.sender_info.is_super_user \ or msg.target.sender_id in msg.options.get('ban', []): return @@ -368,7 +368,7 @@ async def execute_submodule(msg: Bot.MessageSession, command_first_word, command else: kwargs[func_params[list(func_params.keys())[0]].name] = msg - if not msg.target.sender_info.query.disable_typing: + if not msg.target.sender_info.disable_typing: async with msg.Typing(msg): await parsed_msg[0].function(**kwargs) # 将msg传入下游模块 else: @@ -400,7 +400,7 @@ async def execute_submodule(msg: Bot.MessageSession, command_first_word, command msg.parsed_msg = None for func in module.command_list.set: if not func.help_doc: - if not msg.target.sender_info.query.disable_typing: + if not msg.target.sender_info.disable_typing: async with msg.Typing(msg): await func.function(msg) # 将msg传入下游模块 else: @@ -535,7 +535,7 @@ async def execute_submodule(msg: Bot.MessageSession, command_first_word, command ExecutionLockList.add(msg) else: return await msg.send_message(msg.locale.t("parser.command.running.prompt")) - if rfunc.show_typing and not msg.target.sender_info.query.disable_typing: + if rfunc.show_typing and not msg.target.sender_info.disable_typing: async with msg.Typing(msg): await rfunc.function(msg) # 将msg传入下游模块 else: diff --git a/core/tos.py b/core/tos.py index b6919192d5..9f015ce188 100644 --- a/core/tos.py +++ b/core/tos.py @@ -9,7 +9,7 @@ async def warn_target(msg: Bot.MessageSession, reason: str): if WARNING_COUNTS >= 1 and not msg.check_super_user(): - current_warns = int(msg.target.sender_info.query.warns) + 1 + current_warns = int(msg.target.sender_info.warns) + 1 msg.target.sender_info.edit('warns', current_warns) warn_template = [msg.locale.t("tos.message.warning")] i18n_reason = msg.locale.tl_str(reason) diff --git a/database/__init__.py b/database/__init__.py index 9336651bb6..ea5270d996 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -228,21 +228,53 @@ def clear_petal(self) -> bool: return True class SenderInfo: - @retry(stop=stop_after_attempt(3)) - @auto_rollback_error def __init__(self, sender_id): self.sender_id = sender_id self.query = self.query_SenderInfo - if not self.query: - session.add_all([SenderInfo(id=sender_id)]) - session.commit() - self.query = session.query(SenderInfo).filter_by(id=sender_id).first() @property - @retry(stop=stop_after_attempt(3)) - @auto_rollback_error def query_SenderInfo(self): return session.query(SenderInfo).filter_by(id=self.sender_id).first() + + @retry(stop=stop_after_attempt(3)) + @auto_rollback_error + def init(self): + if not self.query: + session.add_all([SenderInfo(id=self.sender_id)]) + session.commit() + return self.query_SenderInfo + else: + return self.query + + @property + def is_in_block_list(self): + if not self.query: + return False + return self.query.isInBlockList + + @property + def is_in_allow_list(self): + if not self.query: + return False + return self.query.isInAllowList + + @property + def is_super_user(self): + if not self.query: + return False + return self.query.isSuperUser + + @property + def warns(self): + if not self.query: + return 0 + return self.query.warns + + @property + def disable_typing(self): + if not self.query: + return False + return self.query.disable_typing @retry(stop=stop_after_attempt(3)) @auto_rollback_error