From 39f1085f078865a6d3bc48a05b6ab923eb9ebcae Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Tue, 20 Sep 2022 16:47:03 +0200 Subject: Add support for removing command listeners --- include/tgbot/EventBroadcaster.h | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 44cad96..ebecf3b 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -43,21 +43,30 @@ public: /** * @brief Registers listener which receives all messages with commands (messages with leading '/' char). * @param commandName Command name which listener can handle. - * @param listener Listener. + * @param listener Listener. Pass nullptr to remove listener of command */ inline void onCommand(const std::string& commandName, const MessageListener& listener) { - _onCommandListeners[commandName] = listener; + if (listener) { + _onCommandListeners[commandName] = listener; + } else { + _onCommandListeners.erase(commandName); + } } /** * @brief Registers listener which receives all messages with commands (messages with leading '/' char). * @param commandsList Commands names which listener can handle. - * @param listener Listener. + * @param listener Listener. Pass nullptr to remove listener of commands */ inline void onCommand(const std::initializer_list& commandsList, const MessageListener& listener) { - for (const auto& command : commandsList) - { - _onCommandListeners[command] = listener; + if (listener) { + for (const auto& command : commandsList) { + _onCommandListeners[command] = listener; + } + } else { + for (const auto& command : commandsList) { + _onCommandListeners.erase(command); + } } } @@ -93,6 +102,10 @@ public: _onChosenInlineResultListeners.push_back(listener); } + /** + * @brief Registers listener which receives all the callback query. + * @param listener Listener. + */ inline void onCallbackQuery(const CallbackQueryListener& listener){ _onCallbackQueryListeners.push_back(listener); } -- cgit v1.2.3