-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatting.go
227 lines (181 loc) · 5.9 KB
/
formatting.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
package welcomer
import (
"fmt"
"html"
"strconv"
"strings"
"time"
"github.com/Knetic/govaluate"
"github.com/WelcomerTeam/Discord/discord"
mustache "github.com/WelcomerTeam/Mustachvulate"
sandwich "github.com/WelcomerTeam/Sandwich/sandwich"
)
// ordinal takes 1 argument but 0 was given
// ordinal argument 1 type "string" is not supported
func AssertLength(name string, expectedLength int, arguments ...interface{}) (err error) {
if len(arguments) != expectedLength {
return fmt.Errorf("%s takes %d argument(s) but %d was given", name, expectedLength, len(arguments))
}
return nil
}
func GatherFunctions() (funcs map[string]govaluate.ExpressionFunction) {
funcs = map[string]govaluate.ExpressionFunction{}
funcs["Ordinal"] = func(arguments ...interface{}) (interface{}, error) {
if err := AssertLength("Ordinal", 1, arguments...); err != nil {
return nil, err
}
argument, ok := arguments[0].(float64)
if !ok {
return nil, fmt.Errorf("ordinal argument 1 is not supported")
}
var suffix string
switch int64(argument) % 10 {
case 1:
suffix = "st"
case 2:
suffix = "nd"
case 3:
suffix = "rd"
default:
suffix = "th"
}
return Itoa(int64(argument)) + suffix, nil
}
return
}
func GatherVariables(eventCtx *sandwich.EventContext, member discord.GuildMember, guild discord.Guild, extraValues map[string]interface{}) (vars map[string]interface{}) {
vars = make(map[string]interface{})
vars["User"] = StubUser{
ID: member.User.ID,
Name: GetUserDisplayName(member.User),
Username: member.User.Username,
Discriminator: member.User.Discriminator,
GlobalName: member.User.GlobalName,
Mention: "<@" + member.User.ID.String() + ">",
CreatedAt: StubTime(member.User.ID.Time()),
JoinedAt: StubTime(member.JoinedAt),
Avatar: GetUserAvatar(member.User),
Bot: member.User.Bot,
Pending: member.Pending,
}
vars["Guild"] = StubGuild{
ID: guild.ID,
Name: guild.Name,
Icon: getGuildIcon(guild),
Splash: getGuildSplash(guild),
Members: guild.MemberCount,
Banner: getGuildBanner(guild),
}
for key, value := range extraValues {
vars[key] = value
}
return vars
}
func FormatString(funcs map[string]govaluate.ExpressionFunction, vars map[string]interface{}, message string) (string, error) {
tmpl, err := mustache.ParseString(message)
if err != nil {
return "", fmt.Errorf("failed to parse string: %v", err)
}
out, err := tmpl.Render(funcs, vars)
if err != nil {
return "", fmt.Errorf("failed to format string: %v", err)
}
return html.UnescapeString(out), nil
}
func getGuildIcon(guild discord.Guild) string {
if guild.Icon == "" {
return ""
}
if strings.HasPrefix(guild.Icon, "a_") {
return discord.EndpointCDN + "/" + discord.EndpointGuildIconAnimated(guild.ID.String(), guild.Icon)
}
return discord.EndpointCDN + "/" + discord.EndpointGuildIcon(guild.ID.String(), guild.Icon)
}
func getGuildSplash(guild discord.Guild) string {
if guild.Splash == "" {
return ""
}
return discord.EndpointCDN + "/" + discord.EndpointGuildSplash(guild.ID.String(), guild.Splash)
}
func getGuildBanner(guild discord.Guild) string {
if guild.Banner == "" {
return ""
}
return discord.EndpointCDN + "/" + discord.EndpointGuildBanner(guild.ID.String(), guild.Banner)
}
func GetUserAvatar(user *discord.User) string {
if user.Avatar == "" {
if user.Discriminator == "" {
// If a user is on the new username system, the index is (user_id >> 22) % 6
return discord.EndpointCDN + "/" + discord.EndpointDefaultUserAvatar(Itoa((int64(user.ID)>>22)%6))
}
// If a user is on the old username system, the index is discriminator % 5
discriminator, err := strconv.ParseInt(user.Discriminator, int64Base, int64BitSize)
if err != nil {
discriminator = 0
}
return discord.EndpointCDN + "/" + discord.EndpointDefaultUserAvatar(Itoa(discriminator%5))
}
if strings.HasPrefix(user.Avatar, "a_") {
// If the avatar has the prefix a_, it is animated.
return discord.EndpointCDN + "/" + discord.EndpointUserAvatarAnimated(user.ID.String(), user.Avatar)
}
return discord.EndpointCDN + "/" + discord.EndpointUserAvatar(user.ID.String(), user.Avatar)
}
func GetGuildMemberDisplayName(member discord.GuildMember) string {
if member.Nick != "" {
return member.Nick
}
return GetUserDisplayName(member.User)
}
func GetUserDisplayName(user *discord.User) string {
if user.GlobalName != "" {
return user.GlobalName
}
if user.Discriminator != "" && user.Discriminator != "0" {
return user.Username + "#" + user.Discriminator
}
return user.Username
}
// StubUser represents a user on discord.
type StubUser struct {
CreatedAt StubTime `json:"created_at"`
JoinedAt StubTime `json:"joined_at"`
Name string `json:"name"`
Username string `json:"username"`
Discriminator string `json:"discriminator"`
GlobalName string `json:"global_name"`
Mention string `json:"mention"`
Avatar string `json:"avatar"`
ID discord.Snowflake `json:"id"`
Bot bool `json:"bot"`
Pending bool `json:"pending"`
}
func (s StubUser) String() string {
if s.GlobalName != "" {
return s.GlobalName
}
if s.Discriminator != "" && s.Discriminator != "0" {
return s.Username + "#" + s.Discriminator
}
return s.Username
}
// Guild represents a guild on discord.
type StubGuild struct {
Name string `json:"name"`
Icon string `json:"icon"`
Splash string `json:"splash"`
Banner string `json:"banner"`
ID discord.Snowflake `json:"id"`
Members int32 `json:"members"`
}
func (s StubGuild) String() string {
return s.Name
}
type StubTime time.Time
func (s StubTime) String() string {
return s.Relative()
}
func (s StubTime) Relative() string {
return "<t:" + Itoa(time.Time(s).Unix()) + ":R>"
}