Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/builtins/tasks.py

75 lines
3.1 KiB
Python
Raw Normal View History

2022-06-28 08:47:00 +00:00
from datetime import datetime
2022-06-27 17:00:00 +00:00
2023-06-29 13:49:35 +00:00
from core.logger import Logger
2023-02-05 14:33:33 +00:00
from core.types import MessageSession
2022-07-31 08:27:58 +00:00
2022-06-27 17:00:00 +00:00
class MessageTaskManager:
_list = {}
2023-06-02 17:31:06 +00:00
@classmethod
def add_task(cls, session: MessageSession, flag, all_=False, reply=None):
2022-06-27 17:00:00 +00:00
sender = session.target.senderId
2022-06-28 06:11:03 +00:00
task_type = 'reply' if reply is not None else 'wait'
2022-06-27 17:00:00 +00:00
if all_:
sender = 'all'
2023-06-02 17:31:06 +00:00
cls._list.update(
2023-06-28 15:21:38 +00:00
{session.target.targetId: {sender: {session: {'flag': flag, 'active': True, 'type': task_type,
'reply': reply, 'ts': datetime.now().timestamp()}}}})
2023-06-29 13:49:35 +00:00
Logger.info(cls._list)
2022-06-27 17:00:00 +00:00
2023-06-02 17:31:06 +00:00
@classmethod
def get_result(cls, session: MessageSession):
2023-06-28 15:21:38 +00:00
if 'result' in cls._list[session.target.targetId][session.target.senderId][session]:
return cls._list[session.target.targetId][session.target.senderId][session]['result']
2022-06-28 08:47:00 +00:00
else:
return None
2022-06-27 17:00:00 +00:00
2023-06-02 17:31:06 +00:00
@classmethod
def get(cls):
return cls._list
2022-06-27 17:00:00 +00:00
2023-06-02 17:31:06 +00:00
@classmethod
2023-06-28 15:21:38 +00:00
async def bg_check(cls):
2023-06-02 17:31:06 +00:00
for target in cls._list:
for sender in cls._list[target]:
2023-06-28 15:21:38 +00:00
for session in cls._list[target][sender]:
if cls._list[target][sender][session]['active']:
if datetime.now().timestamp() - cls._list[target][sender][session]['ts'] > 3600:
cls._list[target][sender][session]['active'] = False
cls._list[target][sender][session]['flag'].set() # no result = cancel
@classmethod
2023-06-29 11:12:50 +00:00
async def check(cls, session: MessageSession):
2023-06-02 17:31:06 +00:00
if session.target.targetId in cls._list:
2023-06-29 13:14:31 +00:00
senders = []
2023-06-02 17:31:06 +00:00
if session.target.senderId in cls._list[session.target.targetId]:
2023-06-29 13:14:31 +00:00
senders.append(session.target.senderId)
2023-06-02 17:31:06 +00:00
if 'all' in cls._list[session.target.targetId]:
2023-06-29 13:14:31 +00:00
senders.append('all')
if senders is not None:
for sender in senders:
for s in cls._list[session.target.targetId][sender]:
get_ = cls._list[session.target.targetId][sender][s]
if get_['type'] == 'wait':
get_['result'] = session
get_['active'] = False
get_['flag'].set()
elif get_['type'] == 'reply':
if isinstance(get_['reply'], list):
for reply in get_['reply']:
if reply == s.target.replyId:
get_['result'] = session
get_['active'] = False
get_['flag'].set()
break
else:
if get_['reply'] == s.target.replyId:
2023-06-28 15:21:38 +00:00
get_['result'] = session
get_['active'] = False
get_['flag'].set()
2022-06-27 17:00:00 +00:00
__all__ = ['MessageTaskManager']