diff --git a/api/guild.go b/api/guild.go index 66b69c0..1bee6fc 100644 --- a/api/guild.go +++ b/api/guild.go @@ -2,6 +2,7 @@ package api import ( "io" + "net/url" "github.com/diamondburned/arikawa/discord" // for clarity "github.com/diamondburned/arikawa/utils/httputil" @@ -40,6 +41,20 @@ func (c *Client) Guild(id discord.Snowflake) (*discord.Guild, error) { return g, c.RequestJSON(&g, "GET", EndpointGuilds+id.String()) } +// GuildWithCount will also return ApproximateMembers and ApproximatePresences +// for the guild. +func (c *Client) GuildWithCount(id discord.Snowflake) (*discord.Guild, error) { + var g *discord.Guild + + return g, c.RequestJSON( + &g, "GET", + EndpointGuilds+id.String(), + httputil.WithSchema(c, url.Values{ + "with_counts": {"true"}, + }), + ) +} + // Guilds returns all guilds, automatically paginating. Be careful, as this // method may abuse the API by requesting thousands or millions of guilds. For // lower-level access, usee GuildsRange. Guilds returned have some fields diff --git a/discord/guild.go b/discord/guild.go index 2ca4bb4..63a8da8 100644 --- a/discord/guild.go +++ b/discord/guild.go @@ -49,6 +49,10 @@ type Guild struct { // Defaults to en-US, only set if guild has DISCOVERABLE PreferredLocale string `json:"preferred_locale"` + + // Only presented if WithCounts is true. + ApproximateMembers uint64 `json:"approximate_member_count,omitempty"` + ApproximatePresences uint64 `json:"approximate_presence_count,omitempty"` } // IconURL returns the URL to the guild icon. An empty string is removed if diff --git a/utils/httputil/options.go b/utils/httputil/options.go index 786e608..2fb7443 100644 --- a/utils/httputil/options.go +++ b/utils/httputil/options.go @@ -3,6 +3,7 @@ package httputil import ( "io" "net/http" + "net/url" "github.com/diamondburned/arikawa/utils/httputil/httpdriver" "github.com/diamondburned/arikawa/utils/json" @@ -50,9 +51,16 @@ func WithContentType(ctype string) RequestOption { func WithSchema(schema SchemaEncoder, v interface{}) RequestOption { return func(r httpdriver.Request) error { - params, err := schema.Encode(v) - if err != nil { - return err + var params url.Values + + if v, ok := v.(url.Values); ok { + params = v + } else { + v, err := schema.Encode(v) + if err != nil { + return err + } + params = v } r.AddQuery(params)