forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_auditlog.go
73 lines (61 loc) · 2.37 KB
/
rest_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
package disgord
import (
"errors"
"strconv"
"github.com/andersfylling/disgord/endpoint"
"github.com/andersfylling/disgord/httd"
)
// GuildAuditLogsParams set params used in endpoint request
// https://discordapp.com/developers/docs/resources/audit-log#get-guild-audit-log-query-string-parameters
type GuildAuditLogsParams struct {
UserID Snowflake `urlparam:"user_id,omitempty"` // filter the log for a user id
ActionType uint `urlparam:"action_type,omitempty"` // the type of audit log event
Before Snowflake `urlparam:"before,omitempty"` // filter the log before a certain entry id
Limit int `urlparam:"limit,omitempty"` // how many entries are returned (default 50, minimum 1, maximum 100)
}
// GetQueryString .
func (params *GuildAuditLogsParams) GetQueryString() string {
separator := "?"
query := ""
if !params.UserID.Empty() {
query += separator + "user_id=" + params.UserID.String()
separator = "&"
}
if params.ActionType > 0 {
query += separator + "action_type=" + strconv.FormatUint(uint64(params.ActionType), 10)
separator = "&"
}
if !params.Before.Empty() {
query += separator + "before=" + params.Before.String()
separator = "&"
}
if params.Limit > 0 {
query += separator + "limit=" + strconv.Itoa(params.Limit)
}
return query
}
// GuildAuditLogs [GET] Returns an audit log object for the guild.
// Requires the 'VIEW_AUDIT_LOG' permission.
// Endpoint /guilds/{guild.id}/audit-logs
// Rate limiter [MAJOR] /guilds/{guild.id}/audit-logs
// Discord documentation https://discordapp.com/developers/docs/resources/audit-log#get-guild-audit-log
// Reviewed 2018-06-05
// Comment -
// Note Check the last entry in the cache, to avoid
// fetching data we already go
func GuildAuditLogs(client httd.Getter, guildID Snowflake, params *GuildAuditLogsParams) (log *AuditLog, err error) {
details := &httd.Request{
Ratelimiter: ratelimitGuildAuditLogs(guildID),
Endpoint: endpoint.GuildAuditLogs(guildID) + params.GetQueryString(),
}
resp, body, err := client.Get(details)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = errors.New("incorrect status code. Got " + strconv.Itoa(resp.StatusCode) + ", wants 200. Message: " + string(body))
return
}
err = unmarshal(body, &log)
return
}