diff --git a/web/service/tgbot.go b/web/service/tgbot.go index bba3e504..449161d3 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -243,7 +243,12 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo command, _, commandArgs := tu.ParseCommand(message.Text) - // Extract the command from the Message. + // Helper function to handle unknown commands. + handleUnknownCommand := func() { + msg += t.I18nBot("tgbot.commands.unknown") + } + + // Handle the command. switch command { case "help": msg += t.I18nBot("tgbot.commands.help") @@ -266,9 +271,7 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo if isAdmin { t.searchClient(chatId, commandArgs[0]) } else { - // Convert message.From.ID to int64 - fromID := int64(message.From.ID) - t.getClientUsage(chatId, fromID, commandArgs[0]) + t.getClientUsage(chatId, int64(message.From.ID), commandArgs[0]) } } else { msg += t.I18nBot("tgbot.commands.usage") @@ -278,42 +281,46 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo if isAdmin && len(commandArgs) > 0 { t.searchInbound(chatId, commandArgs[0]) } else { - msg += t.I18nBot("tgbot.commands.unknown") + handleUnknownCommand() } case "restart": onlyMessage = true - if isAdmin && len(commandArgs) == 0 { - msg += t.I18nBot("tgbot.commands.unknown") - msg += t.I18nBot("tgbot.commands.restartUsage") - } else if isAdmin && len(commandArgs) > 0 { - if strings.ToLower(commandArgs[0]) == "force" { + if isAdmin { + if len(commandArgs) == 0 { + msg += t.I18nBot("tgbot.commands.restartUsage") + } else if strings.ToLower(commandArgs[0]) == "force" { if t.xrayService.IsXrayRunning() { err := t.xrayService.RestartXray(true) - msg += t.I18nBot("tgbot.commands.restartSuccess") if err != nil { msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error()) + } else { + msg += t.I18nBot("tgbot.commands.restartSuccess") } } else { msg += t.I18nBot("tgbot.commands.xrayNotRunning") } } else { - msg += t.I18nBot("tgbot.commands.unknown") + handleUnknownCommand() msg += t.I18nBot("tgbot.commands.restartUsage") } } else { - msg += t.I18nBot("tgbot.commands.unknown") + handleUnknownCommand() } default: - msg += t.I18nBot("tgbot.commands.unknown") + handleUnknownCommand() } if msg != "" { - if onlyMessage { - t.SendMsgToTgbot(chatId, msg) - return - } else { - t.SendAnswer(chatId, msg, isAdmin) - } + t.sendResponse(chatId, msg, onlyMessage, isAdmin) + } +} + +// Helper function to send the message based on onlyMessage flag. +func (t *Tgbot) sendResponse(chatId int64, msg string, onlyMessage, isAdmin bool) { + if onlyMessage { + t.SendMsgToTgbot(chatId, msg) + } else { + t.SendAnswer(chatId, msg, isAdmin) } }