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/__init__.py

100 lines
3.9 KiB
Python
Raw Normal View History

2023-08-30 11:52:59 +00:00
import asyncio
2023-04-17 08:55:55 +00:00
from typing import Union, List
2023-04-16 07:44:17 +00:00
from config import Config
2023-08-29 14:08:47 +00:00
from core.loader import ModulesManager
2023-09-03 08:50:12 +00:00
from core.types.message import FetchTarget, FetchedSession as FS, MsgInfo, Session, ModuleHookContext
2023-04-17 08:55:55 +00:00
from database import BotDBUtil
2023-02-04 14:42:21 +00:00
from .message import *
2023-02-05 14:33:33 +00:00
from .message.chain import *
from .message.internal import *
from .tasks import *
from .temp import *
from .utils import *
2023-02-04 14:42:21 +00:00
class Bot:
MessageSession = MessageSession
FetchTarget = FetchTarget
2023-08-27 14:51:16 +00:00
client_name = FetchTarget.name
FetchedSession = FS
ModuleHookContext = ModuleHookContext
2023-04-16 07:44:17 +00:00
2023-04-16 07:46:17 +00:00
@staticmethod
2023-09-01 14:38:32 +00:00
async def send_message(target: Union[FS, MessageSession, str], msg: Union[MessageChain, list],
disable_secret_check=False,
allow_split_image=True):
2023-04-16 07:44:17 +00:00
if isinstance(target, str):
2023-04-17 08:55:55 +00:00
target = Bot.FetchTarget.fetch_target(target)
if not target:
raise ValueError("Target not found")
2023-04-16 07:44:17 +00:00
if isinstance(msg, list):
msg = MessageChain(msg)
2023-09-01 14:38:32 +00:00
await target.send_direct_message(msg, disable_secret_check, allow_split_image)
2023-04-17 08:55:55 +00:00
@staticmethod
async def fetch_target(target: str):
return Bot.FetchTarget.fetch_target(target)
@staticmethod
2023-08-27 14:51:16 +00:00
async def get_enabled_this_module(module: str) -> List[FS]:
2023-04-17 08:55:55 +00:00
lst = BotDBUtil.TargetInfo.get_enabled_this(module)
fetched = []
for x in lst:
x = Bot.FetchTarget.fetch_target(x)
if isinstance(x, FetchedSession):
fetched.append(x)
return fetched
2023-08-29 14:08:47 +00:00
class Hook:
@staticmethod
2023-08-29 17:35:56 +00:00
async def trigger(module_or_hook_name: str, args):
hook_mode = False
if '.' in module_or_hook_name:
hook_mode = True
if not hook_mode:
if module_or_hook_name is not None:
modules = ModulesManager.modules
if module_or_hook_name in modules:
for hook in modules[module_or_hook_name].hooks_list.set:
2023-08-30 11:52:59 +00:00
await asyncio.create_task(hook.function(Bot.FetchTarget, ModuleHookContext(args)))
2023-08-29 17:35:56 +00:00
return
2023-08-29 17:35:56 +00:00
raise ValueError("Invalid module name")
else:
if module_or_hook_name is not None:
if module_or_hook_name in ModulesManager.modules_hooks:
2023-08-30 11:52:59 +00:00
await asyncio.create_task(ModulesManager.modules_hooks[module_or_hook_name](Bot.FetchTarget,
ModuleHookContext(
args)))
2023-08-29 17:35:56 +00:00
return
raise ValueError("Invalid hook name")
2023-08-29 14:08:47 +00:00
2023-08-27 14:51:16 +00:00
class FetchedSession(FS):
2023-09-01 14:38:32 +00:00
def __init__(self, target_from, target_id, sender_from=None, sender_id=None):
if sender_from is None:
sender_from = target_from
if sender_id is None:
sender_id = target_id
self.target = MsgInfo(target_id=f'{target_from}|{target_id}',
sender_id=f'{sender_from}|{sender_id}',
target_from=target_from,
sender_from=sender_from,
sender_name='',
client_name=Bot.client_name,
message_id=0,
reply_id=None)
self.session = Session(message=False, target=target_id, sender=sender_id)
2023-08-27 14:51:16 +00:00
self.parent = Bot.MessageSession(self.target, self.session)
2023-09-01 14:38:32 +00:00
if sender_id is not None:
self.parent.target.sender_info = BotDBUtil.SenderInfo(f'{sender_from}|{sender_id}')
2023-08-27 14:51:16 +00:00
Bot.FetchedSession = FetchedSession
base_superuser_list = Config("base_superuser")
if isinstance(base_superuser_list, str):
base_superuser_list = [base_superuser_list]