-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhelpplugin.go
170 lines (141 loc) · 4.63 KB
/
helpplugin.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package bruxism
import (
"encoding/json"
"fmt"
"log"
"sort"
"strings"
)
type helpPlugin struct {
Private map[string]bool
}
// Name returns the name of the service.
func (p *helpPlugin) Name() string {
return "Help"
}
// Help returns a list of help strings that are printed when the user requests them.
func (p *helpPlugin) Help(bot *Bot, service Service, message Message, detailed bool) []string {
privs := service.SupportsPrivateMessages() && !service.IsPrivate(message) && service.IsModerator(message)
if detailed && !privs {
return nil
}
ticks := ""
if service.Name() == DiscordServiceName {
ticks = "`"
}
commands := []string{}
for _, plugin := range bot.Services[service.Name()].Plugins {
hasDetailed := false
if plugin == p {
hasDetailed = privs
} else {
t := plugin.Help(bot, service, message, true)
hasDetailed = t != nil && len(t) > 0
}
if hasDetailed {
commands = append(commands, strings.ToLower(plugin.Name()))
}
}
sort.Strings(commands)
help := []string{}
if len(commands) > 0 {
help = append(help, CommandHelp(service, "help", "[topic]", fmt.Sprintf("Returns help for a specific topic. Available topics: %s%s%s", ticks, strings.Join(commands, ", "), ticks))[0])
}
if detailed {
help = append(help, []string{
CommandHelp(service, "setprivatehelp", "", "Sets help text to be sent through private messages in this channel.")[0],
CommandHelp(service, "setpublichelp", "", "Sets the default help behavior for this channel.")[0],
}...)
}
return help
}
func (p *helpPlugin) Message(bot *Bot, service Service, message Message) {
if !service.IsMe(message) {
if MatchesCommand(service, "help", message) || MatchesCommand(service, "command", message) {
if service.Name() == YouTubeServiceName {
service.SendMessage(message.Channel(), "Visit septapus dot com for help.")
return
}
_, parts := ParseCommand(service, message)
help := []string{}
for _, plugin := range bot.Services[service.Name()].Plugins {
var h []string
if len(parts) == 0 {
h = plugin.Help(bot, service, message, false)
} else if len(parts) == 1 && strings.ToLower(parts[0]) == strings.ToLower(plugin.Name()) {
h = plugin.Help(bot, service, message, true)
}
if h != nil && len(h) > 0 {
help = append(help, h...)
}
}
if len(parts) == 0 {
sort.Strings(help)
if service.SupportsPrivateMessages() {
help = append([]string{fmt.Sprintf("All commands can be used in private messages without the `%s` prefix.", service.CommandPrefix())}, help...)
}
}
if len(parts) != 0 && len(help) == 0 {
help = []string{fmt.Sprintf("Unknown topic: %s", parts[0])}
}
if p.Private[message.Channel()] {
service.SendMessage(message.Channel(), "Help has been sent via private message.")
if service.SupportsMultiline() {
service.PrivateMessage(message.UserID(), strings.Join(help, "\n"))
} else {
for _, h := range help {
if err := service.PrivateMessage(message.UserID(), h); err != nil {
break
}
}
}
} else {
if service.SupportsMultiline() {
service.SendMessage(message.Channel(), strings.Join(help, "\n"))
} else {
for _, h := range help {
if err := service.SendMessage(message.Channel(), h); err != nil {
break
}
}
}
}
} else if MatchesCommand(service, "setprivatehelp", message) && service.SupportsPrivateMessages() && !service.IsPrivate(message) {
if !service.IsModerator(message) {
return
}
p.Private[message.Channel()] = true
service.PrivateMessage(message.UserID(), fmt.Sprintf("Help text in <#%s> will be sent through private messages.", message.Channel()))
} else if MatchesCommand(service, "setpublichelp", message) && service.SupportsPrivateMessages() && !service.IsPrivate(message) {
if !service.IsModerator(message) {
return
}
p.Private[message.Channel()] = false
service.PrivateMessage(message.UserID(), fmt.Sprintf("Help text in <#%s> will be sent publically.", message.Channel()))
}
}
}
// Load will load plugin state from a byte array.
func (p *helpPlugin) Load(bot *Bot, service Service, data []byte) error {
if data != nil {
if err := json.Unmarshal(data, p); err != nil {
log.Println("Error loading data", err)
}
}
return nil
}
// Save will save plugin state to a byte array.
func (p *helpPlugin) Save() ([]byte, error) {
return json.Marshal(p)
}
// Stats will return the stats for a plugin.
func (p *helpPlugin) Stats(bot *Bot, service Service, message Message) []string {
return nil
}
// NeHelpPlugin will create a new help plugin.
func NewHelpPlugin() Plugin {
p := &helpPlugin{
Private: make(map[string]bool),
}
return p
}