-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
45 lines (33 loc) · 1.32 KB
/
Copy pathutils.py
File metadata and controls
45 lines (33 loc) · 1.32 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
import importlib
import inspect
import logging
import os
from telegram.ext import CommandHandler
from commands.abstract import AbstractCommand
logger = logging.getLogger(__name__)
async def echo_handler(update, context):
if update.message.text:
text = \
f"What does \"{update.message.text}\" mean?\nTry to type /help."
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=text
)
def autodiscovery(application):
"""
Discover all available commands
:param application: application
"""
commands_module_name = "commands"
directory = os.fsencode(commands_module_name)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith('.py'):
base_module_name = os.path.splitext(filename)[0]
full_module_name = '.'.join((commands_module_name, base_module_name))
# Command importing..
module = importlib.import_module(full_module_name)
# Iterate over classes and register discovered commands
for _, klass in inspect.getmembers(module, inspect.isclass):
if issubclass(klass, AbstractCommand) and klass is not AbstractCommand:
application.add_handler(CommandHandler(klass.COMMAND, klass().handler))