-
Notifications
You must be signed in to change notification settings - Fork 2
/
help.go
60 lines (51 loc) · 1.5 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
package commands
import (
"sort"
"strings"
"github.com/hexbotio/hex/models"
"github.com/hexbotio/hex/parse"
)
func Help(message *models.Message, rules *map[string]models.Rule, config models.Config) {
help := make([]string, 0)
// pull internal help
help = make([]string, 4)
help[0] = config.BotName + " help <filter> - This help"
help[1] = config.BotName + " ping - Simple ping response for the bot"
help[2] = config.BotName + " rules - dump of loaded rules"
help[3] = config.BotName + " version - Compiled version number/sha"
// pull all help from rules
for _, rule := range *rules {
if rule.Active && !rule.Hide {
if parse.EitherMember(rule.ACL, message.Attributes["hex.user"], message.Attributes["hex.channel"]) {
if rule.Help != "" {
help = append(help, rule.Help)
} else {
help = append(help, rule.Match)
}
}
}
}
// sort, filter and de-dupe help
helpTitle := "Help for " + config.BotName + "..."
sort.Strings(help)
var lasthelp = ""
var newhelp = make([]string, 0)
var filter = strings.TrimSpace(strings.Replace(message.Attributes["hex.input"], config.BotName+" help", "", 1))
newhelp = append(newhelp, helpTitle)
for _, h := range help {
if strings.Contains(h, filter) || filter == "" {
if h != lasthelp {
newhelp = append(newhelp, " - "+h)
}
lasthelp = h
}
}
// output help
if len(newhelp) > 1 {
message.Outputs = append(message.Outputs, models.Output{
Rule: "help",
Response: strings.Join(newhelp[:], "\n"),
Success: true,
})
}
}