fix /exit will mis-trigger on meant to be used on other bots

This commit is contained in:
A.C.Sukazyo Eyre 2024-08-22 15:32:57 +08:00
parent 1230b9e6b5
commit c632df4cd8
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
3 changed files with 24 additions and 5 deletions

View File

@ -1,4 +1,5 @@
package cc.sukazyo.cono.morny.bot.command package cc.sukazyo.cono.morny.bot.command
import cc.sukazyo.cono.morny.bot.command.ICommandAlias.HiddenAlias import cc.sukazyo.cono.morny.bot.command.ICommandAlias.HiddenAlias
import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.MornyCoeur
import cc.sukazyo.cono.morny.data.TelegramStickers 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.InputCommand
import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramFormatter.* import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramFormatter.*
import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec 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 com.pengrad.telegrambot.request.SendSticker
import scala.language.postfixOps import scala.language.postfixOps
class MornyManagers (using coeur: MornyCoeur) { 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 { object Exit extends ITelegramCommand {
override val name: String = "exit" override val name: String = "exit"
@ -23,6 +36,10 @@ class MornyManagers (using coeur: MornyCoeur) {
override def execute (using command: InputCommand, event: Update): Unit = { 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 val user = event.message.from
if (coeur.trusted isTrusted user.id) { if (coeur.trusted isTrusted user.id) {
@ -58,6 +75,10 @@ class MornyManagers (using coeur: MornyCoeur) {
override def execute (using command: InputCommand, event: Update): Unit = { 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 val user = event.message.from
if (coeur.trusted isTrusted user.id) { if (coeur.trusted isTrusted user.id) {

View File

@ -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.util.tgapi.TelegramExtensions.Bot.exec
import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.Log.logger
import com.google.gson.Gson import com.google.gson.Gson
import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.model.request.InlineQueryResult import com.pengrad.telegrambot.model.request.InlineQueryResult
import com.pengrad.telegrambot.request.AnswerInlineQuery import com.pengrad.telegrambot.request.AnswerInlineQuery
@ -32,8 +31,7 @@ class MornyOnInlineQuery (using queryManager: MornyQueries) (using coeur: MornyC
if (results isEmpty) return; 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( coeur.account exec AnswerInlineQuery(
update.inlineQuery.id, resultAnswers toArray:_* update.inlineQuery.id, resultAnswers toArray:_*
).cacheTime(cacheTime).isPersonal(isPersonal) ).cacheTime(cacheTime).isPersonal(isPersonal)

View File

@ -6,7 +6,7 @@ import com.pengrad.telegrambot.model.request.InlineQueryResult
object InlineQueryUnit { object InlineQueryUnit {
object defaults: object defaults:
val CACHE_TIME = 1 val CACHE_TIME = 300
val IS_PERSONAL = false val IS_PERSONAL = false
} }