forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
417 lines (344 loc) · 10.6 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package disgord
import (
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/andersfylling/snowflake/v3"
"github.com/andersfylling/disgord/constant"
)
// common functionality/types used by struct_*.go files goes here
// 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
}
// cacheCopier is similar to Copier interface. Except that it only copies over fields which has a value, unlike Copier
// that creates an exact copy of everything. This will also ignore arrays that can be simplified to a snowflake array.
// An example of said simplification is Guild.Channels, as there will already exist a channel cacheLink.
//
// It is important to know that this should only be called by the cacheLink. The cacheLink must also make sure that the type
// given as an argument for `other` is correct. Failure to do so results in a panic.
type cacheCopier interface {
copyOverToCache(other interface{}) error
}
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
}
// DiscordUpdater holds the Update method for updating any given Discord struct
// (fetch the latest content). If you only want to keep up to date with the
// cacheLink use the UpdateFromCache method.
// TODO: change param type for UpdateFromCache once caching is implemented
//type DiscordUpdater interface {
// Update(session Session)
// UpdateFromCache(session Session)
//}
// DiscordSaver holds the SaveToDiscord method for sending changes to the
// Discord API over REST.
// If you change any of the values and want to notify Discord about your change,
// use the Save method to send a REST request (assuming that the struct values
// can be updated).
//
// NOTE! if the struct has an snowflake/ID, it will update content. But if the
// snowflake is missing/not set, it will create content (if possible,
// otherwise you will get an error)
type discordSaver interface {
saveToDiscord(session Session, changes discordSaver) error
}
// DiscordDeleter holds the DeleteFromDiscord method which deletes a given
// object from the Discord servers.
type discordDeleter interface {
deleteFromDiscord(session Session) error
}
// DeepCopier holds the DeepCopy method which creates and returns a deep copy of
// any struct.
type DeepCopier interface {
DeepCopy() interface{}
}
// 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 {
getGuildID() snowflake.ID
}
// 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"
// Timestamp handles Discord timestamps
type Timestamp time.Time
// MarshalJSON see json.Marshaler
// error: https://stackoverflow.com/questions/28464711/go-strange-json-hyphen-unmarshall-error
func (t Timestamp) MarshalJSON() ([]byte, error) {
// wrap in double qoutes for valid json parsing
jsonReady := fmt.Sprintf("\"%s\"", t.String())
return []byte(jsonReady), nil
}
// UnmarshalJSON see json.Unmarshaler
func (t *Timestamp) UnmarshalJSON(data []byte) error {
var ts time.Time
err := unmarshal(data, &ts)
if err != nil {
return err
}
*t = Timestamp(ts)
return nil
}
// String converts the timestamp into a discord formatted timestamp. time.RFC3331 does not suffice
func (t Timestamp) String() string {
return t.Time().Format(timestampFormat)
}
// Time converts the DiscordTimestamp into a time.Time type.......
func (t Timestamp) Time() time.Time {
return time.Time(t)
}
// Empty check if the timestamp holds no value / not set
func (t Timestamp) Empty() bool {
return time.Time(t).UnixNano() == 0
}
// -----------
// levels
// ExplicitContentFilterLvl ...
// https://discordapp.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://discordapp.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://discordapp.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://discordapp.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
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) (err error) {
*d = 0
length := len(data) - 1
for i := 1; i < length; i++ {
*d = *d*10 + Discriminator(data[i]-'0')
}
return
}
// MarshalJSON see interface json.Marshaler
func (d Discriminator) MarshalJSON() (data []byte, err error) {
return []byte("\"" + d.String() + "\""), nil
}
// Gateway is for parsing the Gateway endpoint response
type Gateway struct {
URL string `json:"url"`
}
// GatewayBot is for parsing the Gateway Bot endpoint response
type GatewayBot struct {
Gateway
Shards uint `json:"shards"`
SessionStartLimit struct {
Total uint `json:"total"`
Remaining uint `json:"remaining"`
ResetAfter uint `json:"reset_after"`
} `json:"session_start_limit"`
}
// 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
}
func handleRWLocking(read, write *sync.RWMutex) {
if constant.LockedMethods {
read.RLock()
write.Lock()
}
}
func handleRWUnlocking(read, write *sync.RWMutex) {
if constant.LockedMethods {
read.RUnlock()
write.Unlock()
}
}