From 96efe41c4fbf5325806133bd88ed2b258233ea17 Mon Sep 17 00:00:00 2001 From: Kwong Tung Nan Date: Sun, 30 Jun 2024 16:03:12 +0800 Subject: [PATCH] fix(tg): Incorrect logic on bot command modifications Signed-off-by: Kwong Tung Nan --- telegram_provider/apps.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/telegram_provider/apps.py b/telegram_provider/apps.py index 12d88b0..9a88aca 100644 --- a/telegram_provider/apps.py +++ b/telegram_provider/apps.py @@ -112,23 +112,27 @@ async def startup(self, **_): for k, v in handlers_mapping.items(): handlers_dict[k]["handler"] = v - # TODO: Add logic for when a new command is added + has_changes = False + existing_commands = set() for bot_command in await ptb_application.bot.get_my_commands(): command = bot_command.command + existing_commands.add(command) if ( command not in handlers_dict.keys() or handlers_dict[command]["description"] != bot_command.description ): - logger.info( - "Modification to bot command list required, updating now..." - ) - await ptb_application.bot.set_my_commands( - [ - (command, elem["description"]) - for command, elem in handlers_dict.items() - ] - ) - break + has_changes = True + + if has_changes or set(handlers_dict.keys()).symmetric_difference( + existing_commands + ): + logger.info("Modification to bot command list required, updating now...") + await ptb_application.bot.set_my_commands( + [ + (command, elem["description"]) + for command, elem in handlers_dict.items() + ] + ) ptb_application.add_handlers( [