-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
52 lines (38 loc) · 1.83 KB
/
loader.py
File metadata and controls
52 lines (38 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from pathlib import Path
import importlib.util
import inspect
from telegram.ext import Application, CommandHandler, CallbackQueryHandler
def load_modules(app: Application):
for py_file in Path("modules").glob("*.py"):
module_name = py_file.stem
if module_name.startswith("__"):
continue
spec = importlib.util.spec_from_file_location(module_name, py_file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
functions = []
for name, obj in inspect.getmembers(module):
if inspect.isfunction(obj) and not name.startswith("_"):
functions.append(name)
decorator_groups = {"command": [], "callback": []}
for func_name in functions:
func = getattr(module, func_name)
if hasattr(func, "decorator"):
decorator_name = func.decorator
else:
continue
if decorator_name not in decorator_groups:
continue
decorator_groups[decorator_name].append(func_name)
print(decorator_groups)
for decorator_name, func_names in decorator_groups.items():
if decorator_name == "command":
for func_name in func_names:
func = getattr(module, func_name)
app.add_handler(CommandHandler(func.command, func))
print(f"Command handler for {func.command} added from module {module_name}")
elif decorator_name == "callback":
for func_name in func_names:
func = getattr(module, func_name)
app.add_handler(CallbackQueryHandler(func, pattern=f"^{func.callback_data}$"))
print(f"Callback handler for {func.callback_data} added from module {module_name}")