-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcommand.go
321 lines (305 loc) · 8.35 KB
/
command.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
prefix string
prefixPattern *regexp.Regexp
)
type Command struct {
Name string
Aliases []string
Description string
Usage []string
RoleNeeded *Role
Handler func(caller *discordgo.Member, message *discordgo.Message, args []string) error
}
// List of commands
var Commands = []Command{
{
Name: "optout",
Description: "Opt out of our terms and leave the server permanently",
Usage: []string{"i am sure"},
Handler: optOutHandler,
},
{
Name: "tempmute",
Aliases: []string{"tm"},
Description: "Mute someone temporarily, optionally from a specific channel",
Usage: []string{"@user reason", "@user #channel reason", "#channel @user reason"},
RoleNeeded: &Support,
Handler: muteHandler,
},
{
Name: "mute",
Aliases: []string{"m"},
Description: "Mute someone permanently, optionally from a specific channel",
Usage: []string{"@user reason", "@user #channel reason", "#channel @user reason"},
RoleNeeded: &Moderator,
Handler: muteHandler,
},
{
Name: "unmute",
Aliases: []string{"um"},
Description: "Unmute someone, either server-wide, from a specific channel, or remove all mutes",
Usage: []string{"@user", "@user #channel", "@user all"},
RoleNeeded: &Moderator,
Handler: unmuteHandler,
},
{
Name: "kick",
Description: "Kick someone from the server",
Usage: []string{"@user reason"},
RoleNeeded: &Moderator,
Handler: rektHandler,
},
{
Name: "ban",
Aliases: []string{"rm"},
Description: "Ban someone from the server",
Usage: []string{"@user reason"},
RoleNeeded: &Moderator,
Handler: rektHandler,
},
{
Name: "rules",
Aliases: []string{"rule", "r"},
Description: "Display the rules or a specific rule. Optionally @mention a user to tag them in the response.",
Usage: []string{
"",
"<number>",
"<search term>",
},
Handler: rulesHandler,
},
{
Name: "want",
Description: "want a nick",
Usage: []string{
"<number>",
},
RoleNeeded: &Support,
Handler: wantHandler,
},
{
Name: "cringe",
Aliases: []string{"c"},
Description: "Generates a random cringe image",
Usage: []string{""},
Handler: handleCringe,
},
{
Name: "addcringe",
Aliases: []string{"ac"},
Description: "Adds a cringe photo to the collection",
Usage: []string{"", "url"},
RoleNeeded: &Support,
Handler: handleAddCringe,
},
{
Name: "delcringe",
Aliases: []string{"dc"},
Description: "Removes a cringe photo from the collection",
Usage: []string{"", "url"},
RoleNeeded: &Moderator,
Handler: handleDelCringe,
},
{
Name: "genkey",
Aliases: []string{"gk"},
Description: "Generates an Impact premium key",
Usage: []string{"", "role [...roles]"},
RoleNeeded: &SeniorMod,
Handler: genkey,
},
{
Name: "giveaway",
Description: "Gives you the giveaway role",
Usage: []string{""},
Handler: giveaway,
},
{
Name: "ungiveaway",
Description: "Removes the giveaway role",
Usage: []string{""},
Handler: ungiveaway,
},
{
Name: "stupid",
Description: "makes you so stupid impcat bot will ignore you",
Usage: []string{""},
Handler: stupid,
},
{
Name: "unstupid",
Description: "no more stupid",
Usage: []string{""},
Handler: unstupid,
},
{
Name: "funny",
Aliases: []string{"unfunny"},
Description: "didnt laugh",
Usage: []string{""},
Handler: handleFunny,
},
{
Name: "chess",
Description: "chess mode! gives you chess role",
Usage: []string{""},
Handler: chess,
},
{
Name: "unchess",
Description: "no more chess",
Usage: []string{""},
Handler: unchess,
},
}
func init() {
// Load prefix from the environment
prefix = os.Getenv("IMPACT_PREFIX")
if prefix == "" {
prefix = "i!"
}
// Match case-insensitive & ignore whitespace around prefix
prefixPattern = regexp.MustCompile(`(?i)^\s*` + regexp.QuoteMeta(prefix) + `\s*`)
// Have to append helpCommand after initializing Commands to avoid an initialization loop
Commands = append(Commands, helpCommand)
}
func onMessageSentCommandHandler(session *discordgo.Session, m *discordgo.MessageCreate) {
msg := m.Message
if msg == nil || msg.Author == nil || msg.Type != discordgo.MessageTypeDefault || msg.Author.ID == myselfID {
return // wtf
}
if msg.GuildID != impactServer && msg.GuildID != "" {
return // Only allow guild messages and DMs
}
content := msg.Content
content = strings.Replace(content, ">", "> ", -1)
content = strings.Replace(content, "<", " <", -1)
if match := prefixPattern.FindString(content); match != "" { // bot woke
args := strings.Fields(content[len(match):])
command := findCommand(strings.ToLower(args[0]))
if command == nil {
_ = resp(msg.ChannelID, fmt.Sprintf("Command \"%s\" not found! Try %shelp", args[0], prefix))
return
}
author, err := GetMember(msg.Author.ID)
if err != nil {
return
}
if command.RoleNeeded != nil && !IsUserAtLeast(author, *command.RoleNeeded) {
_ = resp(msg.ChannelID, fmt.Sprintf("Command \"%s\" requires at least %s", command.Name, command.RoleNeeded.Name))
return
}
err = command.Handler(author, msg, args)
if err != nil {
_ = resp(msg.ChannelID, fmt.Sprintf("Command \"%s\" returned an error: %s", command.Name, err.Error()))
return
}
}
}
func findCommand(command string) *Command {
for _, it := range Commands {
if command == it.Name {
return &it
}
for _, alias := range it.Aliases {
if command == alias {
return &it
}
}
}
return nil
}
var helpCommand = Command{
Name: "help",
Description: "Display this help message. Specify `all` to include commands you don't have permission to run. Specify a command's name or alias to see help for only that specific command.",
Aliases: []string{"?"},
Usage: []string{
"",
"all",
"<command>",
},
Handler: func(caller *discordgo.Member, message *discordgo.Message, args []string) error {
embed := discordgo.MessageEmbed{
Color: prettyembedcolor,
Fields: []*discordgo.MessageEmbedField{},
}
// all is true if the user asked for commands they don't have permission for
all := len(args) == 2 && strings.ToLower(args[1]) == "all"
if len(args) < 2 || all {
// All commands
embed.Title = "ImpactBot help"
embed.Description = "Available commands:"
if all {
embed.Description = "All commands:"
}
for _, command := range Commands {
// Include this command if the user has permission to run it or they asked for "all" commands
if all || command.RoleNeeded == nil || IsUserAtLeast(caller, *command.RoleNeeded) {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: command.Name,
Value: command.helpText(),
})
}
}
} else {
// Specified commands
command := findCommand(args[1])
if command == nil {
return errors.New(fmt.Sprintf("Command \"%s\" not found! Try %shelp", args[1], prefix))
}
embed.Title = fmt.Sprintf("Command `%s` usage", command.Name)
embed.Description = command.helpText()
embed.Footer = &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("See %shelp for alternative commands", prefix),
}
}
_, err := discord.ChannelMessageSendEmbed(message.ChannelID, &embed)
if err != nil {
return err
}
return nil
},
}
func (c Command) helpText() string {
var desc strings.Builder
// Print aliases first
if len(c.Aliases) > 0 {
desc.WriteString("\n_Alias")
if len(c.Aliases) > 1 {
// plural meme
desc.WriteString("es")
}
desc.WriteString(": ")
for _, alias := range c.Aliases {
desc.WriteString(fmt.Sprintf("**%s** ", alias))
}
desc.WriteString("_\n")
}
// Then usages
if len(c.Usage) > 0 {
desc.WriteString("```\n")
for _, usage := range c.Usage {
desc.WriteString(fmt.Sprintf("%s%s %s\n", prefix, c.Name, usage))
}
desc.WriteString("```")
}
// Then description
desc.WriteString(c.Description)
if !strings.HasSuffix(c.Description, ".") {
desc.WriteString(".")
}
// Then, finally, required permissions
if c.RoleNeeded != nil {
desc.WriteString(fmt.Sprintf("\nRequires `%s` or higher", c.RoleNeeded.Name))
}
return desc.String()
}