Compare commits
2 commits
b0c5302c82
...
dc6f99dc3c
Author | SHA1 | Date | |
---|---|---|---|
Bohdan Horbeshko | dc6f99dc3c | ||
Bohdan Horbeshko | 772246ee4b |
|
@ -3,6 +3,7 @@ package telegram
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -146,6 +147,21 @@ func GetCommand(typ CommandType, cmd string) (command, bool) {
|
||||||
return command, ok
|
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
|
// CommandToHelpString builds a text description of a command
|
||||||
func CommandToHelpString(name string, cmd command) string {
|
func CommandToHelpString(name string, cmd command) string {
|
||||||
var str strings.Builder
|
var str strings.Builder
|
||||||
|
@ -175,7 +191,8 @@ func helpString(typ CommandType) string {
|
||||||
commandMap := GetCommands(typ)
|
commandMap := GetCommands(typ)
|
||||||
|
|
||||||
str.WriteString("Available commands:\n")
|
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(CommandToHelpString(name, command))
|
||||||
str.WriteString("\n")
|
str.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
|
@ -557,7 +557,8 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) {
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := telegram.GetCommands(cmdType)
|
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))
|
di.AddItem(iq.To, name, telegram.CommandToHelpString(name, command))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -759,9 +760,15 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command
|
||||||
if ok && len(cmd.Arguments) > 0 {
|
if ok && len(cmd.Arguments) > 0 {
|
||||||
var fields []*stanza.Field
|
var fields []*stanza.Field
|
||||||
for i, arg := range cmd.Arguments {
|
for i, arg := range cmd.Arguments {
|
||||||
|
var required *string
|
||||||
|
if i < cmd.RequiredArgs {
|
||||||
|
dummyString := ""
|
||||||
|
required = &dummyString
|
||||||
|
}
|
||||||
fields = append(fields, &stanza.Field{
|
fields = append(fields, &stanza.Field{
|
||||||
Var: strconv.FormatInt(int64(i), 10),
|
Var: strconv.FormatInt(int64(i), 10),
|
||||||
Label: arg,
|
Label: arg,
|
||||||
|
Required: required,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
answer.Payload = &stanza.Command{
|
answer.Payload = &stanza.Command{
|
||||||
|
|
Loading…
Reference in a new issue