forked from go-chat-bot/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
46 lines (39 loc) · 1.17 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package bot
import (
"fmt"
"strings"
)
const (
helpDescripton = "Description: %s"
helpUsage = "Usage: %s%s %s"
availableCommands = "Available commands: %v"
helpAboutCommand = "Type: '%shelp <command>' to see details about a specific command."
helpCommand = "help"
)
func (b *Bot) help(c *Cmd) {
cmd, _ := parse(CmdPrefix+c.RawArgs, c.ChannelData, c.User)
if cmd == nil {
b.showAvailabeCommands(c.Channel, c.User)
return
}
command := commands[cmd.Command]
if command == nil {
b.showAvailabeCommands(c.Channel, c.User)
return
}
b.showHelp(cmd, command)
}
func (b *Bot) showHelp(c *Cmd, help *customCommand) {
if help.Description != "" {
b.handlers.Response(c.Channel, fmt.Sprintf(helpDescripton, help.Description), c.User)
}
b.handlers.Response(c.Channel, fmt.Sprintf(helpUsage, CmdPrefix, c.Command, help.ExampleArgs), c.User)
}
func (b *Bot) showAvailabeCommands(channel string, sender *User) {
var cmds []string
for k := range commands {
cmds = append(cmds, k)
}
b.handlers.Response(channel, fmt.Sprintf(helpAboutCommand, CmdPrefix), sender)
b.handlers.Response(channel, fmt.Sprintf(availableCommands, strings.Join(cmds, ", ")), sender)
}