forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauditlog.go
354 lines (307 loc) · 13.7 KB
/
auditlog.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
package disgord
import (
"context"
"github.com/andersfylling/disgord/internal/constant"
"github.com/andersfylling/disgord/internal/endpoint"
"github.com/andersfylling/disgord/internal/httd"
)
type AuditLogEvt uint
// Audit-log event types
const (
AuditLogEvtGuildUpdate AuditLogEvt = 1
)
const (
AuditLogEvtChannelCreate AuditLogEvt = 10 + iota
AuditLogEvtChannelUpdate
AuditLogEvtChannelDelete
AuditLogEvtOverwriteCreate
AuditLogEvtOverwriteUpdate
AuditLogEvtOverwriteDelete
)
const (
AuditLogEvtMemberKick AuditLogEvt = 20 + iota
AuditLogEvtMemberPrune
AuditLogEvtMemberBanAdd
AuditLogEvtMemberBanRemove
AuditLogEvtMemberUpdate
AuditLogEvtMemberRoleUpdate
)
const (
AuditLogEvtRoleCreate AuditLogEvt = 30 + iota
AuditLogEvtRoleUpdate
AuditLogEvtRoleDelete
)
const (
AuditLogEvtInviteCreate AuditLogEvt = 40
AuditLogEvtInviteUpdate
AuditLogEvtInviteDelete
)
const (
AuditLogEvtWebhookCreate AuditLogEvt = 50 + iota
AuditLogEvtWebhookUpdate
AuditLogEvtWebhookDelete
)
const (
AuditLogEvtEmojiCreate AuditLogEvt = 60 + iota
AuditLogEvtEmojiUpdate
AuditLogEvtEmojiDelete
)
const (
AuditLogEvtMessageDelete AuditLogEvt = 72
)
type AuditLogChange string
// all the different keys for an audit log change
const (
// key name, identifier changed, type, description
AuditLogChangeName AuditLogChange = "name" // guild string name changed
AuditLogChangeIconHash AuditLogChange = "icon_hash" // guild string icon changed
AuditLogChangeSplashHash AuditLogChange = "splash_hash" // guild string invite splash page artwork changed
AuditLogChangeOwnerID AuditLogChange = "owner_id" // guild snowflake owner changed
AuditLogChangeRegion AuditLogChange = "region" // guild string region changed
AuditLogChangeAFKChannelID AuditLogChange = "afk_channel_id" // guild snowflake afk channel changed
AuditLogChangeAFKTimeout AuditLogChange = "afk_timeout" // guild integer afk timeout duration changed
AuditLogChangeMFALevel AuditLogChange = "mfa_level" // guild integer two-factor auth requirement changed
AuditLogChangeVerificationLevel AuditLogChange = "verification_level" // guild integer required verification level changed
AuditLogChangeExplicitContentFilter AuditLogChange = "explicit_content_filter" // guild integer change in whose messages are scanned and deleted for explicit content in the server
AuditLogChangeDefaultMessageNotifications AuditLogChange = "default_message_notifications" // guild integer default message notification level changed
AuditLogChangeVanityURLCode AuditLogChange = "vanity_url_code" // guild string guild invite vanity url changed
AuditLogChangeAdd AuditLogChange = "$add" // add guild array of role objects new role added
AuditLogChangeRemove AuditLogChange = "$remove" // remove guild array of role objects role removed
AuditLogChangePruneDeleteDays AuditLogChange = "prune_delete_days" // guild integer change in number of days after which inactive and role-unassigned members are kicked
AuditLogChangeWidgetEnabled AuditLogChange = "widget_enabled" // guild bool server widget enabled/disable
AuditLogChangeWidgetChannelID AuditLogChange = "widget_channel_id" // guild snowflake channel id of the server widget changed
AuditLogChangePosition AuditLogChange = "position" // channel integer text or voice channel position changed
AuditLogChangeTopic AuditLogChange = "topic" // channel string text channel topic changed
AuditLogChangeBitrate AuditLogChange = "bitrate" // channel integer voice channel bitrate changed
AuditLogChangePermissionOverwrites AuditLogChange = "permission_overwrites" // channel array of channel overwrite objects permissions on a channel changed
AuditLogChangeNSFW AuditLogChange = "nsfw" // channel bool channel nsfw restriction changed
AuditLogChangeApplicationID AuditLogChange = "application_id" // channel snowflake application id of the added or removed webhook or bot
AuditLogChangePermissions AuditLogChange = "permissions" // role integer permissions for a role changed
AuditLogChangeColor AuditLogChange = "color" // role integer role color changed
AuditLogChangeHoist AuditLogChange = "hoist" // role bool role is now displayed/no longer displayed separate from online users
AuditLogChangeMentionable AuditLogChange = "mentionable" // role bool role is now mentionable/unmentionable
AuditLogChangeAllow AuditLogChange = "allow" // role integer a permission on a text or voice channel was allowed for a role
AuditLogChangeDeny AuditLogChange = "deny" // role integer a permission on a text or voice channel was denied for a role
AuditLogChangeCode AuditLogChange = "code" // invite string invite code changed
AuditLogChangeChannelID AuditLogChange = "channel_id" // invite snowflake channel for invite code changed
AuditLogChangeInviterID AuditLogChange = "inviter_id" // invite snowflake person who created invite code changed
AuditLogChangeMaxUses AuditLogChange = "max_uses" // invite integer change to max number of times invite code can be used
AuditLogChangeUses AuditLogChange = "uses" // invite integer number of times invite code used changed
AuditLogChangeMaxAge AuditLogChange = "max_age" // invite integer how long invite code lasts changed
AuditLogChangeTemporary AuditLogChange = "temporary" // invite bool invite code is temporary/never expires
AuditLogChangeDeaf AuditLogChange = "deaf" // user bool user server deafened/undeafened
AuditLogChangeMute AuditLogChange = "mute" // user bool user server muted/unmuteds
AuditLogChangeNick AuditLogChange = "nick" // user string user nickname changed
AuditLogChangeAvatarHash AuditLogChange = "avatar_hash" // user string user avatar changed
AuditLogChangeID AuditLogChange = "id" // any snowflake the id of the changed entity - sometimes used in conjunction with other keys
AuditLogChangeType AuditLogChange = "type" // any integer (channel type) or string type of entity created
)
// AuditLog ...
type AuditLog struct {
Lockable `json:"-"`
Webhooks []*Webhook `json:"webhooks"`
Users []*User `json:"users"`
AuditLogEntries []*AuditLogEntry `json:"audit_log_entries"`
}
func (l *AuditLog) Bans() (bans []*PartialBan) {
for _, e := range l.AuditLogEntries {
if e.Event == AuditLogEvtMemberBanAdd {
bans = append(bans, &PartialBan{
Reason: e.Reason,
ModeratorResponsibleID: e.UserID,
BannedUserID: e.TargetID,
})
}
}
return bans
}
// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLog) DeepCopy() (copy interface{}) {
copy = &AuditLog{}
l.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (l *AuditLog) CopyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLog
if log, ok = other.(*AuditLog); !ok {
err = newErrorUnsupportedType("given interface{} was not of type *AuditLog")
return
}
if constant.LockedMethods {
l.RLock()
log.Lock()
}
for _, webhook := range l.Webhooks {
log.Webhooks = append(log.Webhooks, webhook.DeepCopy().(*Webhook))
}
for _, user := range l.Users {
log.Users = append(log.Users, user.DeepCopy().(*User))
}
for _, entry := range l.AuditLogEntries {
log.AuditLogEntries = append(log.AuditLogEntries, entry.DeepCopy().(*AuditLogEntry))
}
if constant.LockedMethods {
l.RUnlock()
log.Unlock()
}
return
}
// AuditLogEntry ...
type AuditLogEntry struct {
Lockable `json:"-"`
TargetID Snowflake `json:"target_id"`
Changes []*AuditLogChanges `json:"changes,omitempty"`
UserID Snowflake `json:"user_id"`
ID Snowflake `json:"id"`
Event AuditLogEvt `json:"action_type"`
Options *AuditLogOption `json:"options,omitempty"`
Reason string `json:"reason,omitempty"`
}
// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLogEntry) DeepCopy() (copy interface{}) {
copy = &AuditLogEntry{}
l.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (l *AuditLogEntry) CopyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLogEntry
if log, ok = other.(*AuditLogEntry); !ok {
err = newErrorUnsupportedType("given interface{} was not of type *AuditLogEntry")
return
}
if constant.LockedMethods {
l.RLock()
log.Lock()
}
log.TargetID = l.TargetID
log.UserID = l.UserID
log.ID = l.ID
log.Event = l.Event
log.Reason = l.Reason
for _, change := range l.Changes {
log.Changes = append(log.Changes, change.DeepCopy().(*AuditLogChanges))
}
if l.Options != nil {
log.Options = l.Options.DeepCopy().(*AuditLogOption)
}
if constant.LockedMethods {
l.RUnlock()
log.Unlock()
}
return
}
// AuditLogOption ...
type AuditLogOption struct {
Lockable `json:"-"`
DeleteMemberDays string `json:"delete_member_days"`
MembersRemoved string `json:"members_removed"`
ChannelID Snowflake `json:"channel_id"`
Count string `json:"count"`
ID Snowflake `json:"id"`
Type string `json:"type"` // type of overwritten entity ("member" or "role")
RoleName string `json:"role_name"`
}
// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLogOption) DeepCopy() (copy interface{}) {
copy = &AuditLogOption{}
l.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (l *AuditLogOption) CopyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLogOption
if log, ok = other.(*AuditLogOption); !ok {
err = newErrorUnsupportedType("given interface{} was not of type *AuditLogOption")
return
}
if constant.LockedMethods {
l.RLock()
log.Lock()
}
log.DeleteMemberDays = l.DeleteMemberDays
log.MembersRemoved = l.MembersRemoved
log.ChannelID = l.ChannelID
log.Count = l.Count
log.ID = l.ID
log.Type = l.Type
log.RoleName = l.RoleName
if constant.LockedMethods {
l.RUnlock()
log.Unlock()
}
return
}
// AuditLogChanges ...
type AuditLogChanges struct {
Lockable `json:"-"`
NewValue interface{} `json:"new_value,omitempty"`
OldValue interface{} `json:"old_value,omitempty"`
Key string `json:"key"`
}
// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLogChanges) DeepCopy() (copy interface{}) {
copy = &AuditLogChanges{}
l.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (l *AuditLogChanges) CopyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLogChanges
if log, ok = other.(*AuditLogChanges); !ok {
err = newErrorUnsupportedType("given interface{} was not of type *AuditLogChanges")
return
}
if constant.LockedMethods {
l.RLock()
log.Lock()
}
log.NewValue = l.NewValue
log.OldValue = l.OldValue
log.Key = l.Key
if constant.LockedMethods {
l.RUnlock()
log.Unlock()
}
return
}
// auditLogFactory temporary until flyweight is implemented
func auditLogFactory() interface{} {
return &AuditLog{}
}
// GetGuildAuditLogs [REST] Returns an audit log object for the guild. Requires the 'VIEW_AUDIT_LOG' permission.
// Note that this request will _always_ send a REST request, regardless of you calling IgnoreCache or not.
// Method GET
// Endpoint /guilds/{guild.id}/audit-logs
// Discord documentation https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log
// Reviewed 2018-06-05
// Comment -
// Note Check the last entry in the cacheLink, to avoid fetching data we already got
func (c *Client) GetGuildAuditLogs(ctx context.Context, guildID Snowflake, flags ...Flag) (builder *guildAuditLogsBuilder) {
builder = &guildAuditLogsBuilder{}
builder.r.itemFactory = auditLogFactory
builder.r.flags = flags
builder.r.IgnoreCache().setup(c.cache, c.req, &httd.Request{
Ctx: ctx,
Method: httd.MethodGet,
Endpoint: endpoint.GuildAuditLogs(guildID),
}, nil)
return builder
}
//////////////////////////////////////////////////////
//
// REST Builders
//
//////////////////////////////////////////////////////
// guildAuditLogsBuilder for building the GetGuildAuditLogs request.
// TODO: support caching of audit log entries. So we only fetch those we don't have.
//generate-rest-params: user_id:Snowflake, action_type:uint, before:Snowflake, limit:int,
//generate-rest-basic-execute: log:*AuditLog,
type guildAuditLogsBuilder struct {
r RESTBuilder
}