diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala index aea4127..36713ea 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala @@ -1,4 +1,5 @@ package cc.sukazyo.cono.morny.bot.command + import cc.sukazyo.cono.morny.bot.command.ICommandAlias.HiddenAlias import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.data.TelegramStickers @@ -7,13 +8,25 @@ import cc.sukazyo.cono.morny.daemon.MornyReport import cc.sukazyo.cono.morny.util.tgapi.InputCommand import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramFormatter.* import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec -import com.pengrad.telegrambot.model.Update +import com.pengrad.telegrambot.model.{Chat, Update} import com.pengrad.telegrambot.request.SendSticker import scala.language.postfixOps class MornyManagers (using coeur: MornyCoeur) { + /** Check if the command is directly targeted to Morny. + * + * If and only if the command is in private chat, or the command is in group chat AND the command target is explicitly + * set to Morny's username, then the command is targeted to Morny. + * + * This aims to reduce the mis-trigger on some commands that are easily collisions to other bots and are important. + */ + def isMornyTargeted (event: Update, command: InputCommand): Boolean = + if event.message.chat.`type` != Chat.Type.Private && command.target != coeur.username then false + else if command.target != null && command.target != coeur.username then false + else true + object Exit extends ITelegramCommand { override val name: String = "exit" @@ -23,6 +36,10 @@ class MornyManagers (using coeur: MornyCoeur) { override def execute (using command: InputCommand, event: Update): Unit = { + if !isMornyTargeted(event, command) then + logger debug "seems command does not targeted to morny, skipped" + return + val user = event.message.from if (coeur.trusted isTrusted user.id) { @@ -58,6 +75,10 @@ class MornyManagers (using coeur: MornyCoeur) { override def execute (using command: InputCommand, event: Update): Unit = { + if !isMornyTargeted(event, command) then + logger debug "seems command does not targeted to morny, skipped" + return + val user = event.message.from if (coeur.trusted isTrusted user.id) { diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala index c785528..e8f4069 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala @@ -6,7 +6,6 @@ import cc.sukazyo.cono.morny.bot.query.{InlineQueryUnit, MornyQueries} import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import cc.sukazyo.cono.morny.Log.logger import com.google.gson.Gson -import com.pengrad.telegrambot.model.Update import com.pengrad.telegrambot.model.request.InlineQueryResult import com.pengrad.telegrambot.request.AnswerInlineQuery @@ -32,8 +31,7 @@ class MornyOnInlineQuery (using queryManager: MornyQueries) (using coeur: MornyC if (results isEmpty) return; - logger debug resultAnswers.map(Gson().toJson(_)).mkString("\n") - + logger trace "Query answers:\n" + resultAnswers.map(" " + Gson().toJson(_)).mkString("\n") coeur.account exec AnswerInlineQuery( update.inlineQuery.id, resultAnswers toArray:_* ).cacheTime(cacheTime).isPersonal(isPersonal) diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala index 911a5ae..b15f4b6 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala @@ -6,7 +6,7 @@ import com.pengrad.telegrambot.model.request.InlineQueryResult object InlineQueryUnit { object defaults: - val CACHE_TIME = 1 + val CACHE_TIME = 300 val IS_PERSONAL = false }