コード例 #1
0
 def _get_plugin_callbacks(self, name: str, path: str) -> List[Callback]:
     _module: ModuleSpec = importlib.util.spec_from_file_location(
         name, path)
     loader: SourceFileLoader = _module.loader
     module = loader.load_module()
     callbacks = []
     with open(path, encoding='utf-8') as f:
         tree = ast.parse(f.read())
         for item in tree.body:
             if isinstance(item, ast.ClassDef) and not item.name.startswith(
                     '_') and item.bases:
                 plugin = getattr(module, item.name)
                 if issubclass(plugin, KantekPlugin):
                     for method in item.body:
                         if isinstance(method, ast.AsyncFunctionDef):
                             func = getattr(plugin, method.name)
                             if events.is_handler(func):
                                 is_private = self.__is_private(
                                     self.__get_event_decorator_keywords(
                                         method))
                                 callbacks.append(
                                     Callback(method.name, func.__doc__,
                                              func, is_private))
     return callbacks
コード例 #2
0
### LOG IN TO TELEGRAM ##
client = TelegramClient(path.join(script_dir, session_name), api_id, api_hash)

### IMPORT PLUGINS ###
plugindir = "plugins"  # Change plugin path here
pluginfiles = listdir(plugindir)
plugin_dict = {}

for pluginfile in pluginfiles:
    if re.search(r".+plugin\.py$", pluginfile):
        plugin_name = pluginfile[:-3]
        plugin_shortname = plugin_name[:-7]
        plugin = import_module(f"plugins.{plugin_name}")
        plugin_dict[plugin_shortname] = plugin.__doc__
        for name, handler in getmembers(plugin):
            if events.is_handler(handler):
                client.add_event_handler(handler)

# ## RELOAD PLUGINS ###
# @client.on(events.NewMessage(pattern=r"^/reload$"))
# async def reload_plugins(event):
#     for name, module in sys.modules.items():
#         print(name)
#         print(module)
#         reload(module)

### HELP! ###
plugin_list = "`\n• `".join(plugin_dict)
help_message = f"""**List of functions:**
• `{plugin_list}`