1- import os
2- import importlib .util
3- import inspect
4- from pathlib import Path
1+ from modules .apis import *
2+ from modules .decorators import command , callback
53
6- from telegram . ext import Application
4+ from telegram import Update
75
8- def load_modules (app : Application ):
9- for py_file in Path ("modules" ).glob ("*.py" ):
10- module_name = py_file .stem
11- if module_name .startswith ('__' ):
12- continue
13-
14- spec = importlib .util .spec_from_file_location (module_name , py_file )
15- module = importlib .util .module_from_spec (spec )
16- spec .loader .exec_module (module )
17-
18- functions = [name for name , obj in inspect .getmembers (module )
19- if inspect .isfunction (obj ) and not name .startswith ('_' )]
20-
21- print (f"Module: { module_name } " )
22-
23- decorator_groups = {}
24-
25- for func_name in functions :
26- func = getattr (module , func_name )
27-
28- # 데코레이터 이름 찾기
29- if hasattr (func , '__wrapped__' ):
30- # 데코레이터가 적용된 함수
31- decorator_name = func .__name__ if hasattr (func , '__name__' ) else 'unknown'
32- # 실제 데코레이터 이름을 찾기 위해 원본 함수까지 추적
33- original_func = func
34- while hasattr (original_func , '__wrapped__' ):
35- original_func = original_func .__wrapped__
36- decorator_name = f"decorated_{ original_func .__name__ } "
37- else :
38- decorator_name = "no_decorator"
39-
40- if decorator_name not in decorator_groups :
41- decorator_groups [decorator_name ] = []
42- decorator_groups [decorator_name ].append (func_name )
43-
44- # 데코레이터별로 함수 실행
45- for decorator_name , func_names in decorator_groups .items ():
46- print (f"\n [{ decorator_name } ]" )
47- print (f"Functions: { func_names } " )
48-
49- for func_name in func_names :
50- func = getattr (module , func_name )
51- sig = inspect .signature (func )
52-
53- if len (sig .parameters ) == 0 :
54- if inspect .iscoroutinefunction (func ):
55- import asyncio
56- result = asyncio .run (func ())
57- print (f"{ func_name } () -> { result } " )
58- else :
59- result = func ()
60- print (f"{ func_name } () -> { result } " )
61- else :
62- try :
63- if inspect .iscoroutinefunction (func ):
64- import asyncio
65- result = asyncio .run (func ())
66- print (f"{ func_name } () -> { result } " )
67- else :
68- result = func ()
69- print (f"{ func_name } () -> { result } " )
70- except :
71- print (f"{ func_name } () -> needs parameters" )
72-
73- print ("=" * 50 )
74-
75- load_and_execute_modules ()
6+ @command ("test" )
7+ async def test_command (update : Update ):
8+ print ("Test command executed" )
9+ await update .message .reply_text ("This is a test command." )
0 commit comments