|
1 | 1 | package api
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/DisgoOrg/restclient" |
| 7 | +) |
| 8 | + |
3 | 9 | // 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.
|
4 | 10 | type AuditLogChangeKey struct {
|
5 | 11 | Name *string `json:"name"`
|
@@ -62,7 +68,7 @@ type AuditLogChangeKey struct {
|
62 | 68 | // AuditLogEvent is an 8-bit unsigned integer representing an audit log event.
|
63 | 69 | type AuditLogEvent int
|
64 | 70 |
|
65 |
| -// AuditLogEventGuildUpdate |
| 71 | +// AuditLogEventGuildUpdate ... |
66 | 72 | const (
|
67 | 73 | AuditLogEventGuildUpdate AuditLogEvent = 1
|
68 | 74 | )
|
@@ -159,10 +165,59 @@ type AuditLogEntry struct {
|
159 | 165 | Reason *string `json:"reason"`
|
160 | 166 | }
|
161 | 167 |
|
| 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 | + |
162 | 176 | // AuditLog (https://discord.com/developers/docs/resources/audit-log) These are logs of events that occurred, accessible via the Discord API.
|
163 | 177 | 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) |
168 | 223 | }
|
0 commit comments