PaiGram/utils/plugins/manager.py

45 lines
1.2 KiB
Python
Raw Normal View History

2022-08-11 13:18:12 +00:00
from typing import List
2022-07-26 10:07:31 +00:00
from telegram.ext import Application
from logger import Log
2022-08-11 13:18:12 +00:00
from utils.manager import ModulesManager
2022-07-26 10:07:31 +00:00
PluginsClass: List[object] = []
def listener_plugins_class():
"""监听插件
:return: None
"""
def decorator(func: object):
PluginsClass.append(func)
return func
return decorator
2022-08-11 13:18:12 +00:00
class PluginsManager(ModulesManager):
2022-07-26 10:07:31 +00:00
2022-08-11 13:18:12 +00:00
def __init__(self):
super().__init__()
self.manager_name = "插件管理器"
2022-07-26 10:07:31 +00:00
@staticmethod
def add_handler(application: Application):
for func in PluginsClass:
if callable(func):
try:
handlers_list = func.create_handlers()
for handler in handlers_list:
application.add_handler(handler)
except AttributeError as exc:
if "create_handlers" in str(exc):
Log.error("创建 handlers 函数未找到", exc)
Log.error("初始化Class失败", exc)
except BaseException as exc:
Log.error("初始化Class失败", exc)
finally:
pass