Use a command list (not a map) to have a consistent manually-defined commands order
This commit is contained in:
@@ -15,23 +15,57 @@ import (
|
|||||||
|
|
||||||
type sanitizerFunc func(string) string
|
type sanitizerFunc func(string) string
|
||||||
|
|
||||||
|
type commandDefinition struct {
|
||||||
|
key string
|
||||||
|
description string
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandList []commandDefinition
|
||||||
|
|
||||||
|
func (c commandList) get(key string) (*commandDefinition, bool) {
|
||||||
|
for _, command := range c {
|
||||||
|
if command.key == key {
|
||||||
|
return &command, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
commands = map[string]string{
|
commands = commandList{
|
||||||
// special commands
|
// special commands
|
||||||
"help": "Get help",
|
{
|
||||||
"stop": "Disable bridge for the room and clear all configuration",
|
key: "help",
|
||||||
|
description: "Show this help message",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "stop",
|
||||||
|
description: "Disable bridge for the room and clear all configuration",
|
||||||
|
},
|
||||||
|
|
||||||
// options commands
|
// options commands
|
||||||
optionMailbox: "Get or set mailbox of the room",
|
{
|
||||||
optionOwner: "Get or set owner of the room",
|
key: optionMailbox,
|
||||||
optionNoSender: fmt.Sprintf(
|
description: "Get or set mailbox of the room",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionOwner,
|
||||||
|
description: "Get or set owner of the room",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionNoSender,
|
||||||
|
description: fmt.Sprintf(
|
||||||
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
|
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
|
||||||
optionNoSender,
|
optionNoSender,
|
||||||
),
|
),
|
||||||
optionNoSubject: fmt.Sprintf(
|
},
|
||||||
|
{
|
||||||
|
key: optionNoSubject,
|
||||||
|
description: fmt.Sprintf(
|
||||||
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
|
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
|
||||||
optionNoSubject,
|
optionNoSubject,
|
||||||
),
|
),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizers is map of option name => sanitizer function
|
// sanitizers is map of option name => sanitizer function
|
||||||
@@ -43,7 +77,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
||||||
if _, ok := commands[command[0]]; !ok {
|
if _, ok := commands.get(command[0]); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,11 +116,11 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
|||||||
|
|
||||||
var msg strings.Builder
|
var msg strings.Builder
|
||||||
msg.WriteString("the following commands are supported:\n\n")
|
msg.WriteString("the following commands are supported:\n\n")
|
||||||
for name, desc := range commands {
|
for _, command := range commands {
|
||||||
msg.WriteString("* **")
|
msg.WriteString("* **")
|
||||||
msg.WriteString(name)
|
msg.WriteString(command.key)
|
||||||
msg.WriteString("** - ")
|
msg.WriteString("** - ")
|
||||||
msg.WriteString(desc)
|
msg.WriteString(command.description)
|
||||||
msg.WriteString("\n")
|
msg.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user