API/Discord: add GuildPreview

This commit is contained in:
mavolin 2020-05-15 20:02:49 +02:00
parent 795a69ca7d
commit b5dedf9408
No known key found for this signature in database
GPG Key ID: D8681218EDF216DF
2 changed files with 77 additions and 0 deletions

View File

@ -80,6 +80,15 @@ func (c *Client) Guild(id discord.Snowflake) (*discord.Guild, error) {
return g, c.RequestJSON(&g, "GET", EndpointGuilds+id.String())
}
// GuildPreview returns the guild preview object for the given id, even if the
// user is not in the guild.
//
// This endpoint is only for public guilds.
func (c *Client) GuildPreview(id discord.Snowflake) (*discord.GuildPreview, error) {
var g *discord.GuildPreview
return g, c.RequestJSON(&g, "GET", EndpointGuilds+id.String()+"/preview")
}
// GuildWithCount returns the guild object for the given id.
// This will also set the ApproximateMembers and ApproximatePresences fields
// of the guild struct.

View File

@ -1,5 +1,7 @@
package discord
import "strings"
type Guild struct {
ID Snowflake `json:"id,string"`
Name string `json:"name"`
@ -94,6 +96,72 @@ func (g Guild) SplashURL() string {
g.ID.String() + "/" + g.Splash + ".png"
}
// https://discord.com/developers/docs/resources/guild#guild-preview-object
type GuildPreview struct {
// ID is the guild id.
ID Snowflake `json:"id"`
// Name is the guild name (2-100 characters).
Name string `json:"name"`
// Icon is the icon hash.
Icon Hash `json:"icon"`
// Splash is the splash hash.
Splash Hash `json:"splash"`
// DiscoverySplash is the discovery splash hash.
DiscoverySplash Hash `json:"discovery_splash"`
// Emojis are the custom guild emojis.
Emojis []Emoji `json:"emojis"`
// Features are the enabled guild features.
Features []GuildFeature `json:"guild_features"`
// ApproximateMembers is the approximate number of members in this guild.
ApproximateMembers uint64 `json:"approximate_member_count"`
// ApproximatePresences is the approximate number of online members in this
// guild.
ApproximatePresences uint64 `json:"approximate_presence_count"`
// Description is the description for the guild.
Description string `json:"description,omitempty"`
}
// IconURL returns the URL to the guild icon. An empty string is removed if
// there's no icon.
func (g GuildPreview) IconURL() string {
if g.Icon == "" {
return ""
}
base := "https://cdn.discordapp.com/icons/" + g.ID.String() + "/" + g.Icon
if strings.HasPrefix(g.Icon, "a_") {
return base + ".gif"
}
return base + ".png"
}
// SplashURL returns the URL to the guild splash, which is the invite page's
// background.
func (g GuildPreview) SplashURL() string {
if g.Splash == "" {
return ""
}
return "https://cdn.discordapp.com/splashes/" + g.ID.String() + "/" + g.Splash + ".png"
}
// SplashURL returns the URL to the guild splash, which is the invite page's
// background.
func (g GuildPreview) DiscoverySplashURL() string {
if g.Splash == "" {
return ""
}
return "https://cdn.discordapp.com/discovery-splashes/" +
g.ID.String() + "/" + g.DiscoverySplash + ".png"
}
type Role struct {
ID Snowflake `json:"id,string"`
Name string `json:"name"`