From 4c9a50af1fcbd5a3f13a25a3245343ba02b9bb70 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Thu, 25 Jan 2024 11:56:27 +0100 Subject: [PATCH] Removed unknown command message when not tagged in groups --- bot/bot.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 653fa9d..bad81c1 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -89,19 +89,34 @@ func handleUnknown(bot *tgbotapi.BotAPI, update *tgbotapi.Update, _ string) bool func handleCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) { commandName := strings.ToLower(update.Message.Command()) - // Check if the command is for me - commandWithAt := update.Message.CommandWithAt() - atIndex := strings.Index(commandWithAt, "@") - if atIndex != -1 { - forName := commandWithAt[atIndex+1:] - log.Println(forName) - - if bot.Self.UserName != forName { - return + // If the bot is in a group and the command does NOT have the recipient bot: + // If is the command is not correct nothing is done + // + // If the bot is in a group and the command does HAVE the recipient bot: + // If the command is not correct the bot returns unknown command message + + permitUnknownCommandMessage := true + + if !update.Message.Chat.IsPrivate() { + // Check if the command is for me + commandWithAt := update.Message.CommandWithAt() + atIndex := strings.Index(commandWithAt, "@") + if atIndex != -1 { + forName := commandWithAt[atIndex+1:] + + if bot.Self.UserName != forName { + return + } + } else { + permitUnknownCommandMessage = false } } for _, h := range handlers { + if !permitUnknownCommandMessage && h.string == "unknown" { + continue + } + if h.handlerBehavior(bot, update, commandName) { log.Printf("@%s: \t%s -> %s", update.Message.From.UserName, update.Message.Text, h.string) return