import{_ as n,o as s,c as a,d as e}from"./app-CLi4lRj1.js";const t={},o=e(`

func get_bot() -> LiteyukiBot

Description: 获取轻雪实例

Return: LiteyukiBot: 当前的轻雪实例

Source code
def get_bot() -> LiteyukiBot:
    """
    获取轻雪实例

    Returns:
        LiteyukiBot: 当前的轻雪实例
    """
    if IS_MAIN_PROCESS:
        if _BOT_INSTANCE is None:
            raise RuntimeError('Liteyuki instance not initialized.')
        return _BOT_INSTANCE
    else:
        raise RuntimeError("Can't get bot instance in sub process.")

func get_config(key: str = None) -> Any

Description: 获取配置

Return: Any: 配置值

Arguments:

Source code
def get_config(key: str, default: Any=None) -> Any:
    """
    获取配置
    Args:
        key: 配置键
        default: 默认值

    Returns:
        Any: 配置值
    """
    return get_bot().config.get(key, default)

func get_config_with_compat(key: str = None) -> Any

Description: 获取配置,兼容旧版本

Return: Any: 配置值

Arguments:

Source code
def get_config_with_compat(key: str, compat_keys: tuple[str], default: Any=None) -> Any:
    """
    获取配置,兼容旧版本
    Args:
        key: 配置键
        compat_keys: 兼容键
        default: 默认值

    Returns:
        Any: 配置值
    """
    if key in get_bot().config:
        return get_bot().config[key]
    for compat_key in compat_keys:
        if compat_key in get_bot().config:
            logger.warning(f'Config key "{compat_key}" will be deprecated, use "{key}" instead.')
            return get_bot().config[compat_key]
    return default
Source code
def print_logo():
    print('\\x1b[34m' + '\\n     __        ______  ________  ________  __      __  __    __  __    __  ______ \\n    /  |      /      |/        |/        |/  \\\\    /  |/  |  /  |/  |  /  |/      |\\n    $$ |      $$$$$$/ $$$$$$$$/ $$$$$$$$/ $$  \\\\  /$$/ $$ |  $$ |$$ | /$$/ $$$$$$/ \\n    $$ |        $$ |     $$ |   $$ |__     $$  \\\\/$$/  $$ |  $$ |$$ |/$$/    $$ |  \\n    $$ |        $$ |     $$ |   $$    |     $$  $$/   $$ |  $$ |$$  $$<     $$ |  \\n    $$ |        $$ |     $$ |   $$$$$/       $$$$/    $$ |  $$ |$$$$$  \\\\    $$ |  \\n    $$ |_____  _$$ |_    $$ |   $$ |_____     $$ |    $$ \\\\__$$ |$$ |$$  \\\\  _$$ |_ \\n    $$       |/ $$   |   $$ |   $$       |    $$ |    $$    $$/ $$ | $$  |/ $$   |\\n    $$$$$$$$/ $$$$$$/    $$/    $$$$$$$$/     $$/      $$$$$$/  $$/   $$/ $$$$$$/ \\n                ' + '\\x1b[0m')

class LiteyukiBot

method __init__(self) -> None

Description: 初始化轻雪实例

Arguments:

Source code
def __init__(self, *args, **kwargs) -> None:
    """
        初始化轻雪实例
        Args:
            *args:
            **kwargs: 配置

        """
    '常规操作'
    print_logo()
    global _BOT_INSTANCE
    _BOT_INSTANCE = self
    '配置'
    self.config: dict[str, Any] = kwargs
    '初始化'
    self.init(**self.config)
    logger.info('Liteyuki is initializing...')
    '生命周期管理'
    self.lifespan = Lifespan()
    self.process_manager: ProcessManager = ProcessManager(lifespan=self.lifespan)
    '事件循环'
    self.loop = asyncio.new_event_loop()
    asyncio.set_event_loop(self.loop)
    self.stop_event = threading.Event()
    self.call_restart_count = 0
    '加载插件加载器'
    load_plugin('liteyuki.plugins.plugin_loader')
    '信号处理'
    signal.signal(signal.SIGINT, self._handle_exit)
    signal.signal(signal.SIGTERM, self._handle_exit)
    atexit.register(self.process_manager.terminate_all)

async method _run(self)

Description: 启动逻辑

Source code
async def _run(self):
    """
        启动逻辑
        """
    await self.lifespan.before_start()
    await self.process_manager.start_all()
    await self.lifespan.after_start()
    await self.keep_alive()

method run(self)

Description: 外部启动接口

Source code
def run(self):
    """
        外部启动接口
        """
    try:
        asyncio.run(self._run())
    except KeyboardInterrupt:
        logger.info('Liteyuki is stopping...')

async method keep_alive(self)

Description: 保持轻雪运行

Source code
async def keep_alive(self):
    """
        保持轻雪运行
        Returns:

        """
    try:
        while not self.stop_event.is_set():
            time.sleep(0.5)
    except KeyboardInterrupt:
        logger.info('Liteyuki is stopping...')
        self.stop()

method _handle_exit(self, signum, frame)

Description: 信号处理

Arguments:

Source code
def _handle_exit(self, signum, frame):
    """
        信号处理
        Args:
            signum:
            frame:

        Returns:

        """
    logger.info('Received signal, stopping all processes.')
    self.stop()
    sys.exit(0)

method restart(self, delay: int = 0)

Description: 重启轻雪本体

Source code
def restart(self, delay: int=0):
    """
        重启轻雪本体
        Returns:

        """
    if self.call_restart_count < 1:
        executable = sys.executable
        args = sys.argv
        logger.info('Restarting LiteyukiBot...')
        time.sleep(delay)
        if platform.system() == 'Windows':
            cmd = 'start'
        elif platform.system() == 'Linux':
            cmd = 'nohup'
        elif platform.system() == 'Darwin':
            cmd = 'open'
        else:
            cmd = 'nohup'
        self.process_manager.terminate_all()
        threading.Thread(target=os.system, args=(f"{cmd} {executable} {' '.join(args)}",)).start()
        sys.exit(0)
    self.call_restart_count += 1

method restart_process(self, name: Optional[str] = None)

Description: 停止轻雪

Arguments:

Source code
def restart_process(self, name: Optional[str]=None):
    """
        停止轻雪
        Args:
            name: 进程名称, 默认为None, 所有进程
        Returns:
        """
    if name is not None:
        chan_active = get_channel(f'{name}-active')
        chan_active.send(1)
    else:
        for process_name in self.process_manager.processes:
            chan_active = get_channel(f'{process_name}-active')
            chan_active.send(1)

method init(self)

Description: 初始化轻雪, 自动调用

Source code
def init(self, *args, **kwargs):
    """
        初始化轻雪, 自动调用
        Returns:

        """
    self.init_logger()

method init_logger(self)

Source code
def init_logger(self):
    init_log(config=self.config)

method stop(self)

Description: 停止轻雪

Source code
def stop(self):
    """
        停止轻雪
        Returns:

        """
    self.stop_event.set()
    self.loop.stop()

method on_before_start(self, func: LIFESPAN_FUNC)

Description: 注册启动前的函数

Arguments:

Source code
def on_before_start(self, func: LIFESPAN_FUNC):
    """
        注册启动前的函数
        Args:
            func:

        Returns:

        """
    return self.lifespan.on_before_start(func)

method on_after_start(self, func: LIFESPAN_FUNC)

Description: 注册启动后的函数

Arguments:

Source code
def on_after_start(self, func: LIFESPAN_FUNC):
    """
        注册启动后的函数
        Args:
            func:

        Returns:

        """
    return self.lifespan.on_after_start(func)

method on_after_shutdown(self, func: LIFESPAN_FUNC)

Description: 注册停止后的函数:未实现

Arguments:

Source code
def on_after_shutdown(self, func: LIFESPAN_FUNC):
    """
        注册停止后的函数:未实现
        Args:
            func:

        Returns:

        """
    return self.lifespan.on_after_shutdown(func)

method on_before_process_shutdown(self, func: LIFESPAN_FUNC)

Description: 注册进程停止前的函数,为子进程停止时调用

Arguments:

Source code
def on_before_process_shutdown(self, func: LIFESPAN_FUNC):
    """
        注册进程停止前的函数,为子进程停止时调用
        Args:
            func:

        Returns:

        """
    return self.lifespan.on_before_process_shutdown(func)

method on_before_process_restart(self, func: LIFESPAN_FUNC)

Description: 注册进程重启前的函数,为子进程重启时调用

Arguments:

Source code
def on_before_process_restart(self, func: LIFESPAN_FUNC):
    """
        注册进程重启前的函数,为子进程重启时调用
        Args:
            func:

        Returns:

        """
    return self.lifespan.on_before_process_restart(func)

method on_after_restart(self, func: LIFESPAN_FUNC)

Description: 注册重启后的函数:未实现

Arguments:

Source code
def on_after_restart(self, func: LIFESPAN_FUNC):
    """
        注册重启后的函数:未实现
        Args:
            func:

        Returns:

        """
    return self.lifespan.on_after_restart(func)

var _BOT_INSTANCE = NO_DEFAULT

`,86),p=[o];function i(l,c){return s(),a("div",null,p)}const r=n(t,[["render",i],["__file","index.html.vue"]]),d=JSON.parse('{"path":"/en/api/bot/","title":"liteyuki.bot","lang":"en-US","frontmatter":{"title":"liteyuki.bot","description":"func get_bot() -> LiteyukiBot Description: 获取轻雪实例 Return: LiteyukiBot: 当前的轻雪实例 Source code func get_config(key: str = None) -> Any Description: 获取配置 Return: Any: 配置值 Arguments: ...","head":[["link",{"rel":"alternate","hreflang":"zh-cn","href":"https://vuepress-theme-hope-docs-demo.netlify.app/api/bot/"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/en/api/bot/"}],["meta",{"property":"og:site_name","content":"LiteyukiBot"}],["meta",{"property":"og:title","content":"liteyuki.bot"}],["meta",{"property":"og:description","content":"func get_bot() -> LiteyukiBot Description: 获取轻雪实例 Return: LiteyukiBot: 当前的轻雪实例 Source code func get_config(key: str = None) -> Any Description: 获取配置 Return: Any: 配置值 Arguments: ..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:locale:alternate","content":"zh-CN"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.bot\\",\\"image\\":[\\"\\"],\\"dateModified\\":null,\\"author\\":[]}"]]},"headers":[{"level":3,"title":"func get_bot() -> LiteyukiBot","slug":"func-get-bot-liteyukibot","link":"#func-get-bot-liteyukibot","children":[]},{"level":3,"title":"func get_config(key: str = None) -> Any","slug":"func-get-config-key-str-none-any","link":"#func-get-config-key-str-none-any","children":[]},{"level":3,"title":"func get_config_with_compat(key: str = None) -> Any","slug":"func-get-config-with-compat-key-str-none-any","link":"#func-get-config-with-compat-key-str-none-any","children":[]},{"level":3,"title":"func print_logo()","slug":"func-print-logo","link":"#func-print-logo","children":[]},{"level":3,"title":"class LiteyukiBot","slug":"class-liteyukibot","link":"#class-liteyukibot","children":[]},{"level":3,"title":"method __init__(self) -> None","slug":"method-init-self-none","link":"#method-init-self-none","children":[]},{"level":3,"title":"async method _run(self)","slug":"async-method-run-self","link":"#async-method-run-self","children":[]},{"level":3,"title":"method run(self)","slug":"method-run-self","link":"#method-run-self","children":[]},{"level":3,"title":"async method keep_alive(self)","slug":"async-method-keep-alive-self","link":"#async-method-keep-alive-self","children":[]},{"level":3,"title":"method _handle_exit(self, signum, frame)","slug":"method-handle-exit-self-signum-frame","link":"#method-handle-exit-self-signum-frame","children":[]},{"level":3,"title":"method restart(self, delay: int = 0)","slug":"method-restart-self-delay-int-0","link":"#method-restart-self-delay-int-0","children":[]},{"level":3,"title":"method restart_process(self, name: Optional[str] = None)","slug":"method-restart-process-self-name-optional-str-none","link":"#method-restart-process-self-name-optional-str-none","children":[]},{"level":3,"title":"method init(self)","slug":"method-init-self","link":"#method-init-self","children":[]},{"level":3,"title":"method init_logger(self)","slug":"method-init-logger-self","link":"#method-init-logger-self","children":[]},{"level":3,"title":"method stop(self)","slug":"method-stop-self","link":"#method-stop-self","children":[]},{"level":3,"title":"method on_before_start(self, func: LIFESPAN_FUNC)","slug":"method-on-before-start-self-func-lifespan-func","link":"#method-on-before-start-self-func-lifespan-func","children":[]},{"level":3,"title":"method on_after_start(self, func: LIFESPAN_FUNC)","slug":"method-on-after-start-self-func-lifespan-func","link":"#method-on-after-start-self-func-lifespan-func","children":[]},{"level":3,"title":"method on_after_shutdown(self, func: LIFESPAN_FUNC)","slug":"method-on-after-shutdown-self-func-lifespan-func","link":"#method-on-after-shutdown-self-func-lifespan-func","children":[]},{"level":3,"title":"method on_before_process_shutdown(self, func: LIFESPAN_FUNC)","slug":"method-on-before-process-shutdown-self-func-lifespan-func","link":"#method-on-before-process-shutdown-self-func-lifespan-func","children":[]},{"level":3,"title":"method on_before_process_restart(self, func: LIFESPAN_FUNC)","slug":"method-on-before-process-restart-self-func-lifespan-func","link":"#method-on-before-process-restart-self-func-lifespan-func","children":[]},{"level":3,"title":"method on_after_restart(self, func: LIFESPAN_FUNC)","slug":"method-on-after-restart-self-func-lifespan-func","link":"#method-on-after-restart-self-func-lifespan-func","children":[]},{"level":3,"title":"var _BOT_INSTANCE = NO_DEFAULT","slug":"var-bot-instance-no-default","link":"#var-bot-instance-no-default","children":[]}],"git":{"createdTime":null,"updatedTime":null,"contributors":[]},"readingTime":{"minutes":3.96,"words":1188},"filePathRelative":"en/api/bot/README.md","autoDesc":true}');export{r as comp,d as data};