forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invite.go
246 lines (198 loc) · 5.95 KB
/
invite.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
package disgord
import (
"net/http"
"github.com/andersfylling/disgord/constant"
"github.com/andersfylling/disgord/endpoint"
"github.com/andersfylling/disgord/httd"
"github.com/andersfylling/disgord/ratelimit"
)
// PartialInvite ...
// {
// "code": "abc"
// }
type PartialInvite = Invite
// Invite Represents a code that when used, adds a user to a guild.
// https://discordapp.com/developers/docs/resources/invite#invite-object
// Reviewed: 2018-06-10
type Invite struct {
Lockable `json:"-"`
// Code the invite code (unique Snowflake)
Code string `json:"code"`
// Guild the guild this invite is for
Guild *PartialGuild `json:"guild"`
// Channel the channel this invite is for
Channel *PartialChannel `json:"channel"`
// ApproximatePresenceCount approximate count of online members
ApproximatePresenceCount int `json:"approximate_presence_count,omitempty"`
// ApproximatePresenceCount approximate count of total members
ApproximateMemberCount int `json:"approximate_member_count,omitempty"`
}
// DeepCopy see interface at struct.go#DeepCopier
func (i *Invite) DeepCopy() (copy interface{}) {
copy = &Invite{}
i.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (i *Invite) CopyOverTo(other interface{}) (err error) {
var ok bool
var invite *Invite
if invite, ok = other.(*Invite); !ok {
err = newErrorUnsupportedType("given interface{} was not of type *Invite")
return
}
if constant.LockedMethods {
i.RLock()
invite.Lock()
}
invite.Code = i.Code
invite.ApproximatePresenceCount = i.ApproximatePresenceCount
invite.ApproximateMemberCount = i.ApproximateMemberCount
if i.Guild != nil {
invite.Guild = NewPartialGuild(i.Guild.ID)
}
if i.Channel != nil {
c := i.Channel
invite.Channel = &PartialChannel{
ID: c.ID,
Name: c.Name,
Type: c.Type,
}
}
if constant.LockedMethods {
i.RUnlock()
invite.Unlock()
}
return
}
// InviteMetadata Object
// https://discordapp.com/developers/docs/resources/invite#invite-metadata-object
// Reviewed: 2018-06-10
type InviteMetadata struct {
Lockable `json:"-"`
// Inviter user who created the invite
Inviter *User `json:"inviter"`
// Uses number of times this invite has been used
Uses int `json:"uses"`
// MaxUses max number of times this invite can be used
MaxUses int `json:"max_uses"`
// MaxAge duration (in seconds) after which the invite expires
MaxAge int `json:"max_age"`
// Temporary whether this invite only grants temporary membership
Temporary bool `json:"temporary"`
// CreatedAt when this invite was created
CreatedAt Timestamp `json:"created_at"`
// Revoked whether this invite is revoked
Revoked bool `json:"revoked"`
}
// DeepCopy see interface at struct.go#DeepCopier
func (i *InviteMetadata) DeepCopy() (copy interface{}) {
copy = &InviteMetadata{}
i.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (i *InviteMetadata) CopyOverTo(other interface{}) (err error) {
var ok bool
var invite *InviteMetadata
if invite, ok = other.(*InviteMetadata); !ok {
err = newErrorUnsupportedType("given interface{} was not of type *InviteMetadata")
return
}
if constant.LockedMethods {
i.RLock()
invite.Lock()
}
invite.Uses = i.Uses
invite.MaxUses = i.MaxUses
invite.MaxAge = i.MaxAge
invite.Temporary = i.Temporary
invite.CreatedAt = i.CreatedAt
invite.Revoked = i.Revoked
if i.Inviter != nil {
invite.Inviter = i.Inviter.DeepCopy().(*User)
}
if constant.LockedMethods {
i.RUnlock()
invite.Unlock()
}
return
}
// voiceRegionsFactory temporary until flyweight is implemented
func inviteFactory() interface{} {
return &Invite{}
}
// GetInvite [REST] Returns an invite object for the given code.
// Method GET
// Endpoint /invites/{invite.code}
// Rate limiter /invites
// Discord documentation https://discordapp.com/developers/docs/resources/invite#get-invite
// Reviewed 2018-06-10
// Comment withCounts whether the invite should contain approximate member counts
func (c *Client) GetInvite(inviteCode string) (builder *getInviteBuilder) {
builder = &getInviteBuilder{}
builder.r.itemFactory = inviteFactory
builder.r.IgnoreCache().setup(nil, c.req, &httd.Request{
Method: http.MethodGet,
Ratelimiter: ratelimit.Invites(),
Endpoint: endpoint.Invite(inviteCode),
}, nil)
return builder
}
type getInviteBuilder struct {
r RESTRequestBuilder
}
func (b *getInviteBuilder) AsInviteCode() *getInviteBuilder {
b.r.queryParam("with_counts", true)
return b
}
//func (b *getInviteBuilder) IgnoreCache() *getInviteBuilder {
// b.r.IgnoreCache()
// return b
//}
func (b *getInviteBuilder) CancelOnRatelimit() *getInviteBuilder {
b.r.CancelOnRatelimit()
return b
}
func (b *getInviteBuilder) Execute() (invite *Invite, err error) {
var v interface{}
v, err = b.r.execute()
if err != nil {
return
}
invite = v.(*Invite)
return
}
// DeleteInvite [REST] Delete an invite. Requires the MANAGE_CHANNELS permission. Returns an invite object on success.
// Method DELETE
// Endpoint /invites/{invite.code}
// Rate limiter /invites
// Discord documentation https://discordapp.com/developers/docs/resources/invite#delete-invite
// Reviewed 2018-06-10
// Comment -
func (c *Client) DeleteInvite(inviteCode string) (builder *deleteInviteBuilder) {
builder = &deleteInviteBuilder{}
builder.r.itemFactory = inviteFactory
builder.r.IgnoreCache().setup(nil, c.req, &httd.Request{
Method: http.MethodDelete,
Ratelimiter: ratelimit.Invites(),
Endpoint: endpoint.Invite(inviteCode),
}, nil)
return builder
}
type deleteInviteBuilder struct {
r RESTRequestBuilder
}
func (b *deleteInviteBuilder) CancelOnRatelimit() *deleteInviteBuilder {
b.r.CancelOnRatelimit()
return b
}
func (b *deleteInviteBuilder) Execute() (invite *Invite, err error) {
var v interface{}
v, err = b.r.execute()
if err != nil {
return
}
invite = v.(*Invite)
return
}