forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_web.go
417 lines (332 loc) · 15.5 KB
/
plugin_web.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package commands
import (
"database/sql"
"fmt"
"html"
"html/template"
"net/http"
"strconv"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/commands/models"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/web"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries/qm"
"github.com/volatiletech/sqlboiler/types"
"goji.io"
"goji.io/pat"
)
type ChannelOverrideForm struct {
Channels []int64 `valid:"channel,true`
ChannelCategories []int64 `valid:"channel,true`
Global bool
CommandsEnabled bool
AutodeleteResponse bool
AutodeleteTrigger bool
AutodeleteResponseDelay int
AutodeleteTriggerDelay int
RequireRoles []int64 `valid:"role,true"`
IgnoreRoles []int64 `valid:"role,true"`
}
type CommandOverrideForm struct {
Commands []string
CommandsEnabled bool
AutodeleteResponse bool
AutodeleteTrigger bool
AutodeleteResponseDelay int
AutodeleteTriggerDelay int
RequireRoles []int64 `valid:"role,true"`
IgnoreRoles []int64 `valid:"role,true"`
}
func (p *Plugin) InitWeb() {
web.LoadHTMLTemplate("../../commands/assets/commands.html", "templates/plugins/commands.html")
subMux := goji.SubMux()
web.CPMux.Handle(pat.New("/commands/settings"), subMux)
web.CPMux.Handle(pat.New("/commands/settings/*"), subMux)
subMux.Use(web.RequireGuildChannelsMiddleware)
getHandler := web.ControllerHandler(HandleCommands, "cp_commands")
subMux.Handle(pat.Get(""), getHandler)
subMux.Handle(pat.Get("/"), getHandler)
subMux.Handle(pat.Post("/general"), web.ControllerPostHandler(HandlePostCommands, getHandler, nil, "Updated command prefix"))
// Channel override handlers
subMux.Handle(pat.Post("/channel_overrides/new"),
web.ControllerPostHandler(HandleCreateChannelsOverride, getHandler, ChannelOverrideForm{}, "Created a new command channels override"))
subMux.Handle(pat.Post("/channel_overrides/:channelOverride/update"),
web.ControllerPostHandler(ChannelOverrideMiddleware(HandleUpdateChannelsOverride), getHandler, ChannelOverrideForm{}, "Updated a commands channel override"))
subMux.Handle(pat.Post("/channel_overrides/:channelOverride/delete"),
web.ControllerPostHandler(ChannelOverrideMiddleware(HandleDeleteChannelsOverride), getHandler, nil, "Deleted a commands channel override"))
// Command override handlers
subMux.Handle(pat.Post("/channel_overrides/:channelOverride/command_overrides/new"),
web.ControllerPostHandler(ChannelOverrideMiddleware(HandleCreateCommandOverride), getHandler, CommandOverrideForm{}, "Created a commands command override"))
subMux.Handle(pat.Post("/channel_overrides/:channelOverride/command_overrides/:commandsOverride/update"),
web.ControllerPostHandler(ChannelOverrideMiddleware(HandleUpdateCommandOVerride), getHandler, CommandOverrideForm{}, "Updated a commands command override"))
subMux.Handle(pat.Post("/channel_overrides/:channelOverride/command_overrides/:commandsOverride/delete"),
web.ControllerPostHandler(ChannelOverrideMiddleware(HandleDeleteCommandOverride), getHandler, nil, "Deleted a commands command override"))
}
// Servers the command page with current config
func HandleCommands(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
type SortedCommands struct {
Category string
Commands []string
}
// Compile all the commands into a sorted list by category
commands := make([]*SortedCommands, 0, len(CommandSystem.Root.Commands))
addCommand := func(cmd *YAGCommand, name string) {
for _, v := range commands {
if v.Category == cmd.CmdCategory.Name {
v.Commands = append(v.Commands, name)
return
}
}
commands = append(commands, &SortedCommands{
Category: cmd.CmdCategory.Name,
Commands: []string{name},
})
}
for _, cmd := range CommandSystem.Root.Commands {
switch t := cmd.Command.(type) {
case *YAGCommand:
if t.HideFromCommandsPage {
continue
}
addCommand(t, cmd.Trigger.Names[0])
case *dcmd.Container:
for _, containerCmd := range t.Commands {
cast := containerCmd.Command.(*YAGCommand)
if cast.HideFromCommandsPage {
continue
}
addCommand(cast, t.Names[0]+" "+containerCmd.Trigger.Names[0])
}
}
}
templateData["SortedCommands"] = commands
channelOverrides, err := models.CommandsChannelsOverrides(qm.Where("guild_id=?", activeGuild.ID), qm.Load("CommandsCommandOverrides")).AllG(r.Context())
if err != nil {
return templateData, err
}
var global *models.CommandsChannelsOverride
for i, v := range channelOverrides {
if v.Global {
global = v
channelOverrides = append(channelOverrides[:i], channelOverrides[i+1:]...)
break
}
}
if global == nil {
global = &models.CommandsChannelsOverride{
Global: true,
CommandsEnabled: true,
}
}
templateData["GlobalCommandSettings"] = global
templateData["ChannelOverrides"] = channelOverrides
prefix, _ := GetCommandPrefix(activeGuild.ID)
templateData["CommandPrefix"] = prefix
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/commands/settings"
return templateData, nil
}
// Handles the updating of global and per channel command settings
func HandlePostCommands(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
newPrefix := r.FormValue("Prefix")
if len(newPrefix) < 1 || len(newPrefix) > 100 {
return templateData, web.NewPublicError("Prefix is smaller than 1 or larger than 100 characters")
}
common.RedisPool.Do(retryableredis.Cmd(nil, "SET", "command_prefix:"+discordgo.StrID(activeGuild.ID), newPrefix))
return templateData, nil
}
// Channel override handlers
func ChannelOverrideMiddleware(inner func(w http.ResponseWriter, r *http.Request, override *models.CommandsChannelsOverride) (web.TemplateData, error)) web.ControllerHandlerFunc {
return func(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
activeGuild := r.Context().Value(common.ContextKeyCurrentGuild).(*discordgo.Guild)
var override *models.CommandsChannelsOverride
var err error
id := pat.Param(r, "channelOverride")
if id == "global" {
override, err = models.CommandsChannelsOverrides(qm.Where("guild_id = ? AND global=true", activeGuild.ID)).OneG(r.Context())
if err == sql.ErrNoRows {
override = &models.CommandsChannelsOverride{
Global: true,
GuildID: activeGuild.ID,
CommandsEnabled: true,
Channels: []int64{},
RequireRoles: []int64{},
IgnoreRoles: []int64{},
}
// Insert it
err = override.InsertG(r.Context(), boil.Infer())
if err != nil {
logger.WithError(err).Error("Failed inserting global commands row")
// Was inserted somewhere else in the meantime
override, err = models.CommandsChannelsOverrides(qm.Where("guild_id = ? AND global=true", activeGuild.ID)).OneG(r.Context())
}
}
} else {
idParsed, _ := strconv.ParseInt(id, 10, 64)
override, err = models.CommandsChannelsOverrides(qm.Where("guild_id = ? AND id = ?", activeGuild.ID, idParsed)).OneG(r.Context())
}
if err != nil {
return nil, web.NewPublicError("Channels override not found, someone else deledted it in the meantime perhaps? Check control panel logs")
}
return inner(w, r, override)
}
}
func HandleCreateChannelsOverride(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
formData := r.Context().Value(common.ContextKeyParsedForm).(*ChannelOverrideForm)
count, err := models.CommandsChannelsOverrides(qm.Where("guild_id = ?", activeGuild.ID), qm.Where("channels && ?", types.Int64Array(formData.Channels))).CountG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "count")
}
if count > 0 {
return templateData.AddAlerts(web.ErrorAlert("One of the selected channels is already used in another override")), nil
}
count, err = models.CommandsChannelsOverrides(qm.Where("guild_id = ?", activeGuild.ID)).CountG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "count2")
}
if count > 100 {
return templateData.AddAlerts(web.ErrorAlert("Max 100 channel overrides allowed")), nil
}
model := &models.CommandsChannelsOverride{
GuildID: activeGuild.ID,
Channels: formData.Channels,
ChannelCategories: formData.ChannelCategories,
CommandsEnabled: formData.CommandsEnabled,
AutodeleteResponse: formData.AutodeleteResponse,
AutodeleteTrigger: formData.AutodeleteTrigger,
AutodeleteResponseDelay: formData.AutodeleteResponseDelay,
AutodeleteTriggerDelay: formData.AutodeleteTriggerDelay,
RequireRoles: formData.RequireRoles,
IgnoreRoles: formData.IgnoreRoles,
}
err = model.InsertG(r.Context(), boil.Infer())
return templateData, errors.WithMessage(err, "InsertG")
}
func HandleUpdateChannelsOverride(w http.ResponseWriter, r *http.Request, currentOverride *models.CommandsChannelsOverride) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
formData := r.Context().Value(common.ContextKeyParsedForm).(*ChannelOverrideForm)
count, err := models.CommandsChannelsOverrides(
qm.Where("guild_id = ?", activeGuild.ID), qm.Where("channels && ?", types.Int64Array(formData.Channels)), qm.Where("id != ?", currentOverride.ID)).CountG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "count")
}
if count > 0 {
return templateData.AddAlerts(web.ErrorAlert("One of the selected channels is already used in another override")), nil
}
currentOverride.Channels = formData.Channels
currentOverride.ChannelCategories = formData.ChannelCategories
currentOverride.CommandsEnabled = formData.CommandsEnabled
currentOverride.AutodeleteResponse = formData.AutodeleteResponse
currentOverride.AutodeleteTrigger = formData.AutodeleteTrigger
currentOverride.AutodeleteResponseDelay = formData.AutodeleteResponseDelay
currentOverride.AutodeleteTriggerDelay = formData.AutodeleteTriggerDelay
currentOverride.RequireRoles = formData.RequireRoles
currentOverride.IgnoreRoles = formData.IgnoreRoles
_, err = currentOverride.UpdateG(r.Context(), boil.Infer())
return templateData, errors.WithMessage(err, "UpdateG")
}
func HandleDeleteChannelsOverride(w http.ResponseWriter, r *http.Request, currentOverride *models.CommandsChannelsOverride) (web.TemplateData, error) {
_, templateData := web.GetBaseCPContextData(r.Context())
_, err := currentOverride.DeleteG(r.Context())
return templateData, errors.WithMessage(err, "DeleteG")
}
// Command handlers
func HandleCreateCommandOverride(w http.ResponseWriter, r *http.Request, channelOverride *models.CommandsChannelsOverride) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
formData := r.Context().Value(common.ContextKeyParsedForm).(*CommandOverrideForm)
count, err := models.CommandsCommandOverrides(qm.Where("commands_channels_overrides_id = ?", channelOverride.ID), qm.Where("commands && ?", types.StringArray(formData.Commands))).CountG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "count")
}
if count > 0 {
return templateData, web.NewPublicError("One of the selected commands is already used in another command override for this channel override")
}
count, err = models.CommandsCommandOverrides(qm.Where("commands_channels_overrides_id = ?", channelOverride.ID)).CountG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "count2")
}
if count > 250 {
return templateData, web.NewPublicError("Max 250 command overrides")
}
model := &models.CommandsCommandOverride{
GuildID: activeGuild.ID,
CommandsChannelsOverridesID: channelOverride.ID,
Commands: formData.Commands,
CommandsEnabled: formData.CommandsEnabled,
AutodeleteResponse: formData.AutodeleteResponse,
AutodeleteTrigger: formData.AutodeleteTrigger,
AutodeleteResponseDelay: formData.AutodeleteResponseDelay,
AutodeleteTriggerDelay: formData.AutodeleteTriggerDelay,
RequireRoles: formData.RequireRoles,
IgnoreRoles: formData.IgnoreRoles,
}
err = model.InsertG(r.Context(), boil.Infer())
return templateData, errors.WithMessage(err, "InsertG")
}
func HandleUpdateCommandOVerride(w http.ResponseWriter, r *http.Request, channelOverride *models.CommandsChannelsOverride) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
id := pat.Param(r, "commandsOverride")
idParsed, _ := strconv.ParseInt(id, 10, 64)
override, err := models.CommandsCommandOverrides(qm.Where("id = ?", idParsed), qm.Where("guild_id = ?", activeGuild.ID)).OneG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "query override")
}
formData := r.Context().Value(common.ContextKeyParsedForm).(*CommandOverrideForm)
count, err := models.CommandsCommandOverrides(qm.Where("commands_channels_overrides_id = ?", channelOverride.ID), qm.Where("commands && ?", types.StringArray(formData.Commands)), qm.Where("id != ?", override.ID)).CountG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "count")
}
if count > 0 {
return templateData, web.NewPublicError("One of the selected commands is already used in another command override for this channel override")
}
override.Commands = formData.Commands
override.CommandsEnabled = formData.CommandsEnabled
override.AutodeleteResponse = formData.AutodeleteResponse
override.AutodeleteTrigger = formData.AutodeleteTrigger
override.AutodeleteResponseDelay = formData.AutodeleteResponseDelay
override.AutodeleteTriggerDelay = formData.AutodeleteTriggerDelay
override.RequireRoles = formData.RequireRoles
override.IgnoreRoles = formData.IgnoreRoles
_, err = override.UpdateG(r.Context(), boil.Infer())
return templateData, errors.WithMessage(err, "UpdateG")
}
func HandleDeleteCommandOverride(w http.ResponseWriter, r *http.Request, channelOverride *models.CommandsChannelsOverride) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
id := pat.Param(r, "commandsOverride")
idParsed, _ := strconv.ParseInt(id, 10, 64)
override, err := models.CommandsCommandOverrides(qm.Where("id = ?", idParsed), qm.Where("guild_id = ?", activeGuild.ID)).OneG(r.Context())
if err != nil {
return templateData, errors.WithMessage(err, "query override")
}
_, err = override.DeleteG(r.Context())
return templateData, errors.WithMessage(err, "DeleteG")
}
var _ web.PluginWithServerHomeWidget = (*Plugin)(nil)
func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ag, templateData := web.GetBaseCPContextData(r.Context())
templateData["WidgetTitle"] = "Commands"
templateData["SettingsPath"] = "/commands/settings"
templateData["WidgetEnabled"] = true
prefix, err := GetCommandPrefix(ag.ID)
if err != nil {
return templateData, err
}
count, err := models.CommandsChannelsOverrides(qm.Where("guild_id=?", ag.ID), qm.Where("global=false")).CountG(r.Context())
if err != nil {
return templateData, err
}
const format = `<ul>
<li>Command prefix: <code>%s</code></li>
<li>Active channel overrides: <code>%d</code></li>
</ul>`
templateData["WidgetBody"] = template.HTML(fmt.Sprintf(format, html.EscapeString(prefix), count))
return templateData, nil
}