Merge pull request #44 from F4ria/load_priority

This commit is contained in:
yihong 2024-07-10 07:28:24 +08:00 committed by GitHub
commit 23ad429cd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -22,6 +22,8 @@ markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
T = TypeVar("T", bound=Callable)
DEFAULT_LOAD_PRIORITY = 10
BOT_MESSAGE_LENGTH = 4000
REPLY_MESSAGE_CACHE = ExpiringDict(max_len=1000, max_age_seconds=300)
@ -146,12 +148,18 @@ def wrap_handler(handler: T, bot: TeleBot) -> T:
def load_handlers(bot: TeleBot, disable_commands: list[str]) -> None:
# import all submodules
modules_with_priority = []
for name in list_available_commands():
if name in disable_commands:
continue
module = importlib.import_module(f".{name}", __package__)
load_priority = getattr(module, "load_priority", DEFAULT_LOAD_PRIORITY)
modules_with_priority.append((module, name, load_priority))
modules_with_priority.sort(key=lambda x: x[-1])
for module, name, priority in modules_with_priority:
if hasattr(module, "register"):
print(f"Loading {name} handlers.")
print(f"Loading {name} handlers with priority {priority}.")
module.register(bot)
print("Loading handlers done.")

View File

@ -13,6 +13,9 @@ from . import *
from telegramify_markdown.customize import markdown_symbol
# Define the load priority, lower numbers have higher priority
load_priority = 1000
# If you want, Customizing the head level 1 symbol
markdown_symbol.head_level_1 = "📌"
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
@ -196,7 +199,7 @@ def latest_handle_messages(message: Message, bot: TeleBot):
elif message.text.startswith(
(
"md",
"chatgpt",
"gpt",
"gemini",
"qwen",
"map",