Skip to content

Commit

Permalink
Update senderinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroWolf committed Aug 2, 2024
1 parent 6c76ef1 commit c2be41e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
15 changes: 10 additions & 5 deletions bots/api/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion bots/discord/slash_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/builtins/message/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions core/parser/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion core/tos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 40 additions & 8 deletions database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2be41e

Please sign in to comment.