This repository was archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathhelp.go
65 lines (58 loc) · 1.4 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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) {
msg := &Message{
Text: CmdPrefix + c.RawArgs,
}
cmd, _ := parse(msg, 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.SendMessage(OutgoingMessage{
Target: c.Channel,
Message: fmt.Sprintf(helpDescripton, help.Description),
Sender: c.User,
})
}
b.SendMessage(OutgoingMessage{
Target: c.Channel,
Message: fmt.Sprintf(helpUsage, CmdPrefix, c.Command, help.ExampleArgs),
Sender: c.User,
})
}
func (b *Bot) showAvailabeCommands(channel string, sender *User) {
var cmds []string
for k := range commands {
cmds = append(cmds, k)
}
b.SendMessage(OutgoingMessage{
Target: channel,
Message: fmt.Sprintf(helpAboutCommand, CmdPrefix),
Sender: sender,
})
b.SendMessage(OutgoingMessage{
Target: channel,
Message: fmt.Sprintf(availableCommands, strings.Join(cmds, ", ")),
Sender: sender,
})
}