forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
368 lines (301 loc) · 8.76 KB
/
struct.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
package disgord
import (
"bytes"
"context"
"errors"
"github.com/andersfylling/disgord/json"
"strconv"
"time"
)
// common functionality/types used by struct_*.go files goes here
//go:generate go run generate/interfaces/main.go
//go:generate go run generate/sorters/main.go
//go:generate go run generate/json/main.go
// Copier holds the CopyOverTo method which copies all it's content from one
// struct to another. Note that this requires a deep copy.
// useful when overwriting already existing content in the cacheLink to reduce GC.
type Copier interface {
CopyOverTo(other interface{}) error
}
// DeepCopier holds the DeepCopy method which creates and returns a deep copy of
// any struct.
type DeepCopier interface {
DeepCopy() interface{}
}
func newErrorUnsupportedType(message string) *ErrorUnsupportedType {
return &ErrorUnsupportedType{
info: message,
}
}
// ErrorUnsupportedType used when the given param type is not supported
type ErrorUnsupportedType struct {
info string
}
func (e *ErrorUnsupportedType) Error() string {
return e.info
}
// DiscordDeleter holds the DeleteFromDiscord method which deletes a given
// object from the Discord servers.
type discordDeleter interface {
deleteFromDiscord(ctx context.Context, session Session, flags ...Flag) error
}
// hasher creates a hash for comparing objects. This excludes the identifier and object type as those are expected
// to be the same during a comparison.
type hasher interface {
hash() string
}
type guilder interface {
getGuildIDs() []Snowflake
}
// Mentioner can be implemented by any type that is mentionable.
// https://discord.com/developers/docs/reference#message-formatting-formats
type Mentioner interface {
Mention() string
}
// zeroInitialiser zero initializes a struct by setting all the values to the default initialization values.
// Used in the flyweight pattern.
type zeroInitialiser interface {
zeroInitialize()
}
// Reseter Reset() zero initialises or empties a struct instance
type Reseter interface {
Reset()
}
// internalUpdater is called whenever a socket event or a REST response is created.
type internalUpdater interface {
updateInternals()
}
type internalClientUpdater interface {
updateInternalsWithClient(*Client)
}
// Discord types
// helperTypes: timestamp, levels, etc.
// discordTimeFormat to be able to correctly convert timestamps back into json,
// we need the micro timestamp with an addition at the ending.
// time.RFC3331 does not yield an output similar to the discord timestamp input, the date is however correct.
const timestampFormat = "2006-01-02T15:04:05.000000+00:00"
// Time handles Discord timestamps
type Time struct {
time.Time
}
var _ json.Marshaler = (*Time)(nil)
var _ json.Unmarshaler = (*Time)(nil)
// MarshalJSON implements json.Marshaler.
// error: https://stackoverflow.com/questions/28464711/go-strange-json-hyphen-unmarshall-error
func (t Time) MarshalJSON() ([]byte, error) {
var ts string
if !t.IsZero() {
ts = t.String()
}
// wrap in double quotes for valid json parsing
return []byte(`"` + ts + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (t *Time) UnmarshalJSON(data []byte) error {
var ts time.Time
// Don't try to unmarshal empty strings.
if bytes.Equal([]byte("\"\""), data) {
return nil
}
if err := json.Unmarshal(data, &ts); err != nil {
return err
}
t.Time = ts
return nil
}
// String returns the timestamp as a Discord formatted timestamp. Formatting
// with time.RFC3331 does not suffice.
func (t Time) String() string {
return t.Format(timestampFormat)
}
// -----------
// levels
// ExplicitContentFilterLvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-explicit-content-filter-level
type ExplicitContentFilterLvl uint
// Explicit content filter levels
const (
ExplicitContentFilterLvlDisabled ExplicitContentFilterLvl = iota
ExplicitContentFilterLvlMembersWithoutRoles
ExplicitContentFilterLvlAllMembers
)
// Disabled if the content filter is disabled
func (ecfl *ExplicitContentFilterLvl) Disabled() bool {
return *ecfl == ExplicitContentFilterLvlDisabled
}
// MembersWithoutRoles if the filter only applies for members without a role
func (ecfl *ExplicitContentFilterLvl) MembersWithoutRoles() bool {
return *ecfl == ExplicitContentFilterLvlMembersWithoutRoles
}
// AllMembers if the filter applies for all members regardles of them having a role or not
func (ecfl *ExplicitContentFilterLvl) AllMembers() bool {
return *ecfl == ExplicitContentFilterLvlAllMembers
}
// MFALvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-mfa-level
type MFALvl uint
// Different MFA levels
const (
MFALvlNone MFALvl = iota
MFALvlElevated
)
// None ...
func (mfal *MFALvl) None() bool {
return *mfal == MFALvlNone
}
// Elevated ...
func (mfal *MFALvl) Elevated() bool {
return *mfal == MFALvlElevated
}
// VerificationLvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-verification-level
type VerificationLvl uint
// the different verification levels
const (
VerificationLvlNone VerificationLvl = iota
VerificationLvlLow
VerificationLvlMedium
VerificationLvlHigh
VerificationLvlVeryHigh
)
// None unrestricted
func (vl *VerificationLvl) None() bool {
return *vl == VerificationLvlNone
}
// Low must have verified email on account
func (vl *VerificationLvl) Low() bool {
return *vl == VerificationLvlLow
}
// Medium must be registered on Discord for longer than 5 minutes
func (vl *VerificationLvl) Medium() bool {
return *vl == VerificationLvlMedium
}
// High (╯°□°)╯︵ ┻━┻ - must be a member of the server for longer than 10 minutes
func (vl *VerificationLvl) High() bool {
return *vl == VerificationLvlHigh
}
// VeryHigh ┻━┻ミヽ(ಠ益ಠ)ノ彡┻━┻ - must have a verified phone number
func (vl *VerificationLvl) VeryHigh() bool {
return *vl == VerificationLvlVeryHigh
}
// DefaultMessageNotificationLvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-default-message-notification-level
type DefaultMessageNotificationLvl uint
// different notification levels on new messages
const (
DefaultMessageNotificationLvlAllMessages DefaultMessageNotificationLvl = iota
DefaultMessageNotificationLvlOnlyMentions
)
// AllMessages ...
func (dmnl *DefaultMessageNotificationLvl) AllMessages() bool {
return *dmnl == DefaultMessageNotificationLvlAllMessages
}
// OnlyMentions ...
func (dmnl *DefaultMessageNotificationLvl) OnlyMentions() bool {
return *dmnl == DefaultMessageNotificationLvlOnlyMentions
}
// NewDiscriminator Discord user discriminator hashtag
func NewDiscriminator(d string) (discriminator Discriminator, err error) {
var tmp uint64
tmp, err = strconv.ParseUint(d, 10, 16)
if err == nil {
discriminator = Discriminator(tmp)
}
return
}
// Discriminator value
type Discriminator uint16
var _ json.Unmarshaler = (*Discriminator)(nil)
var _ json.Marshaler = (*Discriminator)(nil)
func (d Discriminator) String() (str string) {
if d == 0 {
str = ""
return
}
if d == 1 {
str = "0001"
return
}
str = strconv.Itoa(int(d))
if d < 1000 {
shift := 4 - len(str)
for i := 0; i < shift; i++ {
str = "0" + str
}
}
return
}
// NotSet checks if the discriminator is not set
func (d Discriminator) NotSet() bool {
return d == 0
}
// UnmarshalJSON see interface json.Unmarshaler
func (d *Discriminator) UnmarshalJSON(data []byte) error {
*d = 0
length := len(data) - 1
for i := 1; i < length; i++ {
*d = *d*10 + Discriminator(data[i]-'0')
}
return nil
}
// MarshalJSON see interface json.Marshaler
func (d Discriminator) MarshalJSON() (data []byte, err error) {
return []byte("\"" + d.String() + "\""), nil
}
// extractAttribute extracts the snowflake value from a JSON string given a attribute filter. For extracting the root ID of an JSON byte array,
// set filter to `"id":"` and scope to `0`. Note that the filter holds the last character before the value starts.
func extractAttribute(filter []byte, scope int, data []byte) (id Snowflake, err error) {
//filter := []byte(`"id":"`)
filterLen := len(filter) - 1
//scope := 0
var start uint
lastPos := len(data) - 1
for i := 1; i <= lastPos-filterLen; i++ {
if data[i] == '{' {
scope++
} else if data[i] == '}' {
scope--
}
if scope != 0 {
continue
}
for j := filterLen; j >= 0; j-- {
if filter[j] != data[i+j] {
break
}
if j == 0 {
start = uint(i + len(filter))
}
}
if start != 0 {
break
}
}
if start == 0 {
err = errors.New("unable to locate ID")
return
}
i := start
//E:
for {
if data[i] >= '0' && data[i] <= '9' {
i++
} else {
break
}
//
//switch data[i] {
//case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// i++
//default:
// break E
//}
}
if i > start {
id = Snowflake(0)
err = id.UnmarshalJSON(data[start-1 : i+1])
} else {
err = errors.New("id was empty")
}
return
}