2024-08-08 18:06:03 +08:00
|
|
|
|
import threading
|
2024-07-27 10:12:45 +08:00
|
|
|
|
import time
|
2024-07-31 02:28:25 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
from typing import Any, Optional
|
|
|
|
|
from multiprocessing import freeze_support
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
2024-07-31 02:28:25 +08:00
|
|
|
|
from liteyuki.bot.lifespan import (LIFESPAN_FUNC, Lifespan)
|
|
|
|
|
from liteyuki.comm.channel import Channel
|
|
|
|
|
from liteyuki.core import IS_MAIN_PROCESS
|
|
|
|
|
from liteyuki.core.manager import ProcessManager
|
|
|
|
|
from liteyuki.core.spawn_process import mb_run, nb_run
|
|
|
|
|
from liteyuki.log import init_log, logger
|
|
|
|
|
from liteyuki.plugin import load_plugins
|
2024-07-27 10:12:45 +08:00
|
|
|
|
from liteyuki.utils import run_coroutine
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"LiteyukiBot",
|
|
|
|
|
"get_bot"
|
|
|
|
|
]
|
|
|
|
|
|
2024-07-27 10:12:45 +08:00
|
|
|
|
"""是否为主进程"""
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LiteyukiBot:
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
global _BOT_INSTANCE
|
|
|
|
|
_BOT_INSTANCE = self # 引用
|
2024-07-31 02:28:25 +08:00
|
|
|
|
self.config: dict[str, Any] = kwargs
|
|
|
|
|
self.init(**self.config) # 初始化
|
|
|
|
|
|
|
|
|
|
self.lifespan: Lifespan = Lifespan()
|
|
|
|
|
self.chan = Channel() # 进程通信通道
|
2024-08-08 18:06:03 +08:00
|
|
|
|
self.pm: ProcessManager = ProcessManager(bot=self, chan=self.chan)
|
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
|
self.loop_thread = threading.Thread(target=self.loop.run_forever, daemon=True)
|
2024-07-31 02:28:25 +08:00
|
|
|
|
|
|
|
|
|
print("\033[34m" + r"""
|
2024-07-24 02:36:46 +08:00
|
|
|
|
__ ______ ________ ________ __ __ __ __ __ __ ______
|
|
|
|
|
/ | / |/ |/ |/ \ / |/ | / |/ | / |/ |
|
|
|
|
|
$$ | $$$$$$/ $$$$$$$$/ $$$$$$$$/ $$ \ /$$/ $$ | $$ |$$ | /$$/ $$$$$$/
|
|
|
|
|
$$ | $$ | $$ | $$ |__ $$ \/$$/ $$ | $$ |$$ |/$$/ $$ |
|
|
|
|
|
$$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |$$ $$< $$ |
|
|
|
|
|
$$ | $$ | $$ | $$$$$/ $$$$/ $$ | $$ |$$$$$ \ $$ |
|
|
|
|
|
$$ |_____ _$$ |_ $$ | $$ |_____ $$ | $$ \__$$ |$$ |$$ \ _$$ |_
|
|
|
|
|
$$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ |
|
|
|
|
|
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
|
|
|
|
""" + "\033[0m")
|
|
|
|
|
|
2024-07-31 02:28:25 +08:00
|
|
|
|
def run(self):
|
2024-08-08 18:06:03 +08:00
|
|
|
|
load_plugins("liteyuki/plugins") # 加载轻雪插件
|
|
|
|
|
|
|
|
|
|
self.loop_thread.start() # 启动事件循环
|
|
|
|
|
asyncio.run(self.lifespan.before_start()) # 启动前钩子
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
2024-07-31 02:28:25 +08:00
|
|
|
|
self.pm.add_target("nonebot", nb_run, **self.config)
|
|
|
|
|
self.pm.start("nonebot")
|
|
|
|
|
|
2024-07-31 02:35:05 +08:00
|
|
|
|
self.pm.add_target("melobot", mb_run, **self.config)
|
|
|
|
|
self.pm.start("melobot")
|
|
|
|
|
|
2024-08-08 18:06:03 +08:00
|
|
|
|
asyncio.run(self.lifespan.after_start()) # 启动后钩子
|
2024-07-31 02:28:25 +08:00
|
|
|
|
|
|
|
|
|
def restart(self, name: Optional[str] = None):
|
2024-07-24 02:36:46 +08:00
|
|
|
|
"""
|
|
|
|
|
停止轻雪
|
2024-07-31 02:28:25 +08:00
|
|
|
|
Args:
|
|
|
|
|
name: 进程名称, 默认为None, 所有进程
|
2024-07-24 02:36:46 +08:00
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
logger.info("Stopping LiteyukiBot...")
|
2024-08-08 18:06:03 +08:00
|
|
|
|
|
|
|
|
|
self.loop.create_task(self.lifespan.before_shutdown()) # 重启前钩子
|
|
|
|
|
self.loop.create_task(self.lifespan.before_shutdown()) # 停止前钩子
|
|
|
|
|
|
2024-07-31 02:28:25 +08:00
|
|
|
|
if name:
|
|
|
|
|
self.chan.send(1, name)
|
|
|
|
|
else:
|
2024-08-08 18:06:03 +08:00
|
|
|
|
for name in self.pm.targets:
|
2024-07-31 02:28:25 +08:00
|
|
|
|
self.chan.send(1, name)
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
|
|
|
|
def init(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
初始化轻雪, 自动调用
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
self.init_config()
|
|
|
|
|
self.init_logger()
|
|
|
|
|
|
|
|
|
|
def init_logger(self):
|
2024-07-31 02:28:25 +08:00
|
|
|
|
# 修改nonebot的日志配置
|
2024-07-27 10:12:45 +08:00
|
|
|
|
init_log(config=self.config)
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
|
|
|
|
def init_config(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def on_before_start(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
|
|
|
|
注册启动前的函数
|
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return self.lifespan.on_before_start(func)
|
|
|
|
|
|
|
|
|
|
def on_after_start(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
|
|
|
|
注册启动后的函数
|
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return self.lifespan.on_after_start(func)
|
|
|
|
|
|
|
|
|
|
def on_before_shutdown(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
2024-08-08 18:06:03 +08:00
|
|
|
|
注册停止前的函数,为子进程停止时调用
|
2024-07-24 02:36:46 +08:00
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return self.lifespan.on_before_shutdown(func)
|
|
|
|
|
|
|
|
|
|
def on_after_shutdown(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
|
|
|
|
注册停止后的函数:未实现
|
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return self.lifespan.on_after_shutdown(func)
|
|
|
|
|
|
|
|
|
|
def on_before_restart(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
2024-08-08 18:06:03 +08:00
|
|
|
|
注册重启前的函数,为子进程重启时调用
|
2024-07-24 02:36:46 +08:00
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return self.lifespan.on_before_restart(func)
|
|
|
|
|
|
|
|
|
|
def on_after_restart(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
|
|
|
|
注册重启后的函数:未实现
|
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return self.lifespan.on_after_restart(func)
|
|
|
|
|
|
|
|
|
|
def on_after_nonebot_init(self, func: LIFESPAN_FUNC):
|
|
|
|
|
"""
|
|
|
|
|
注册nonebot初始化后的函数
|
|
|
|
|
Args:
|
|
|
|
|
func:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return self.lifespan.on_after_nonebot_init(func)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_BOT_INSTANCE: Optional[LiteyukiBot] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_bot() -> Optional[LiteyukiBot]:
|
|
|
|
|
"""
|
|
|
|
|
获取轻雪实例
|
|
|
|
|
Returns:
|
|
|
|
|
LiteyukiBot: 当前的轻雪实例
|
|
|
|
|
"""
|
2024-07-31 02:28:25 +08:00
|
|
|
|
if IS_MAIN_PROCESS:
|
|
|
|
|
return _BOT_INSTANCE
|
|
|
|
|
else:
|
|
|
|
|
# 从多进程上下文中获取
|
|
|
|
|
pass
|