forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
339 lines (283 loc) · 9.14 KB
/
template.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
package web
import (
"bytes"
"errors"
"fmt"
"html/template"
"strings"
"time"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/templates"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
)
func prettyTime(t time.Time) string {
return t.Format(time.RFC822)
}
// mTemplate combines "template" with dictionary. so you can specify multiple variables
// and call templates almost as if they were functions with arguments
// makes certain templates a lot simpler
func mTemplate(name string, values ...interface{}) (template.HTML, error) {
data, err := templates.Dictionary(values...)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = Templates.ExecuteTemplate(&buf, name, data)
if err != nil {
return "", err
}
return template.HTML(buf.String()), nil
}
var permsString = map[string]int64{
"ManageRoles": discordgo.PermissionManageRoles,
"ManageMessages": discordgo.PermissionManageMessages,
}
func hasPerm(botPerms int64, checkPerm string) (bool, error) {
p, ok := permsString[checkPerm]
if !ok {
return false, errors.New("Unknown permission: " + checkPerm)
}
return botPerms&p != 0, nil
}
// tmplRoleDropdown is a template function for generating role dropdown options
// roles: slice of roles to display options for
// highestBotRole: the bot's highest role, if not nil will disable roles above this one.
// args are optinal and in this order:
// 1. current selected roleid
// 2. default empty display name
// 3. default unknown display name
func tmplRoleDropdown(roles []discordgo.Role, highestBotRole *discordgo.Role, args ...interface{}) template.HTML {
hasCurrentSelected := len(args) > 0
var currentSelected int64
if hasCurrentSelected {
currentSelected = templates.ToInt64(args[0])
}
hasEmptyName := len(args) > 1
emptyName := ""
if hasEmptyName {
emptyName = templates.ToString(args[1])
}
hasUnknownName := len(args) > 2
unknownName := "Unknown role (deleted most likely)"
if hasUnknownName {
emptyName = templates.ToString(args[2])
}
output := ""
if hasEmptyName {
output += `<option value=""`
if currentSelected == 0 {
output += `selected`
}
output += ">" + template.HTMLEscapeString(emptyName) + "</option>\n"
output += `<optgroup label="────────────"></optgroup>`
}
found := false
for k, role := range roles {
// Skip the everyone role
if k == len(roles)-1 {
break
}
if role.Managed && highestBotRole != nil {
continue
}
output += `<option value="` + discordgo.StrID(role.ID) + `"`
if role.ID == currentSelected {
output += " selected"
found = true
}
if role.Color != 0 {
hexColor := fmt.Sprintf("%06x", int64(role.Color))
output += " style=\"color: #" + hexColor + "\""
}
optName := template.HTMLEscapeString(role.Name)
if highestBotRole != nil {
if common.IsRoleAbove(&role, highestBotRole) || role.ID == highestBotRole.ID {
output += " disabled"
optName += " (role is above bot)"
}
}
output += ">" + optName + "</option>\n"
}
if !found && currentSelected != 0 {
output += `<option value="` + discordgo.StrID(currentSelected) + `" selected>` + unknownName + "</option>\n"
}
return template.HTML(output)
}
// Same as tmplRoleDropdown but supports multiple selections
func tmplRoleDropdownMutli(roles []discordgo.Role, highestBotRole *discordgo.Role, selections []int64) template.HTML {
var builder strings.Builder
// show deleted roles
OUTER:
for _, sr := range selections {
for _, gr := range roles {
if sr == gr.ID {
continue OUTER
}
}
builder.WriteString(fmt.Sprintf(`<option value="%[1]d" selected>Deleted role: %[1]d</option>\n`, sr))
}
for k, role := range roles {
// Skip the everyone role
if k == len(roles)-1 {
break
}
// Allow the selection of managed roles in cases where we do not assign them (for filters and such for example)
if role.Managed && highestBotRole != nil {
continue
}
optIsSelected := false
builder.WriteString(`<option value="` + discordgo.StrID(role.ID) + `"`)
for _, selected := range selections {
if selected == role.ID {
builder.WriteString(" selected")
optIsSelected = true
}
}
if role.Color != 0 {
hexColor := fmt.Sprintf("%06x", int64(role.Color))
builder.WriteString(" data-color=\"#" + hexColor + "\"")
}
optName := template.HTMLEscapeString(role.Name)
if highestBotRole != nil {
if common.IsRoleAbove(&role, highestBotRole) || highestBotRole.ID == role.ID {
if !optIsSelected {
builder.WriteString(" disabled")
}
optName += " (role is above bot)"
}
}
builder.WriteString(">" + optName + "</option>\n")
}
return template.HTML(builder.String())
}
func tmplChannelOpts(channelTypes []discordgo.ChannelType) interface{} {
optsBuilder := tmplChannelOptsMulti(channelTypes)
return func(channels []dstate.ChannelState, selection interface{}, allowEmpty bool, emptyName string) template.HTML {
var builder strings.Builder
if allowEmpty {
if emptyName == "" {
emptyName = "None"
}
builder.WriteString(`<option value=""`)
if selection == 0 {
builder.WriteString(" selected")
}
builder.WriteString(">" + template.HTMLEscapeString(emptyName) + "</option>")
}
var selections []int64
intSel := templates.ToInt64(selection)
if intSel != 0 {
selections = []int64{intSel}
}
// Generate the rest of the options, which is the same as multi but with a single selections
builder.WriteString(string(optsBuilder(channels, selections)))
return template.HTML(builder.String())
}
}
func tmplChannelOptsMulti(allowedChannelTypes []discordgo.ChannelType) func(channels []dstate.ChannelState, selections []int64) template.HTML {
return func(channels []dstate.ChannelState, selections []int64) template.HTML {
gen := &channelOptsHTMLGenState{allowedChannelTypes: allowedChannelTypes, channels: channels, selections: selections}
return gen.HTML()
}
}
type channelOptsHTMLGenState struct {
allowedChannelTypes []discordgo.ChannelType
channels []dstate.ChannelState
selections []int64
buf strings.Builder
}
func (g *channelOptsHTMLGenState) HTML() template.HTML {
g.outputDeletedChannels()
g.outputUncategorizedChannels()
if len(g.allowedChannelTypes) > 1 || g.allowedChannelTypes[0] != discordgo.ChannelTypeGuildCategory {
g.outputCategorizedChannels()
}
return template.HTML(g.buf.String())
}
func (g *channelOptsHTMLGenState) outputDeletedChannels() {
exists := make(map[int64]bool)
for _, c := range g.channels {
exists[c.ID] = true
}
for _, sel := range g.selections {
if !exists[sel] {
g.output(fmt.Sprintf(`<option value="%[1]d" selected class="deleted-channel">Deleted channel: %[1]d</option>\n`, sel))
}
}
}
func (g *channelOptsHTMLGenState) outputUncategorizedChannels() {
for _, c := range g.channels {
if c.ParentID == 0 && g.include(c.Type) {
g.outputChannel(c.ID, c.Name, c.Type)
}
}
}
func (g *channelOptsHTMLGenState) outputCategorizedChannels() {
for _, cat := range g.channels {
if cat.Type != discordgo.ChannelTypeGuildCategory {
continue
}
g.output(`<optgroup label="` + template.HTMLEscapeString(cat.Name) + `">`)
for _, c := range g.channels {
if c.ParentID == cat.ID && g.include(c.Type) {
g.outputChannel(c.ID, c.Name, c.Type)
}
}
g.output("</optgroup>")
}
}
func (g *channelOptsHTMLGenState) include(channelType discordgo.ChannelType) bool {
for _, t := range g.allowedChannelTypes {
if t == channelType {
return true
}
}
return false
}
func (g *channelOptsHTMLGenState) outputChannel(id int64, name string, channelType discordgo.ChannelType) {
g.output(`<option value="` + discordgo.StrID(id) + `"`)
for _, selected := range g.selections {
if selected == id {
g.output(" selected")
}
}
var prefix string
switch channelType {
case discordgo.ChannelTypeGuildText:
prefix = "#"
case discordgo.ChannelTypeGuildVoice:
prefix = "🔊"
case discordgo.ChannelTypeGuildForum:
prefix = "📃"
default:
prefix = ""
}
g.output(">" + template.HTMLEscapeString(prefix+name) + "</option>")
}
func (g *channelOptsHTMLGenState) output(s string) {
g.buf.WriteString(s)
}
func tmplCheckbox(name, id, description string, checked bool, extraInputAttrs ...string) template.HTML {
// const code = `<div class="checkbox-custom checkbox-primary">
// <input type="checkbox" checked="" id="checkboxExample2">
// <label for="checkboxExample2">Checkbox Primary</label>
// </div>`
// <input class="tgl tgl-flat" id="cb4" type="checkbox"/>
// <label class="tgl-btn" for="cb4"></label>
var builder strings.Builder
builder.WriteString(`<div class="form-group d-flex align-items-center">`)
builder.WriteString(`<input type="checkbox" class="tgl tgl-flat"`)
builder.WriteString(` name="` + name + `" id="` + id + `"`)
if checked {
builder.WriteString(" checked")
}
if len(extraInputAttrs) > 0 {
builder.WriteString(" " + strings.Join(extraInputAttrs, " "))
}
builder.WriteString(`><label for="` + id + `" class="tgl-btn mb-2"></label>`)
// builder.WriteString(`><div class="switch"></div>`)
builder.WriteString(`<span class="tgl-desc ml-2 mb-2">` + description + `</span></div>`)
// builder.WriteString(`</div>`)
return template.HTML(builder.String())
}