-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
147 lines (112 loc) · 2.43 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
package main
import (
"bytes"
"html/template"
"log"
"github.com/RumbleFrog/discordgo"
)
var (
// Data: messageTemplateData
messageTemplate *template.Template
// Data: messageContentTemplateData
messageContentTemplate *template.Template
// Data: []guildsTemplateData
guildTemplate *template.Template
// Data: []categoriesTemplateData
channelsTemplate *template.Template
)
type messageTemplateData struct {
ID int64
Timestamp string
AuthorID int64
AvatarName string
DisplayName string
NameColor string
Blocked bool
Edited bool
Message template.HTML
}
type messageContentTemplateData struct {
Content template.HTML
Attachments []messageAttachmentTemplateData
Embeds []messageEmbedTemplateData
}
type MediaType string
const (
TypeFile MediaType = "file"
TypeAudio MediaType = "audio"
TypeVideo MediaType = "video"
TypeImage MediaType = "image"
)
type messageAttachmentTemplateData struct {
MediaType MediaType
Name string
URL string
Original string
Size string
}
type messageEmbedTemplateData struct {
PillColor string
Title string
TitleURL string
Author string
AuthorURL string
AuthorIcon string
ThumbnailURL string
ThumbnailOriginal string
ThumbnailWidth int
ThumbnailHeight int
Description template.HTML
Fields []*discordgo.MessageEmbedField
// Footer already includes timestamp
Footer string
FooterIcon string
ImageURL string
ImageOriginal string
ImageWidth int
ImageHeight int
VideoURL string
VideoOriginal string
// Todo: find out what Provider is
}
type guildsTemplateData struct {
ID int64
URL string
Name string
}
type categoriesTemplateData struct {
CategoryID int64
CategoryName string
Channels []channelsTemplateData
}
type channelsTemplateData struct {
ID int64
Name string
NSFW bool
Muted bool
}
// RenderToString converts the appropriate option to template string
// using reflection magic
func RenderToString(data interface{}) string {
var (
b bytes.Buffer
err error
)
switch data.(type) {
case messageTemplateData:
err = messageTemplate.Execute(&b, data)
case messageContentTemplateData:
err = messageContentTemplate.Execute(&b, data)
case []guildsTemplateData:
err = guildTemplate.Execute(&b, data)
case []categoriesTemplateData:
err = channelsTemplate.Execute(&b, data)
default:
return ""
}
if err != nil {
log.Println(err)
return ""
}
return b.String()
}