2022-06-27 13:42:24 +00:00
|
|
|
import asyncio
|
|
|
|
|
2022-07-05 06:56:00 +00:00
|
|
|
from pyrogram import StopPropagation
|
|
|
|
|
|
|
|
from pagermaid import hook_functions, logs
|
|
|
|
from pagermaid.single_utils import Message
|
2022-06-27 13:42:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Hook:
|
|
|
|
@staticmethod
|
|
|
|
def on_startup():
|
|
|
|
"""
|
|
|
|
注册一个启动钩子
|
|
|
|
"""
|
|
|
|
def decorator(function):
|
2022-07-05 06:56:00 +00:00
|
|
|
hook_functions["startup"].add(function)
|
2022-06-27 13:42:24 +00:00
|
|
|
return function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def on_shutdown():
|
|
|
|
"""
|
|
|
|
注册一个关闭钩子
|
|
|
|
"""
|
|
|
|
def decorator(function):
|
2022-07-05 06:56:00 +00:00
|
|
|
hook_functions["shutdown"].add(function)
|
2022-06-27 13:42:24 +00:00
|
|
|
return function
|
|
|
|
return decorator
|
|
|
|
|
2022-07-05 06:56:00 +00:00
|
|
|
@staticmethod
|
|
|
|
def command_preprocessor():
|
|
|
|
"""
|
|
|
|
注册一个命令预处理钩子
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(function):
|
|
|
|
hook_functions["command_pre"].add(function)
|
|
|
|
return function
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def command_postprocessor():
|
|
|
|
"""
|
|
|
|
注册一个命令后处理钩子
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(function):
|
|
|
|
hook_functions["command_post"].add(function)
|
|
|
|
return function
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2022-07-08 14:26:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def process_error():
|
|
|
|
"""
|
|
|
|
注册一个错误处理钩子
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(function):
|
|
|
|
hook_functions["process_error"].add(function)
|
|
|
|
return function
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2022-06-27 13:42:24 +00:00
|
|
|
@staticmethod
|
|
|
|
async def startup():
|
2022-07-05 06:56:00 +00:00
|
|
|
if cors := [startup() for startup in hook_functions["startup"]]:
|
2022-06-27 13:42:24 +00:00
|
|
|
try:
|
|
|
|
await asyncio.gather(*cors)
|
|
|
|
except Exception as exception:
|
|
|
|
logs.info(f"[startup]: {type(exception)}: {exception}")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def shutdown():
|
2022-07-05 06:56:00 +00:00
|
|
|
if cors := [shutdown() for shutdown in hook_functions["shutdown"]]:
|
2022-06-27 13:42:24 +00:00
|
|
|
try:
|
|
|
|
await asyncio.gather(*cors)
|
|
|
|
except Exception as exception:
|
|
|
|
logs.info(f"[shutdown]: {type(exception)}: {exception}")
|
2022-07-05 06:56:00 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def command_pre(message: Message):
|
|
|
|
if cors := [pre(message) for pre in hook_functions["command_pre"]]: # noqa
|
|
|
|
try:
|
|
|
|
await asyncio.gather(*cors)
|
|
|
|
except StopPropagation as e:
|
|
|
|
raise StopPropagation from e
|
|
|
|
except Exception as exception:
|
|
|
|
logs.info(f"[command_pre]: {type(exception)}: {exception}")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def command_post(message: Message):
|
|
|
|
if cors := [post(message) for post in hook_functions["command_post"]]: # noqa
|
|
|
|
try:
|
|
|
|
await asyncio.gather(*cors)
|
|
|
|
except StopPropagation as e:
|
|
|
|
raise StopPropagation from e
|
|
|
|
except Exception as exception:
|
|
|
|
logs.info(f"[command_post]: {type(exception)}: {exception}")
|
2022-07-08 14:26:36 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def process_error_exec(message: Message, exc_info: BaseException, exc_format: str):
|
|
|
|
if cors := [error(message, exc_info, exc_format) for error in hook_functions["process_error"]]: # noqa
|
|
|
|
try:
|
|
|
|
await asyncio.gather(*cors)
|
|
|
|
except StopPropagation as e:
|
|
|
|
raise StopPropagation from e
|
|
|
|
except Exception as exception:
|
|
|
|
logs.info(f"[process_error]: {type(exception)}: {exception}")
|