2022-06-27 17:00:00 +00:00
|
|
|
import asyncio
|
2022-08-26 18:51:56 +00:00
|
|
|
from typing import List
|
2022-06-27 17:00:00 +00:00
|
|
|
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.builtins.message.chain import *
|
|
|
|
from core.builtins.message.internal import *
|
|
|
|
from core.builtins.tasks import MessageTaskManager
|
|
|
|
from core.builtins.temp import ExecutionLockList
|
|
|
|
from core.builtins.utils import confirm_command
|
2022-06-28 08:47:00 +00:00
|
|
|
from core.exceptions import WaitCancelException
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.types.message import *
|
2023-03-04 10:17:07 +00:00
|
|
|
from core.utils.i18n import Locale
|
2022-08-25 18:42:31 +00:00
|
|
|
from database import BotDBUtil
|
|
|
|
|
2022-06-27 17:00:00 +00:00
|
|
|
|
|
|
|
class MessageSession(MessageSession):
|
2022-08-26 18:51:56 +00:00
|
|
|
def __init__(self,
|
|
|
|
target: MsgInfo,
|
|
|
|
session: Session):
|
|
|
|
self.target = target
|
|
|
|
self.session = session
|
|
|
|
self.sent: List[MessageChain] = []
|
|
|
|
self.prefixes: List[str] = []
|
2022-08-25 18:42:31 +00:00
|
|
|
self.data = BotDBUtil.TargetInfo(self.target.targetId)
|
|
|
|
self.muted = self.data.is_muted
|
|
|
|
self.options = self.data.options
|
|
|
|
self.custom_admins = self.data.custom_admins
|
|
|
|
self.enabled_modules = self.data.enabled_modules
|
2023-03-04 10:08:40 +00:00
|
|
|
self.locale = Locale(self.data.locale)
|
2022-08-25 18:42:31 +00:00
|
|
|
|
2022-06-28 06:11:03 +00:00
|
|
|
async def waitConfirm(self, msgchain=None, quote=True, delete=True) -> bool:
|
2022-06-27 17:00:00 +00:00
|
|
|
send = None
|
|
|
|
ExecutionLockList.remove(self)
|
|
|
|
if msgchain is not None:
|
|
|
|
msgchain = MessageChain(msgchain)
|
2023-03-05 11:15:44 +00:00
|
|
|
msgchain.append(Plain(self.locale.t("message.wait.confirm.prompt.type1")))
|
2022-06-27 17:00:00 +00:00
|
|
|
send = await self.sendMessage(msgchain, quote)
|
|
|
|
flag = asyncio.Event()
|
|
|
|
MessageTaskManager.add_task(self, flag)
|
|
|
|
await flag.wait()
|
2022-06-28 08:47:00 +00:00
|
|
|
result = MessageTaskManager.get_result(self)
|
|
|
|
if result:
|
|
|
|
if msgchain is not None and delete:
|
|
|
|
await send.delete()
|
2023-03-04 09:40:38 +00:00
|
|
|
if result.asDisplay(text_only=True) in confirm_command:
|
2022-06-28 08:47:00 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise WaitCancelException
|
2022-06-27 17:00:00 +00:00
|
|
|
|
2023-02-07 01:12:52 +00:00
|
|
|
async def waitNextMessage(self, msgchain=None, quote=True, delete=False, append_instruction=True) -> MessageSession:
|
2022-08-04 15:52:53 +00:00
|
|
|
sent = None
|
2022-06-28 16:10:44 +00:00
|
|
|
ExecutionLockList.remove(self)
|
|
|
|
if msgchain is not None:
|
|
|
|
msgchain = MessageChain(msgchain)
|
2023-02-07 01:12:52 +00:00
|
|
|
if append_instruction:
|
2023-03-05 11:15:44 +00:00
|
|
|
msgchain.append(Plain(self.locale.t("message.wait.confirm.prompt.type2")))
|
2022-08-04 15:52:53 +00:00
|
|
|
sent = await self.sendMessage(msgchain, quote)
|
2022-06-28 16:10:44 +00:00
|
|
|
flag = asyncio.Event()
|
|
|
|
MessageTaskManager.add_task(self, flag)
|
|
|
|
await flag.wait()
|
|
|
|
result = MessageTaskManager.get_result(self)
|
2022-08-04 15:52:53 +00:00
|
|
|
if delete and sent is not None:
|
|
|
|
await sent.delete()
|
2022-06-28 16:10:44 +00:00
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
raise WaitCancelException
|
|
|
|
|
2023-02-07 01:12:52 +00:00
|
|
|
async def waitReply(self, msgchain, quote=True, all_=False, append_instruction=True) -> MessageSession:
|
2022-06-28 06:11:03 +00:00
|
|
|
ExecutionLockList.remove(self)
|
|
|
|
msgchain = MessageChain(msgchain)
|
2023-02-07 01:12:52 +00:00
|
|
|
if append_instruction:
|
2023-04-30 03:30:59 +00:00
|
|
|
msgchain.append(Plain(self.locale.t("message.reply.prompt")))
|
2022-06-28 06:11:03 +00:00
|
|
|
send = await self.sendMessage(msgchain, quote)
|
|
|
|
flag = asyncio.Event()
|
2023-01-27 08:02:04 +00:00
|
|
|
MessageTaskManager.add_task(self, flag, reply=send.messageId, all_=all_)
|
2022-06-28 06:11:03 +00:00
|
|
|
await flag.wait()
|
2022-06-28 08:47:00 +00:00
|
|
|
result = MessageTaskManager.get_result(self)
|
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
raise WaitCancelException
|
2022-06-28 06:11:03 +00:00
|
|
|
|
|
|
|
async def waitAnyone(self, msgchain=None, delete=False) -> MessageSession:
|
2022-06-27 17:00:00 +00:00
|
|
|
send = None
|
|
|
|
ExecutionLockList.remove(self)
|
|
|
|
if msgchain is not None:
|
|
|
|
msgchain = MessageChain(msgchain)
|
|
|
|
send = await self.sendMessage(msgchain, quote=False)
|
|
|
|
flag = asyncio.Event()
|
|
|
|
MessageTaskManager.add_task(self, flag, all_=True)
|
|
|
|
await flag.wait()
|
2022-06-28 08:47:00 +00:00
|
|
|
result = MessageTaskManager.get()[self.target.targetId]['all']
|
|
|
|
if 'result' in result:
|
|
|
|
if send is not None and delete:
|
|
|
|
await send.delete()
|
|
|
|
return MessageTaskManager.get()[self.target.targetId]['all']['result']
|
|
|
|
else:
|
|
|
|
raise WaitCancelException
|
2022-06-27 17:00:00 +00:00
|
|
|
|
2022-06-28 06:11:03 +00:00
|
|
|
async def sleep(self, s):
|
|
|
|
ExecutionLockList.remove(self)
|
|
|
|
await asyncio.sleep(s)
|
|
|
|
|
|
|
|
def checkSuperUser(self):
|
|
|
|
return True if self.target.senderInfo.query.isSuperUser else False
|
|
|
|
|
2022-06-27 17:00:00 +00:00
|
|
|
|
|
|
|
__all__ = ["MessageSession"]
|