Skip to content

Commit 65a4d06

Browse files
authored
Merge pull request #53 from DisgoOrg/feature/audit-log-fetching
Feature/audit log fetching
2 parents 4de058e + 13d7419 commit 65a4d06

9 files changed

+134
-20
lines changed

api/audit_log.go

+60-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package api
22

3+
import (
4+
"encoding/json"
5+
6+
"github.com/DisgoOrg/restclient"
7+
)
8+
39
// AuditLogChangeKey (https://discord.com/developers/docs/resources/audit-log#audit-log-change-object-audit-log-change-key) is data representing changes values/settings in an audit log.
410
type AuditLogChangeKey struct {
511
Name *string `json:"name"`
@@ -62,7 +68,7 @@ type AuditLogChangeKey struct {
6268
// AuditLogEvent is an 8-bit unsigned integer representing an audit log event.
6369
type AuditLogEvent int
6470

65-
// AuditLogEventGuildUpdate
71+
// AuditLogEventGuildUpdate ...
6672
const (
6773
AuditLogEventGuildUpdate AuditLogEvent = 1
6874
)
@@ -159,10 +165,59 @@ type AuditLogEntry struct {
159165
Reason *string `json:"reason"`
160166
}
161167

168+
// AuditLogFilterOptions fields used to filter audit-log retrieving
169+
type AuditLogFilterOptions struct {
170+
UserID Snowflake
171+
ActionType AuditLogEvent
172+
Before Snowflake
173+
Limit int
174+
}
175+
162176
// AuditLog (https://discord.com/developers/docs/resources/audit-log) These are logs of events that occurred, accessible via the Discord API.
163177
type AuditLog struct {
164-
Webhooks []Webhook
165-
Users []User
166-
Entries []AuditLogEntry
167-
Integrations []Integration
178+
Disgo Disgo `json:"-"`
179+
GuildID Snowflake `json:"-"`
180+
AuditLogFilterOptions AuditLogFilterOptions `json:"-"`
181+
Webhooks map[Snowflake]*Webhook `json:"webhooks"`
182+
Users map[Snowflake]*User `json:"users"`
183+
Integrations map[Snowflake]*Integration `json:"integrations"`
184+
Entries []AuditLogEntry `json:"entries"`
185+
}
186+
187+
// Unmarshal unmarshals a AuditLog
188+
func (l *AuditLog) Unmarshal(data []byte) (err error) {
189+
var i *struct {
190+
Webhooks []*Webhook `json:"webhooks"`
191+
Users []*User `json:"users"`
192+
Integrations []*Integration `json:"integrations"`
193+
Entries []AuditLogEntry `json:"entries"`
194+
}
195+
196+
if err = json.Unmarshal(data, &i); err != nil {
197+
return
198+
}
199+
200+
for _, webhook := range i.Webhooks {
201+
l.Webhooks[webhook.ID] = webhook
202+
}
203+
204+
for _, user := range i.Users {
205+
l.Users[user.ID] = user
206+
}
207+
208+
for _, integration := range i.Integrations {
209+
l.Integrations[integration.ID] = integration
210+
}
211+
212+
l.Entries = i.Entries
213+
return
214+
}
215+
216+
// Before gets new AuditLog(s) from Discord before the last one
217+
func (l *AuditLog) Before() (*AuditLog, restclient.RestError) {
218+
before := Snowflake("")
219+
if len(l.Entries) > 0 {
220+
before = l.Entries[len(l.Entries)-1].ID
221+
}
222+
return l.Disgo.RestClient().GetAuditLog(l.GuildID, l.AuditLogFilterOptions.UserID, l.AuditLogFilterOptions.ActionType, before, l.AuditLogFilterOptions.Limit)
168223
}

api/channels.go

+7
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,10 @@ type TextChannel struct {
124124
type StoreChannel struct {
125125
GuildChannel
126126
}
127+
128+
// PartialChannel contains basic info about a Channel
129+
type PartialChannel struct {
130+
ID Snowflake `json:"id"`
131+
Type ChannelType `json:"type"`
132+
Name *string `json:"name,omitempty"`
133+
}

api/entity_builder.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type EntityBuilder interface {
3535
CreateRole(guildID Snowflake, role *Role, updateCache CacheStrategy) *Role
3636
CreateVoiceState(guildID Snowflake, voiceState *VoiceState, updateCache CacheStrategy) *VoiceState
3737

38+
CreateAuditLog(guildID Snowflake, auditLogFilterOptions AuditLogFilterOptions, auditLog *AuditLog, updateCache CacheStrategy) *AuditLog
3839
CreateIntegration(guildID Snowflake, integration *Integration, updateCache CacheStrategy) *Integration
3940

4041
CreateTextChannel(channel *Channel, updateCache CacheStrategy) *TextChannel

api/guild.go

+5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ func (g *Guild) IconURL(size int) *string {
259259
return &u
260260
}
261261

262+
// GetAuditLogs gets AuditLog(s) for this Guild
263+
func (g *Guild) GetAuditLogs(userID Snowflake, actionType AuditLogEvent, before Snowflake, limit int) (*AuditLog, restclient.RestError) {
264+
return g.Disgo.RestClient().GetAuditLog(g.ID, userID, actionType, before, limit)
265+
}
266+
262267
// GetIntegrations gets all Integration(s) from the Guild. Requires PermissionManageServer
263268
func (g *Guild) GetIntegrations() ([]*Integration, restclient.RestError) {
264269
return g.Disgo.RestClient().GetIntegrations(g.ID)

api/restclient.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type RestClient interface {
5656
AddMemberRole(guildID Snowflake, userID Snowflake, roleID Snowflake) restclient.RestError
5757
RemoveMemberRole(guildID Snowflake, userID Snowflake, roleID Snowflake) restclient.RestError
5858

59+
GetAuditLog(guildID Snowflake, userID Snowflake, actionType AuditLogEvent, before Snowflake, limit int) (*AuditLog, restclient.RestError)
5960
GetIntegrations(guildID Snowflake) ([]*Integration, restclient.RestError)
6061
DeleteIntegration(guildID Snowflake, integrationID Snowflake) restclient.RestError
6162

api/webhook.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ const (
1212

1313
// Webhook (https://discord.com/developers/docs/resources/webhook) is a way to post messages to Discord using the Discord API which do not require bot authentication or use.
1414
type Webhook struct {
15-
ID Snowflake `json:"id"`
16-
Type WebhookType `json:"type"`
17-
Username Snowflake `json:"username"`
18-
GuildID *Snowflake `json:"guild_id"`
19-
ChannelID Snowflake `json:"channel_id"`
20-
User *User `json:"user"`
21-
Name string `json:"name"`
22-
Avatar string `json:"avatar"`
23-
Token *string `json:"token"`
24-
ApplicationID *Snowflake `json:"application_id"`
25-
SourceGuild *Guild `json:"source_guild"`
26-
SourceChannel *TextChannel `json:"source_channel"`
27-
URL *string `json:"url"`
15+
ID Snowflake `json:"id"`
16+
Type WebhookType `json:"type"`
17+
Username Snowflake `json:"username"`
18+
GuildID *Snowflake `json:"guild_id"`
19+
ChannelID Snowflake `json:"channel_id"`
20+
User *User `json:"user"`
21+
Name string `json:"name"`
22+
Avatar string `json:"avatar"`
23+
Token *string `json:"token"`
24+
ApplicationID *Snowflake `json:"application_id"`
25+
SourceGuild *PartialGuild `json:"source_guild"`
26+
SourceChannel *PartialChannel `json:"source_channel"`
27+
URL *string `json:"url"`
2828
}

example/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/DisgoOrg/log v1.0.3 h1:IjmZQQu/kuBIui22EdXmxzQGYwcPCJEkXa0Fe6W9fJk=
22
github.com/DisgoOrg/log v1.0.3/go.mod h1:KFGKhBQr37d6rxZ7p2bmc8BEmDH8DZbtgdlJDSCsE7I=
3-
github.com/DisgoOrg/restclient v1.2.3 h1:lSvsS63oYlnUH72dgIVfc+hWS24rPzHyaf22ZPF0E7Q=
4-
github.com/DisgoOrg/restclient v1.2.3/go.mod h1:PIhyYsT52w5T6m4LT+HTdKqY6NOIqo71Ai0rgaq9ZtM=
3+
github.com/DisgoOrg/restclient v1.2.4 h1:gSU4+XL9vvIOybD9kVcS47Et1IFdiBNgex+0VS1NWfY=
4+
github.com/DisgoOrg/restclient v1.2.4/go.mod h1:PIhyYsT52w5T6m4LT+HTdKqY6NOIqo71Ai0rgaq9ZtM=
55
github.com/PaesslerAG/gval v1.1.1 h1:4d7pprU9876+m3rc08X33UjGip8oV1kkm8Gh5GBuTss=
66
github.com/PaesslerAG/gval v1.1.1/go.mod h1:Fa8gfkCmUsELXgayr8sfL/sw+VzCVoa03dcOcR/if2w=
77
github.com/PaesslerAG/jsonpath v0.1.0 h1:gADYeifvlqK3R3i2cR5B4DGgxLXIPb3TRTH1mGi0jPI=

internal/entity_builder_impl.go

+12
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ func (b *EntityBuilderImpl) CreateRole(guildID api.Snowflake, role *api.Role, up
282282
return role
283283
}
284284

285+
// CreateAuditLog returns a new api.AuditLog entity
286+
func (b *EntityBuilderImpl) CreateAuditLog(guildID api.Snowflake, auditLogFilterOptions api.AuditLogFilterOptions, auditLog *api.AuditLog, updateCache api.CacheStrategy) *api.AuditLog {
287+
auditLog.Disgo = b.Disgo()
288+
auditLog.GuildID = guildID
289+
auditLog.AuditLogFilterOptions = auditLogFilterOptions
290+
291+
for _, user := range auditLog.Users {
292+
user = b.CreateUser(user, updateCache)
293+
}
294+
return auditLog
295+
}
296+
285297
// CreateIntegration returns a new api.Integration entity
286298
func (b *EntityBuilderImpl) CreateIntegration(guildID api.Snowflake, integration *api.Integration, updateCache api.CacheStrategy) *api.Integration {
287299
integration.Disgo = b.Disgo()

internal/restclient_impl.go

+33
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,39 @@ func (r *restClientImpl) MoveMember(guildID api.Snowflake, userID api.Snowflake,
435435
return
436436
}
437437

438+
func (r *restClientImpl) GetAuditLog(guildID api.Snowflake, userID api.Snowflake, actionType api.AuditLogEvent, before api.Snowflake, limit int) (auditLog *api.AuditLog, rErr restclient.RestError) {
439+
values := restclient.QueryValues{}
440+
if guildID != "" {
441+
values["guild_id"] = guildID
442+
}
443+
if userID != "" {
444+
values["user_id"] = userID
445+
}
446+
if actionType != 0 {
447+
values["action_type"] = actionType
448+
}
449+
if before != "" {
450+
values["before"] = guildID
451+
}
452+
if limit != 0 {
453+
values["limit"] = limit
454+
}
455+
compiledRoute, err := restclient.GetAuditLogs.Compile(values, guildID)
456+
if err != nil {
457+
return nil, restclient.NewError(nil, err)
458+
}
459+
rErr = r.Do(compiledRoute, nil, &auditLog)
460+
if rErr == nil {
461+
auditLog = r.Disgo().EntityBuilder().CreateAuditLog(guildID, api.AuditLogFilterOptions{
462+
UserID: userID,
463+
ActionType: actionType,
464+
Before: before,
465+
Limit: limit,
466+
}, auditLog, api.CacheStrategyNoWs)
467+
}
468+
return
469+
}
470+
438471
func (r *restClientImpl) GetIntegrations(guildID api.Snowflake) (integrations []*api.Integration, rErr restclient.RestError) {
439472
compiledRoute, err := restclient.GetIntegrations.Compile(nil, guildID)
440473
if err != nil {

0 commit comments

Comments
 (0)