|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/juunini/palworld-discord-bot/src/console_decoration" |
| 7 | + "github.com/juunini/palworld-discord-bot/src/i18n" |
| 8 | + "github.com/juunini/palworld-discord-bot/src/utils" |
| 9 | +) |
| 10 | + |
| 11 | +func shutdown(command string) string { |
| 12 | + client, err := utils.RconClient() |
| 13 | + if err != nil { |
| 14 | + return i18n.FailedToConnectRconServer |
| 15 | + } |
| 16 | + |
| 17 | + defer client.Disconnect() |
| 18 | + |
| 19 | + seconds, message, errorMessage := shutdownParameters(command) |
| 20 | + if errorMessage != "" { |
| 21 | + return errorMessage |
| 22 | + } |
| 23 | + |
| 24 | + response, err := client.Shutdown(seconds, message) |
| 25 | + if err != nil { |
| 26 | + console_decoration.PrintError(i18n.FailedToShutdownCommand + ": " + err.Error()) |
| 27 | + return i18n.FailedToShutdownCommand |
| 28 | + } |
| 29 | + |
| 30 | + return response |
| 31 | +} |
| 32 | + |
| 33 | +func shutdownParameters(command string) (uint, string, string) { |
| 34 | + paramString, _ := strings.CutPrefix(command, "shutdown ") |
| 35 | + params := strings.Split(paramString, " ") |
| 36 | + |
| 37 | + if len(params) < 2 { |
| 38 | + console_decoration.PrintError(i18n.WrongParameters) |
| 39 | + return 0, "", i18n.WrongParameters |
| 40 | + } |
| 41 | + |
| 42 | + seconds, err := utils.ToUint(params[0]) |
| 43 | + if err != nil { |
| 44 | + console_decoration.PrintError(i18n.WrongParameters + ": " + err.Error()) |
| 45 | + return 0, "", i18n.WrongParameters |
| 46 | + } |
| 47 | + |
| 48 | + message := params[1] |
| 49 | + return seconds, message, "" |
| 50 | +} |
0 commit comments