Skip to content

Commit c023152

Browse files
authored
Commands (#3)
* add command list * add rcon function * fix palworld rcon port config type is uint64 * fix help command is internal function * add save command * fix config * add doExit command * fix client connect * move rcon function to utils * add shutdown command * fix when wrong rcon port config received * fix i18n fallback is en * fix i18n and config cycle dependency * refactoring shutdown command * add kick/ban/broadcase commands * separate admin command * remove help command function * add only prefix case, print help
1 parent d13ee5b commit c023152

File tree

19 files changed

+337
-27
lines changed

19 files changed

+337
-27
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ go 1.22.0
55
require (
66
github.com/bwmarrin/discordgo v0.27.1
77
github.com/joho/godotenv v1.5.1
8+
github.com/juunini/palworld-rcon v1.0.0
89
)
910

1011
require (
12+
github.com/gorcon/rcon v1.3.5 // indirect
1113
github.com/gorilla/websocket v1.4.2 // indirect
1214
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
1315
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
22
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
3+
github.com/gorcon/rcon v1.3.5 h1:YE/Vrw6R99uEP08wp0EjdPAP3Jwz/ys3J8qxI1nYoeU=
4+
github.com/gorcon/rcon v1.3.5/go.mod h1:zR1qfKZttF8vAgH1NsP6CdpachOvLDq8jE64NboTpIM=
35
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
46
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
57
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
68
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
9+
github.com/juunini/palworld-rcon v1.0.0 h1:H5R6ZW216xepxwQa+EVMexR+5pvchIUa2sQJx6Pa93o=
10+
github.com/juunini/palworld-rcon v1.0.0/go.mod h1:hN2aABVdCDpOjea6aPynO6nEM64CKSss+60evi2jQhY=
711
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
812
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
913
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

src/bot/commands/ban.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package commands
2+
3+
import (
4+
"strings"
5+
6+
"github.com/juunini/palworld-discord-bot/src/i18n"
7+
"github.com/juunini/palworld-discord-bot/src/utils"
8+
)
9+
10+
func ban(command string) string {
11+
steamID, found := strings.CutPrefix(command, "ban ")
12+
13+
if !found {
14+
return i18n.WrongParameters
15+
}
16+
17+
client, err := utils.RconClient()
18+
if err != nil {
19+
return i18n.FailedToConnectRconServer
20+
}
21+
22+
defer client.Disconnect()
23+
24+
response, err := client.BanPlayer(steamID)
25+
if err != nil {
26+
return i18n.FailedToBanCommand
27+
}
28+
29+
return response
30+
}

src/bot/commands/broadcast.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package commands
2+
3+
import (
4+
"strings"
5+
6+
"github.com/juunini/palworld-discord-bot/src/i18n"
7+
"github.com/juunini/palworld-discord-bot/src/utils"
8+
)
9+
10+
func broadcast(command string) string {
11+
message, found := strings.CutPrefix(command, "broadcast ")
12+
13+
if !found {
14+
return i18n.WrongParameters
15+
}
16+
17+
client, err := utils.RconClient()
18+
if err != nil {
19+
return i18n.FailedToConnectRconServer
20+
}
21+
22+
defer client.Disconnect()
23+
24+
response, err := client.Broadcast(message)
25+
if err != nil {
26+
return i18n.FailedToBroadcastCommand
27+
}
28+
29+
return response
30+
}

src/bot/commands/doExit.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package commands
2+
3+
import (
4+
"github.com/juunini/palworld-discord-bot/src/console_decoration"
5+
"github.com/juunini/palworld-discord-bot/src/i18n"
6+
"github.com/juunini/palworld-discord-bot/src/utils"
7+
)
8+
9+
func doExit() string {
10+
client, err := utils.RconClient()
11+
if err != nil {
12+
return i18n.FailedToConnectRconServer
13+
}
14+
15+
defer client.Disconnect()
16+
17+
response, err := client.DoExit()
18+
if err != nil {
19+
console_decoration.PrintError(i18n.FailedToDoExitCommand + ": " + err.Error())
20+
return i18n.FailedToDoExitCommand
21+
}
22+
23+
return response
24+
}

src/bot/commands/help.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/bot/commands/kick.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package commands
2+
3+
import (
4+
"strings"
5+
6+
"github.com/juunini/palworld-discord-bot/src/i18n"
7+
"github.com/juunini/palworld-discord-bot/src/utils"
8+
)
9+
10+
func kick(command string) string {
11+
steamID, found := strings.CutPrefix(command, "kick ")
12+
13+
if !found {
14+
return i18n.WrongParameters
15+
}
16+
17+
client, err := utils.RconClient()
18+
if err != nil {
19+
return i18n.FailedToConnectRconServer
20+
}
21+
22+
defer client.Disconnect()
23+
24+
response, err := client.KickPlayer(steamID)
25+
if err != nil {
26+
return i18n.FailedToKickCommand
27+
}
28+
29+
return response
30+
}

src/bot/commands/response.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,36 @@ import (
44
"strings"
55

66
"github.com/juunini/palworld-discord-bot/src/config"
7+
"github.com/juunini/palworld-discord-bot/src/i18n"
8+
"github.com/juunini/palworld-discord-bot/src/utils"
79
)
810

9-
func Response(message string) string {
10-
command, _ := strings.CutPrefix(message, config.DISCORD_COMMAND_PREFIX+" ")
11+
func Response(message string, username string) string {
12+
isAdmin := utils.IsAdmin(username)
1113

12-
if command == "help" {
13-
return Help()
14+
command, found := strings.CutPrefix(message, config.DISCORD_COMMAND_PREFIX+" ")
15+
if command == "help" || !found {
16+
return i18n.Help(config.DISCORD_COMMAND_PREFIX, isAdmin)
1417
}
1518

16-
return "Unknown command"
19+
// Under commands, only admins can execute
20+
if !isAdmin {
21+
return i18n.UnknownCommand
22+
}
23+
24+
if command == "kick" {
25+
return kick(command)
26+
} else if command == "ban" {
27+
return ban(command)
28+
} else if strings.HasPrefix(command, "broadcast") {
29+
return broadcast(command)
30+
} else if strings.HasPrefix(command, "shutdown") {
31+
return shutdown(command)
32+
} else if command == "doExit" {
33+
return doExit()
34+
} else if command == "save" {
35+
return save()
36+
}
37+
38+
return i18n.UnknownCommand
1739
}

src/bot/commands/save.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package commands
2+
3+
import (
4+
"github.com/juunini/palworld-discord-bot/src/console_decoration"
5+
"github.com/juunini/palworld-discord-bot/src/i18n"
6+
"github.com/juunini/palworld-discord-bot/src/utils"
7+
)
8+
9+
func save() string {
10+
client, err := utils.RconClient()
11+
if err != nil {
12+
return i18n.FailedToConnectRconServer
13+
}
14+
15+
defer client.Disconnect()
16+
17+
response, err := client.Save()
18+
if err != nil {
19+
console_decoration.PrintError(i18n.FailedToSaveCommand + ": " + err.Error())
20+
return i18n.FailedToSaveCommand
21+
}
22+
23+
return response
24+
}

src/bot/commands/shutdown.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)