forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
183 lines (148 loc) · 4.95 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
package disgord
import (
"encoding/json"
"fmt"
"time"
)
// 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 cache to reduce GC.
type Copier interface {
CopyOverTo(other interface{}) error
}
func NewErrorUnsupportedType(message string) *ErrorUnsupportedType {
return &ErrorUnsupportedType{
info: message,
}
}
type ErrorUnsupportedType struct {
info string
}
func (eut *ErrorUnsupportedType) Error() string {
return eut.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
// cache 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) 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{}
}
// 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"
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
type Unmarshaler interface {
UnmarshalJSON(data []byte) error
}
type Timestamp time.Time
// 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
}
func (t *Timestamp) UnmarshalJSON(data []byte) error {
var ts time.Time
err := json.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)
}
// -----------
// levels
// ExplicitContentFilterLvl ...
// https://discordapp.com/developers/docs/resources/guild#guild-object-explicit-content-filter-level
type ExplicitContentFilterLvl uint
func (ecfl *ExplicitContentFilterLvl) Disabled() bool {
return *ecfl == 0
}
func (ecfl *ExplicitContentFilterLvl) MembersWithoutRoles() bool {
return *ecfl == 1
}
func (ecfl *ExplicitContentFilterLvl) AllMembers() bool {
return *ecfl == 2
}
// MFA ...
// https://discordapp.com/developers/docs/resources/guild#guild-object-mfa-level
type MFALvl uint
func (mfal *MFALvl) None() bool {
return *mfal == 0
}
func (mfal *MFALvl) Elevated() bool {
return *mfal == 1
}
// Verification ...
// https://discordapp.com/developers/docs/resources/guild#guild-object-verification-level
type VerificationLvl uint
// None unrestricted
func (vl *VerificationLvl) None() bool {
return *vl == 0
}
// Low must have verified email on account
func (vl *VerificationLvl) Low() bool {
return *vl == 1
}
// Medium must be registered on Discord for longer than 5 minutes
func (vl *VerificationLvl) Medium() bool {
return *vl == 2
}
// High (╯°□°)╯︵ ┻━┻ - must be a member of the server for longer than 10 minutes
func (vl *VerificationLvl) High() bool {
return *vl == 3
}
// VeryHigh ┻━┻ミヽ(ಠ益ಠ)ノ彡┻━┻ - must have a verified phone number
func (vl *VerificationLvl) VeryHigh() bool {
return *vl == 4
}
// DefaultMessageNotification ...
// https://discordapp.com/developers/docs/resources/guild#guild-object-default-message-notification-level
type DefaultMessageNotificationLvl uint
func (dmnl *DefaultMessageNotificationLvl) AllMessages() bool {
return *dmnl == 0
}
func (dmnl *DefaultMessageNotificationLvl) OnlyMentions() bool {
return *dmnl == 1
}
func (dmnl *DefaultMessageNotificationLvl) Equals(v uint) bool {
return uint(*dmnl) == v
}