From 982cd7d7c3e63178dd5405838cf812af89f69016 Mon Sep 17 00:00:00 2001 From: AI Date: Tue, 8 Nov 2016 05:06:08 +0500 Subject: [PATCH] Add support for the prune endpoint (#282) * Add support for the prune endpoint Adds functions to get the amount of members that could be pruned and to prune members using the prune endpoint. May close: bwmarrin/discordgo#147 * Deal with the go vet error Removed the json tags from the unexported struct. Should pass the tests now. * Make the PR consistent with the rest of the file. Removes url building in favour of string concatenation. * Fix the previous commit Adds back the result struct. Converts the uint32 to string. * Deal with golint comments * Remove the failing test Cleans up the uri concatenation. Removes the failing test due to incorrect permissions. --- ratelimit.go | 4 +-- restapi.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ restapi_test.go | 36 +++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/ratelimit.go b/ratelimit.go index a4674bf..bc320f0 100644 --- a/ratelimit.go +++ b/ratelimit.go @@ -7,7 +7,7 @@ import ( "time" ) -// Ratelimiter holds all ratelimit buckets +// RateLimiter holds all ratelimit buckets type RateLimiter struct { sync.Mutex global *Bucket @@ -15,7 +15,7 @@ type RateLimiter struct { globalRateLimit time.Duration } -// New returns a new RateLimiter +// NewRatelimiter returns a new RateLimiter func NewRatelimiter() *RateLimiter { return &RateLimiter{ diff --git a/restapi.go b/restapi.go index c3271f4..d3125f1 100644 --- a/restapi.go +++ b/restapi.go @@ -874,6 +874,71 @@ func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) { return } +// GuildPruneCount Returns the number of members that would be removed in a prune operation. +// Requires 'KICK_MEMBER' permission. +// guildID : The ID of a Guild. +// days : The number of days to count prune for (1 or more). +func (s *Session) GuildPruneCount(guildID string, days uint32) (count uint32, err error) { + count = 0 + + if days <= 0 { + err = errors.New("The number of days should be more than or equal to 1.") + return + } + + p := struct { + Pruned uint32 `json:"pruned"` + }{} + + uri := EndpointGuildPrune(guildID) + fmt.Sprintf("?days=%d", days) + body, err := s.RequestWithBucketID("GET", uri, nil, EndpointGuildPrune(guildID)) + + err = unmarshal(body, &p) + if err != nil { + return + } + + count = p.Pruned + + return +} + +// GuildPrune Begin as prune operation. Requires the 'KICK_MEMBERS' permission. +// Returns an object with one 'pruned' key indicating the number of members that were removed in the prune operation. +// guildID : The ID of a Guild. +// days : The number of days to count prune for (1 or more). +func (s *Session) GuildPrune(guildID string, days uint32) (count uint32, err error) { + + count = 0 + + if days <= 0 { + err = errors.New("The number of days should be more than or equal to 1.") + return + } + + data := struct { + days uint32 + }{days} + + p := struct { + Pruned uint32 `json:"pruned"` + }{} + + body, err := s.RequestWithBucketID("POST", EndpointGuildPrune(guildID), data, EndpointGuildPrune(guildID)) + if err != nil { + return + } + + err = unmarshal(body, &p) + if err != nil { + return + } + + count = p.Pruned + + return +} + // GuildIntegrations returns an array of Integrations for a guild. // guildID : The ID of a Guild. func (s *Session) GuildIntegrations(guildID string) (st []*GuildIntegration, err error) { diff --git a/restapi_test.go b/restapi_test.go index 0d68d6e..e4d111b 100644 --- a/restapi_test.go +++ b/restapi_test.go @@ -238,3 +238,39 @@ func TestChannelMessageSend2(t *testing.T) { t.Errorf("ChannelMessageSend returned error: %+v", err) } } + +// TestGuildPruneCount tests GuildPruneCount() function. This should not return an error. +func TestGuildPruneCount(t *testing.T) { + + if envGuild == "" { + t.Skip("Skipping, DG_GUILD not set.") + } + + if dg == nil { + t.Skip("Skipping, dg not set.") + } + + _, err := dg.GuildPruneCount(envGuild, 1) + if err != nil { + t.Errorf("GuildPruneCount returned error: %+v", err) + } +} + +/* +// TestGuildPrune tests GuildPrune() function. This should not return an error. +func TestGuildPrune(t *testing.T) { + + if envGuild == "" { + t.Skip("Skipping, DG_GUILD not set.") + } + + if dg == nil { + t.Skip("Skipping, dg not set.") + } + + _, err := dg.GuildPrune(envGuild, 1) + if err != nil { + t.Errorf("GuildPrune returned error: %+v", err) + } +} +*/