aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-12-08 22:58:47 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-12-08 23:03:58 +0100
commit02b86e3aafa9a505863ad217fb7916d389f8a199 (patch)
tree8b33b2839e4295b83e38253855ec3f012d14a535
parent59c37c9042c95f3c298065b8ba0fa6d45ef6676b (diff)
add gserver command
-rw-r--r--README.md9
-rw-r--r--chatcommands.go59
2 files changed, 68 insertions, 0 deletions
diff --git a/README.md b/README.md
index 0f34389..5945b88 100644
--- a/README.md
+++ b/README.md
@@ -82,6 +82,15 @@ Description: Display your current upstream server and all other configured serve
Usage: `server [server]`
```
+> `gserver`
+```
+Permission: cmd_gserver
+Description: Display the groups your current upstream server is in
+and all other configured groups. If a valid group name is specified,
+switch to a random server of that group.
+Usage: `gserver [group]`
+```
+
> `kick`
```
Permission: cmd_kick
diff --git a/chatcommands.go b/chatcommands.go
index 6977846..04dd01f 100644
--- a/chatcommands.go
+++ b/chatcommands.go
@@ -341,6 +341,65 @@ func init() {
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
+ Name: "gserver",
+ Perm: "cmd_gserver",
+ Help: "Display the groups your current upstream server is in and all other configured groups. If a valid group name is specified, switch to a random server of that group.",
+ Usage: "gserver [group]",
+ Handler: func(cc *proxy.ClientConn, args ...string) string {
+ if len(args) != 1 {
+ if len(args) > 1 {
+ return "Usage: gserver [group]"
+ }
+
+ conf := proxy.Conf()
+
+ srv, ok := conf.Servers[cc.ServerName()]
+ if !ok {
+ return "Not connected to a server."
+ }
+
+ groups := conf.ServerGroups()
+ srvGroups := strings.Join(srv.Groups, ", ")
+
+ allGroups := make([]string, 0, len(groups))
+ for group := range groups {
+ allGroups = append(allGroups, group)
+ }
+
+ return fmt.Sprintf("Connected to: %s | Groups: %s", srvGroups, strings.Join(allGroups, ", "))
+ }
+
+ for i := 0; i < 5; i++ {
+ srv, ok := proxy.Conf().RandomGroupServer(args[0])
+ if !ok {
+ return "Group does not exist."
+ }
+
+ if srv == args[0] {
+ return "Group is also a server."
+ }
+
+ if i == 4 && cc.ServerName() == srv {
+ return "Already connected to this server after 5 attempts."
+ }
+
+ if err := cc.Hop(srv); err != nil {
+ cc.Log("<-", err)
+
+ if errors.Is(err, proxy.ErrNoSuchServer) {
+ return "Server does not exist."
+ } else if errors.Is(err, proxy.ErrNewMediaPool) {
+ return "The new server belongs to a media pool that is not present on this client. Please reconnect to access it."
+ }
+
+ return ""
+ }
+ }
+
+ return ""
+ },
+ })
+ proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "kick",
Perm: "cmd_kick",
Help: "Disconnect a player with an optional reason.",