mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-09-11 04:36:20 +00:00
Refactor commands and improve error handling
This commit is contained in:
parent
22de381e54
commit
efc752d68b
1 changed files with 27 additions and 20 deletions
|
@ -243,7 +243,12 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
|
||||||
|
|
||||||
command, _, commandArgs := tu.ParseCommand(message.Text)
|
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 {
|
switch command {
|
||||||
case "help":
|
case "help":
|
||||||
msg += t.I18nBot("tgbot.commands.help")
|
msg += t.I18nBot("tgbot.commands.help")
|
||||||
|
@ -266,9 +271,7 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
|
||||||
if isAdmin {
|
if isAdmin {
|
||||||
t.searchClient(chatId, commandArgs[0])
|
t.searchClient(chatId, commandArgs[0])
|
||||||
} else {
|
} else {
|
||||||
// Convert message.From.ID to int64
|
t.getClientUsage(chatId, int64(message.From.ID), commandArgs[0])
|
||||||
fromID := int64(message.From.ID)
|
|
||||||
t.getClientUsage(chatId, fromID, commandArgs[0])
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg += t.I18nBot("tgbot.commands.usage")
|
msg += t.I18nBot("tgbot.commands.usage")
|
||||||
|
@ -278,43 +281,47 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
|
||||||
if isAdmin && len(commandArgs) > 0 {
|
if isAdmin && len(commandArgs) > 0 {
|
||||||
t.searchInbound(chatId, commandArgs[0])
|
t.searchInbound(chatId, commandArgs[0])
|
||||||
} else {
|
} else {
|
||||||
msg += t.I18nBot("tgbot.commands.unknown")
|
handleUnknownCommand()
|
||||||
}
|
}
|
||||||
case "restart":
|
case "restart":
|
||||||
onlyMessage = true
|
onlyMessage = true
|
||||||
if isAdmin && len(commandArgs) == 0 {
|
if isAdmin {
|
||||||
msg += t.I18nBot("tgbot.commands.unknown")
|
if len(commandArgs) == 0 {
|
||||||
msg += t.I18nBot("tgbot.commands.restartUsage")
|
msg += t.I18nBot("tgbot.commands.restartUsage")
|
||||||
} else if isAdmin && len(commandArgs) > 0 {
|
} else if strings.ToLower(commandArgs[0]) == "force" {
|
||||||
if strings.ToLower(commandArgs[0]) == "force" {
|
|
||||||
if t.xrayService.IsXrayRunning() {
|
if t.xrayService.IsXrayRunning() {
|
||||||
err := t.xrayService.RestartXray(true)
|
err := t.xrayService.RestartXray(true)
|
||||||
msg += t.I18nBot("tgbot.commands.restartSuccess")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error())
|
msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error())
|
||||||
|
} else {
|
||||||
|
msg += t.I18nBot("tgbot.commands.restartSuccess")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg += t.I18nBot("tgbot.commands.xrayNotRunning")
|
msg += t.I18nBot("tgbot.commands.xrayNotRunning")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg += t.I18nBot("tgbot.commands.unknown")
|
handleUnknownCommand()
|
||||||
msg += t.I18nBot("tgbot.commands.restartUsage")
|
msg += t.I18nBot("tgbot.commands.restartUsage")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg += t.I18nBot("tgbot.commands.unknown")
|
handleUnknownCommand()
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
msg += t.I18nBot("tgbot.commands.unknown")
|
handleUnknownCommand()
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg != "" {
|
if msg != "" {
|
||||||
|
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 {
|
if onlyMessage {
|
||||||
t.SendMsgToTgbot(chatId, msg)
|
t.SendMsgToTgbot(chatId, msg)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
t.SendAnswer(chatId, msg, isAdmin)
|
t.SendAnswer(chatId, msg, isAdmin)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool) {
|
func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool) {
|
||||||
|
|
Loading…
Reference in a new issue