-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
373 lines (342 loc) · 15 KB
/
response.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
package eapi
import (
"encoding/json"
//"fmt"
"io/ioutil"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
var (
patternDataKey = regexp.MustCompile(`{{data(\[\.\.\.|\[\?|\[)([0-9]+)\](}}\((.+?)\)|}})`)
patternCodeblock = regexp.MustCompile(`^([~*_]*)((\x60\x60\x60(.+?))|(\x60\x60\x60))([~*_]*)$`)
)
func (t *Translation) GetQuickResponse(cat, key string) (msg string) {
msg, err := t.GetResponse(cat, key)
if err != nil {
return ""
}
return msg
}
func (t *Translation) GetResponse(cat, key string) (msg string, err error) {
lb, err := ioutil.ReadFile("translation/"+t.Language+".json")
if err != nil {
return "", err
}
//fmt.Printf("\n\ncat: %v\nkey: %v", cat, key)
switch cat {
case "SubHelp":
tsh := CategorySubHelp{}
json.Unmarshal(lb, &tsh)
if reflect.ValueOf(tsh).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(tsh).Field(0).FieldByName(key).String()
}
case "Errors":
te := CategoryErrors{}
json.Unmarshal(lb, &te)
if reflect.ValueOf(te).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(te).Field(0).FieldByName(key).String()
}
case "Attachments":
tsh := CategoryAttachments{}
json.Unmarshal(lb, &tsh)
case "General":
tg := CategoryGeneral{}
json.Unmarshal(lb, &tg)
if reflect.ValueOf(tg).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(tg).Field(0).FieldByName(key).String()
}
case "Events":
te := CategoryEvents{}
json.Unmarshal(lb, &te)
if reflect.ValueOf(te).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(te).Field(0).FieldByName(key).String()
}
case "Filters":
tf := CategoryFilters{}
json.Unmarshal(lb, &tf)
if reflect.ValueOf(tf).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(tf).Field(0).FieldByName(key).String()
}
case "Commands_Discord":
tcd := CategoryCommandsDiscord{}
json.Unmarshal(lb, &tcd)
if reflect.ValueOf(tcd).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(tcd).Field(0).FieldByName(key).String()
}
case "Commands_Echo":
tce := CategoryCommandsEcho{}
json.Unmarshal(lb, &tce)
if reflect.ValueOf(tce).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(tce).Field(0).FieldByName(key).String()
}
case "Commands_Misc":
tcm := CategoryCommandsMisc{}
json.Unmarshal(lb, &tcm)
if reflect.ValueOf(tcm).Field(0).FieldByName(key).IsValid() {
if key == "EightBall" {
rand.Seed(time.Now().Unix())
msg = reflect.ValueOf(tcm).Field(0).FieldByName(key).Index(rand.Intn(reflect.ValueOf(tcm).Field(0).FieldByName(key).Len())).String()
} else {
msg = reflect.ValueOf(tcm).Field(0).FieldByName(key).String()
}
}
case "Profiles":
tp := CategoryProfiles{}
json.Unmarshal(lb, &tp)
if reflect.ValueOf(tp).Field(0).FieldByName(key).IsValid() {
msg = reflect.ValueOf(tp).Field(0).FieldByName(key).String()
}
/*case "Attachments":
tm.Category = Attachments{}
case "Attachments":
tm.Category = Attachments{}*/
}
//fmt.Printf("\nmsg: %v", msg)
if strings.Contains(msg, "{{prefix}}") && t.prefix != "" {
msg = strings.Replace(msg, "{{prefix}}", t.prefix, -1)
return msg, err
}
if msg != "" {
return msg, err
}
// This should NEVER!!!!! happen.
//
// This means an invalid request in the code was made.
// Something which can only be fixed with a restart.
return "Oh no... Something went very wrong...\nPlease DM <@167717172343209984> with this error + the message you send before this.", err
}
func (t *Translation) GetActionResponse(filter, action string) (msg string) {
lb, err := ioutil.ReadFile("translation/"+t.Language+".json")
if err != nil {
return ""
}
tfa := CategoryActions{}
json.Unmarshal(lb, &tfa)
switch filter {
case "AntiLink":
if reflect.ValueOf(tfa).Field(0).FieldByName("AntiLink_"+action).IsValid() {
msg = reflect.ValueOf(tfa).Field(0).FieldByName("AntiLink_"+action).String()
}
case "WordFilter":
if reflect.ValueOf(tfa).Field(0).FieldByName("WordFilter_"+action).IsValid() {
msg = reflect.ValueOf(tfa).Field(0).FieldByName("WordFilter_"+action).String()
}
case "NameFilter":
if reflect.ValueOf(tfa).Field(0).FieldByName("NameFilter_"+action).IsValid() {
msg = reflect.ValueOf(tfa).Field(0).FieldByName("NameFilter_"+action).String()
}
case "Kick":
if reflect.ValueOf(tfa).Field(0).FieldByName("Kick_"+action).IsValid() {
msg = reflect.ValueOf(tfa).Field(0).FieldByName("Kick_"+action).String()
}
case "Ban":
if reflect.ValueOf(tfa).Field(0).FieldByName("Ban_"+action).IsValid() {
msg = reflect.ValueOf(tfa).Field(0).FieldByName("Ban_"+action).String()
}
}
if strings.Contains(msg, "{{prefix}}") && t.prefix != "" {
msg = strings.Replace(msg, "{{prefix}}", t.prefix, -1)
return msg
}
if msg != "" {
return msg
}
// Should never happen
return "!!!ERROR!!! Please join https://discord.gg/YWBKbkD & mention JurrijnP/Proxy with this error."
}
func (t *Translation) SetResponseOptions(msg string, options []int) (fmsg string) {
var patternOption = regexp.MustCompile(`{{option\[([0-9]+?)\]}}\((((.|[\n])*?)\|((.|[\n])*?))\)`)
if !patternOption.MatchString(msg) {
return msg
} else if len(options) == 0 {
return msg
}
if len(patternOption.FindAllString(msg, -1)) < len(options) {
options = options[0:len(patternOption.FindAllString(msg, -1))]
}
fmsg = msg
for oi := range options {
// TESTING
// fmt.Println(strings.Split(patternOption.ReplaceAllString(patternOption.FindAllString(msg, -1)[oi], "$2"), "|"))
option := strings.Split(patternOption.ReplaceAllString(patternOption.FindAllString(msg, -1)[oi], "$2"), "|")
if len(option) < options[oi] || options[oi] < 0 {
fmsg = strings.Replace(fmsg, patternOption.FindAllString(msg, -1)[oi], option[0], 1)
} else {
fmsg = strings.Replace(fmsg, patternOption.FindAllString(msg, -1)[oi], option[options[oi]], 1)
}
}
return fmsg
}
func (tmd *TranslationMDData) setEmpty() {
tmd.Start = ""
tmd.End = ""
}
func (tmd *TranslationMDData) setMarkdown(dk string) {
tmd.Start = patternDataKey.ReplaceAllString(dk, "$4")
if patternCodeblock.MatchString(tmd.Start) && patternCodeblock.ReplaceAllString(tmd.Start, "$3") != "" {
md2 := patternCodeblock.ReplaceAllString(tmd.Start, "$1```$6")
for _,v := range md2 {
tmd.End = string(v)+tmd.End
}
} else {
for _,v := range tmd.Start {
tmd.End = string(v)+tmd.End
}
}
if strings.Contains(tmd.Start, "```") {
tmd.End = tmd.End[:strings.Index(tmd.End, "```")]+"\n"+tmd.End[strings.Index(tmd.End, "```"):]
mdc := patternCodeblock.ReplaceAllString(tmd.Start, "$2")
tmd.Start = tmd.Start[:strings.Index(tmd.Start, mdc)+len(mdc)]+"\n"+tmd.Start[strings.Index(tmd.Start, mdc)+len(mdc):]
}
}
// FillResponseData is used to fill data into a message.
// Any base type can be used for 'args'.
// Slices can be processed in 2 ways.
func (t *Translation) FillResponseData(msg string, args ...interface{}) (fmsg string) {
if args == nil {
return msg
}
if !strings.Contains(msg, "{{data[") {
return msg
}
fmsg = msg
//fmt.Println(fmsg)
var DataKey string = ""
Argloop:
for _, arg := range args {
if len(patternDataKey.FindAllString(fmsg, -1)) == 0 {
return
} else {
DataKey = patternDataKey.FindAllString(fmsg, -1)[0]
if patternDataKey.ReplaceAllString(DataKey, "$1") == "[?" {
if reflect.TypeOf(arg).Kind() == reflect.Slice {
fmsg = strings.Replace(fmsg, DataKey, patternDataKey.ReplaceAllString(DataKey, "{{data[...$2]$3"), -1)
DataKey = patternDataKey.ReplaceAllString(DataKey, "{{data[...$2]$3")
} else {
fmsg = strings.Replace(fmsg, DataKey, patternDataKey.ReplaceAllString(DataKey, "{{data[$2]$3"), -1)
DataKey = patternDataKey.ReplaceAllString(DataKey, "{{data[$2]$3")
}
}
}
if patternDataKey.ReplaceAllString(DataKey, "$3") != "}}" {
t.markdown.setMarkdown(DataKey)
} else {
t.markdown.setEmpty()
}
switch v := arg.(type) {
case bool:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatBool(v) +t.markdown.End, -1)
case uint8:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatUint(uint64(v), 10) +t.markdown.End, -1)
case uint16:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatUint(uint64(v), 10) +t.markdown.End, -1)
case uint32:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatUint(uint64(v), 10) +t.markdown.End, -1)
case uint64:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatUint(v, 10) +t.markdown.End, -1)
case int8:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatInt(int64(v), 10) +t.markdown.End, -1)
case int16:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatInt(int64(v), 10) +t.markdown.End, -1)
case int32:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatInt(int64(v), 10) +t.markdown.End, -1)
case int64:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatInt(v, 10) +t.markdown.End, -1)
case int:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatInt(int64(v), 10) +t.markdown.End, -1)
case uint:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatUint(uint64(v), 10) +t.markdown.End, -1)
case float32:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatFloat(float64(v), 'f', -1, 32) +t.markdown.End, -1)
case float64:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatFloat(v, 'f', -1, 64) +t.markdown.End, -1)
case complex128:
var fr float64 = real(arg.(complex128))
var fi float64 = imag(arg.(complex128))
var sc string = strconv.FormatFloat(fr, 'f', -1, 64) + "+" + strconv.FormatFloat(fi, 'f', -1, 64) + "i"
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ sc +t.markdown.End, -1)
case complex64:
var fr float32 = real(arg.(complex64))
var fi float32 = imag(arg.(complex64))
var sc string = strconv.FormatFloat(float64(fr), 'f', -1, 32) + "+" + strconv.FormatFloat(float64(fi), 'f', -1, 32) + "i"
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ sc +t.markdown.End, -1)
case string:
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ v +t.markdown.End, -1)
case []string:
if patternDataKey.ReplaceAllString(DataKey, "$1") == "[..." {
if t.markdown.Start != "" {
for vi := range v {
v[vi] = t.markdown.Start+ v[vi] +t.markdown.End
}
}
fmsg = strings.Replace(fmsg, DataKey, strings.Join(v, ", "), -1)
break Argloop
} else {
for _, av := range arg.([]string) {
if len(patternDataKey.FindAllString(fmsg, -1)) == 0 {
return
} else {
DataKey = patternDataKey.FindAllString(fmsg, -1)[0]
if patternDataKey.ReplaceAllString(DataKey, "$3") != "}}" {
t.markdown.setMarkdown(DataKey)
} else {
t.markdown.setEmpty()
}
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ av +t.markdown.End, -1)
}
}
}
case []int:
if strings.Contains(fmsg, "{{data[...") {
var sjav []string
for _, av := range arg.([]int) {
sjav = append(sjav, t.markdown.Start + strconv.FormatInt(int64(av), 10) + t.markdown.Start)
}
fmsg = strings.Replace(fmsg, DataKey, strings.Join(sjav, ", "), -1)
break Argloop
} else {
for _, av := range arg.([]int) {
if len(patternDataKey.FindAllString(fmsg, -1)) == 0 {
return
} else {
DataKey = patternDataKey.FindAllString(fmsg, -1)[0]
if patternDataKey.ReplaceAllString(DataKey, "$3") != "}}" {
t.markdown.setMarkdown(DataKey)
} else {
t.markdown.setEmpty()
}
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatInt(int64(av), 10) +t.markdown.End, -1)
}
}
}
case []float64:
if strings.Contains(fmsg, "{{data[...") {
var sjav []string
for _, av := range arg.([]float64) {
sjav = append(sjav, t.markdown.Start + strconv.FormatFloat(av, 'f', -1, 64) + t.markdown.End)
}
fmsg = strings.Replace(fmsg, DataKey, strings.Join(sjav, ", "), -1)
break Argloop
} else {
for _, av := range arg.([]float64) {
if len(patternDataKey.FindAllString(fmsg, -1)) == 0 {
return
} else {
DataKey = patternDataKey.FindAllString(fmsg, -1)[0]
if patternDataKey.ReplaceAllString(DataKey, "$3") != "}}" {
t.markdown.setMarkdown(DataKey)
} else {
t.markdown.setEmpty()
}
fmsg = strings.Replace(fmsg, DataKey, t.markdown.Start+ strconv.FormatFloat(av, 'f', -1, 64) +t.markdown.End, -1)
}
}
}
}
}
return fmsg
}