This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpmodule.go
196 lines (166 loc) · 4.75 KB
/
helpmodule.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package snorlax
import (
"math"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/omarhachach/snorlax/utils"
)
func init() {
helpModule := &Module{
Name: "Help",
Desc: "Help gives information about a command, or displays a list of commands/modules.",
Commands: map[string]*Command{},
Init: helpInit,
}
helpCommand := &Command{
Command: ".help",
Alias: ".h",
Desc: "Help shows you a help menu for a given module, or a list of modules.",
Usage: ".help [command/module] [page]",
ModuleName: helpModule.Name,
Handler: helpHandler,
}
aboutCommand := &Command{
Command: ".about",
Desc: "Will tell you about the bot project.",
Usage: ".about",
ModuleName: helpModule.Name,
Handler: aboutHandler,
}
helpModule.Commands[helpCommand.Command] = helpCommand
helpModule.Commands[aboutCommand.Command] = aboutCommand
internalModules[helpModule.Name] = helpModule
}
var (
helpModules *discordgo.MessageEmbed
helpModuleCommands = map[string][]*discordgo.MessageEmbed{}
)
func helpInit(s *Snorlax) {
botCommands = s.Commands
helpModuleList := ""
noDescList := ""
for moduleName, module := range s.Modules {
if module.Desc != "" {
helpModuleList += moduleName + " - " + module.Desc + "\n"
} else {
noDescList += moduleName + "\n"
}
numOfPages := int(math.Ceil(float64(len(module.Commands)) / float64(20)))
commandPages := make([]*discordgo.MessageEmbed, 0, numOfPages)
for i := 0; i < numOfPages; i++ {
commandPages = append(commandPages, &discordgo.MessageEmbed{
Color: InfoColor,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Command - Description",
Value: "",
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Use `.help [command]` for help on a command.",
},
})
}
n := 0
for commandName, command := range module.Commands {
n++
page := int(math.Floor(float64(n) / float64(20)))
commandPages[page].Fields[0].Name = "Command - Description [" + strconv.Itoa(page+1) + "/" + strconv.Itoa(numOfPages) + "]"
commandPages[page].Fields[0].Value += commandName + " - " + command.Desc + "\n"
}
helpModuleCommands[strings.ToLower(moduleName)] = commandPages
}
helpModuleList += noDescList
helpModules = &discordgo.MessageEmbed{
Color: InfoColor,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Module Name - Description",
Value: helpModuleList,
Inline: false,
},
},
Footer: &discordgo.MessageEmbedFooter{},
}
return
}
var botCommands map[string]*Command
var footText = false
func helpHandler(ctx *Context) {
parts := utils.GetStringFromQuotes(strings.Split(ctx.Message.Content, " "))
switch len(parts) {
case 1:
footText = !footText
if footText {
helpModules.Footer.Text = "TIP: Use `.help [module] [page]` to show a list of command for a module."
} else {
helpModules.Footer.Text = "TIP: Surround the module's name with \"quotes\" if it spans multiple spaces."
}
ctx.SendEmbed(helpModules)
return
case 2:
command, ok := botCommands[strings.ToLower("."+parts[1])]
if !ok {
commandList, ok := helpModuleCommands[strings.ToLower(parts[1])]
if !ok {
ctx.SendErrorMessage("Command or module \"" + parts[1] + "\" does not exist.")
return
}
ctx.SendEmbed(commandList[0])
return
}
ctx.SendEmbed(&discordgo.MessageEmbed{
Color: InfoColor,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Command Help",
Value: command.Desc + "\n\nUsage:\n`" + command.Usage + "`",
Inline: false,
},
},
})
return
case 3:
commandList, ok := helpModuleCommands[strings.ToLower(parts[1])]
if !ok {
ctx.SendErrorMessage("Module \"" + parts[1] + "\" does not exist.")
return
}
pageNum, err := strconv.Atoi(parts[2])
if err != nil {
ctx.SendErrorMessage("\"" + parts[2] + "\"isn't a valid number.")
ctx.Log.WithError(err).Debug("Error parsing number.")
return
}
if len(commandList) < pageNum {
ctx.SendErrorMessage("Module \"" + parts[1] + "\" only has " + strconv.Itoa(len(commandList)) + " page(s).")
return
}
if pageNum < 1 {
ctx.SendErrorMessage("Please specify a page greater than 0.")
return
}
ctx.SendEmbed(commandList[pageNum-1])
return
default:
ctx.Log.Debugf("Wrong number of args: %#v", parts)
return
}
}
var aboutEmbed = &discordgo.MessageEmbed{
Color: InfoColor,
Fields: []*discordgo.MessageEmbedField{
{
Name: "About",
Value: "Hi, I'm Snorlax, a general purpose bot written in Go.\n\n" +
"I am developed by Omar H., and I am open-source!\n" +
"Support my development by contributing: https://github.com/omarhachach/snorlax\n\n" +
"Thank you very much :D",
},
},
}
func aboutHandler(ctx *Context) {
ctx.SendEmbed(aboutEmbed)
}