forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
150 lines (117 loc) · 4.38 KB
/
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
package customcommands
import (
"fmt"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/web"
"github.com/mediocregopher/radix.v3"
"goji.io"
"goji.io/pat"
"html/template"
"net/http"
"unicode/utf8"
)
func (p *Plugin) InitWeb() {
tmplPathSettings := "templates/plugins/customcommands.html"
if common.Testing {
tmplPathSettings = "../../customcommands/assets/customcommands.html"
}
web.Templates = template.Must(web.Templates.ParseFiles(tmplPathSettings))
getHandler := web.ControllerHandler(HandleCommands, "cp_custom_commands")
subMux := goji.SubMux()
web.CPMux.Handle(pat.New("/customcommands"), subMux)
web.CPMux.Handle(pat.New("/customcommands/*"), subMux)
subMux.Use(web.RequireGuildChannelsMiddleware)
subMux.Use(web.RequireFullGuildMW)
subMux.Handle(pat.Get(""), getHandler)
subMux.Handle(pat.Get("/"), getHandler)
newHandler := web.ControllerPostHandler(HandleNewCommand, getHandler, CustomCommand{}, "Created a new custom command")
subMux.Handle(pat.Post(""), newHandler)
subMux.Handle(pat.Post("/"), newHandler)
subMux.Handle(pat.Post("/:cmd/update"), web.ControllerPostHandler(HandleUpdateCommand, getHandler, CustomCommand{}, "Updated a custom command"))
subMux.Handle(pat.Post("/:cmd/delete"), web.ControllerHandler(HandleDeleteCommand, "cp_custom_commands"))
}
func HandleCommands(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
_, ok := templateData["CustomCommands"]
if !ok {
commands, _, err := GetCommands(activeGuild.ID)
if err != nil {
return templateData, err
}
templateData["CustomCommands"] = commands
}
return templateData, nil
}
func HandleNewCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/customcommands/"
newCmd := ctx.Value(common.ContextKeyParsedForm).(*CustomCommand)
currentCommands, highest, err := GetCommands(activeGuild.ID)
if err != nil {
return templateData, err
}
if len(currentCommands) >= MaxCommandsForContext(ctx) {
return templateData, web.NewPublicError(fmt.Sprintf("Max %d custom commands allowed (or %d for premium servers)", MaxCommands, MaxCommandsPremium))
}
templateData["CustomCommands"] = currentCommands
newCmd.TriggerType = TriggerTypeFromForm(newCmd.TriggerTypeForm)
newCmd.ID = highest + 1
err = newCmd.Save(activeGuild.ID)
if err != nil {
return templateData, err
}
templateData["CustomCommands"] = append(currentCommands, newCmd)
return templateData, nil
}
func HandleUpdateCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/customcommands/"
cmd := ctx.Value(common.ContextKeyParsedForm).(*CustomCommand)
// Validate that they haven't messed with the id
var exists bool
common.RedisPool.Do(radix.FlatCmd(&exists, "HEXISTS", KeyCommands(activeGuild.ID), cmd.ID))
if !exists {
return templateData, web.NewPublicError("That command dosen't exist?")
}
cmd.TriggerType = TriggerTypeFromForm(cmd.TriggerTypeForm)
err := cmd.Save(activeGuild.ID)
return templateData, err
}
func HandleDeleteCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/customcommands/"
cmdIndex := pat.Param(r, "cmd")
err := common.RedisPool.Do(radix.Cmd(nil, "HDEL", KeyCommands(activeGuild.ID), cmdIndex))
if err != nil {
return templateData, err
}
user := ctx.Value(common.ContextKeyUser).(*discordgo.User)
go common.AddCPLogEntry(user, activeGuild.ID, "Deleted command #"+cmdIndex)
return HandleCommands(w, r)
}
func TriggerTypeFromForm(str string) CommandTriggerType {
switch str {
case "prefix":
return CommandTriggerStartsWith
case "regex":
return CommandTriggerRegex
case "contains":
return CommandTriggerContains
case "exact":
return CommandTriggerExact
default:
return CommandTriggerCommand
}
}
func CheckLimits(in ...string) bool {
for _, v := range in {
if utf8.RuneCountInString(v) > 2000 {
return false
}
}
return true
}