From 93fb98f7b585af25d8536e1bd951150b8f975f28 Mon Sep 17 00:00:00 2001 From: Pagliacii Date: Fri, 15 Dec 2023 21:39:21 +0800 Subject: [PATCH 1/3] feat: add --disable-command option to specify a command to disable --- README.md | 3 +++ handlers/__init__.py | 4 +++- tg.py | 14 +++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a18f87..a1b5bde 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ for yihong0618's channel: https://t.me/hyi0618 2. Get tg token, ask Google or ChatGPT, need get it from [BotFather](https://t.me/BotFather) 3. python tg.py ${tg_token} +> [!Note] +> If you don't want to use one of these command, you can use `--disable-command ` option to disable it. This option can be used multiple times. + ## Contribution diff --git a/handlers/__init__.py b/handlers/__init__.py index 8aaab27..c24b9f4 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -55,12 +55,14 @@ def wrap_handler(handler: T, bot: TeleBot) -> T: return update_wrapper(wrapper, handler) -def load_handlers(bot: TeleBot) -> None: +def load_handlers(bot: TeleBot, disable_commands: list[str]) -> None: # import all submodules this_path = Path(__file__).parent for child in this_path.iterdir(): if child.name.startswith("_"): continue + if child.stem in disable_commands: + continue module = importlib.import_module(f".{child.stem}", __package__) if hasattr(module, "register"): print(f"Loading {child.stem} handlers.") diff --git a/tg.py b/tg.py index 9aed42f..a8f77e9 100644 --- a/tg.py +++ b/tg.py @@ -9,12 +9,24 @@ def main(): # Init args parser = argparse.ArgumentParser() parser.add_argument("tg_token", help="tg token") + + # 'disable-command' option + # The action 'append' will allow multiple entries to be saved into a list + # The variable name is changed to 'disable_commands' + parser.add_argument( + "--disable-command", + action="append", + dest="disable_commands", + help="Specify a command to disable. Can be used multiple times.", + default=[], + ) + options = parser.parse_args() print("Arg parse done.") # Init bot bot = TeleBot(options.tg_token) - load_handlers(bot) + load_handlers(bot, options.disable_commands) print("Bot init done.") # Start bot From d410f48db67009650e1de1d1c314a3777b1581f2 Mon Sep 17 00:00:00 2001 From: Pagliacii Date: Sat, 16 Dec 2023 14:30:05 +0800 Subject: [PATCH 2/3] feat(argparse): show available commands as choices for disable_command option --- handlers/__init__.py | 10 ++++++++++ tg.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/handlers/__init__.py b/handlers/__init__.py index c24b9f4..3ea566c 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -80,3 +80,13 @@ def load_handlers(bot: TeleBot, disable_commands: list[str]) -> None: if all_commands: bot.set_my_commands(all_commands) print("Setting commands done.") + + +def available_commands() -> list[str]: + commands = [] + this_path = Path(__file__).parent + for child in this_path.iterdir(): + if child.name.startswith("_"): + continue + commands.append(child.stem) + return commands diff --git a/tg.py b/tg.py index a8f77e9..678a534 100644 --- a/tg.py +++ b/tg.py @@ -2,7 +2,7 @@ import argparse from telebot import TeleBot -from handlers import load_handlers +from handlers import available_commands, load_handlers def main(): @@ -19,6 +19,7 @@ def main(): dest="disable_commands", help="Specify a command to disable. Can be used multiple times.", default=[], + choices=available_commands(), ) options = parser.parse_args() From 73a9c20bc77d20256d840d4d80dd8f7f5c5ffe1f Mon Sep 17 00:00:00 2001 From: Pagliacii Date: Sat, 16 Dec 2023 14:37:41 +0800 Subject: [PATCH 3/3] chore: rename available_commands to list_available_commands --- handlers/__init__.py | 2 +- tg.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/handlers/__init__.py b/handlers/__init__.py index 3ea566c..b9ba4a3 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -82,7 +82,7 @@ def load_handlers(bot: TeleBot, disable_commands: list[str]) -> None: print("Setting commands done.") -def available_commands() -> list[str]: +def list_available_commands() -> list[str]: commands = [] this_path = Path(__file__).parent for child in this_path.iterdir(): diff --git a/tg.py b/tg.py index 678a534..a2fecc9 100644 --- a/tg.py +++ b/tg.py @@ -2,7 +2,7 @@ import argparse from telebot import TeleBot -from handlers import available_commands, load_handlers +from handlers import list_available_commands, load_handlers def main(): @@ -19,7 +19,7 @@ def main(): dest="disable_commands", help="Specify a command to disable. Can be used multiple times.", default=[], - choices=available_commands(), + choices=list_available_commands(), ) options = parser.parse_args()