aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2024-02-11 00:27:08 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2024-02-11 00:27:08 +0300
commitdc6f99dc3ca0906bfd5f9bda9eab618445cfa878 (patch)
tree699905531d2b5afc88adf7a6879825975e93ddc4
parent772246ee4b78883ebacdf594e1fc1d485dcb3a58 (diff)
Stable command order in help and Ad-Hoc list
-rw-r--r--telegram/commands.go19
-rw-r--r--xmpp/handlers.go3
2 files changed, 20 insertions, 2 deletions
diff --git a/telegram/commands.go b/telegram/commands.go
index d9b9f13..8d4de91 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -3,6 +3,7 @@ package telegram
import (
"fmt"
"github.com/pkg/errors"
+ "sort"
"strconv"
"strings"
"time"
@@ -146,6 +147,21 @@ func GetCommand(typ CommandType, cmd string) (command, bool) {
return command, ok
}
+// SortedCommandKeys sorts a slice with command keys
+func SortedCommandKeys(commandMap map[string]command) []string {
+ keys := make([]string, len(commandMap))
+
+ i := 0
+ for k := range commandMap {
+ keys[i] = k
+ i++
+ }
+
+ sort.Strings(keys)
+
+ return keys
+}
+
// CommandToHelpString builds a text description of a command
func CommandToHelpString(name string, cmd command) string {
var str strings.Builder
@@ -175,7 +191,8 @@ func helpString(typ CommandType) string {
commandMap := GetCommands(typ)
str.WriteString("Available commands:\n")
- for name, command := range commandMap {
+ for _, name := range SortedCommandKeys(commandMap) {
+ command := commandMap[name]
str.WriteString(CommandToHelpString(name, command))
str.WriteString("\n")
}
diff --git a/xmpp/handlers.go b/xmpp/handlers.go
index 08278d2..c50dd1c 100644
--- a/xmpp/handlers.go
+++ b/xmpp/handlers.go
@@ -557,7 +557,8 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) {
}
commands := telegram.GetCommands(cmdType)
- for name, command := range commands {
+ for _, name := range telegram.SortedCommandKeys(commands) {
+ command := commands[name]
di.AddItem(iq.To, name, telegram.CommandToHelpString(name, command))
}
} else {