API: Added GuildWithCount

This commit is contained in:
diamondburned (Forefront) 2020-05-09 14:59:39 -07:00
parent ea27e7c88d
commit 0f38294048
3 changed files with 30 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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)