From cb59c78e32470c56073d5046fde0dc0cb377986d Mon Sep 17 00:00:00 2001 From: Sentinel Date: Tue, 17 Jan 2023 22:59:00 +0000 Subject: [PATCH] Add size parameters to IconURL and BannerURL (#1301) * Add missing parameters/structs Adds guild icon size parameter, guild banner size parameter, missing ClientStatus struct * Add missing endpoint for animated banners * feat: revert ClientStatus changes Revert addition of ClientStatus since another PR was already merged with it. * feat: add documentation for size parameter * refactor: move icon url logic into separate func Move logic for icon URLs in various functions into a single iconURL function. Similar to banner and avatar functions. Co-authored-by: Fedor Lapshin --- endpoints.go | 1 + structs.go | 45 ++++++++++++++++----------------------------- util.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/endpoints.go b/endpoints.go index ded4bb0..a2a05fe 100644 --- a/endpoints.go +++ b/endpoints.go @@ -97,6 +97,7 @@ var ( EndpointGuildEmojis = func(gID string) string { return EndpointGuilds + gID + "/emojis" } EndpointGuildEmoji = func(gID, eID string) string { return EndpointGuilds + gID + "/emojis/" + eID } EndpointGuildBanner = func(gID, hash string) string { return EndpointCDNBanners + gID + "/" + hash + ".png" } + EndpointGuildBannerAnimated = func(gID, hash string) string { return EndpointCDNBanners + gID + "/" + hash + ".gif" } EndpointGuildStickers = func(gID string) string { return EndpointGuilds + gID + "/stickers" } EndpointGuildSticker = func(gID, sID string) string { return EndpointGuilds + gID + "/stickers/" + sID } EndpointStageInstance = func(cID string) string { return EndpointStageInstances + "/" + cID } diff --git a/structs.go b/structs.go index 512b129..3ec90be 100644 --- a/structs.go +++ b/structs.go @@ -17,7 +17,6 @@ import ( "math" "net/http" "regexp" - "strings" "sync" "time" @@ -917,16 +916,8 @@ type GuildPreview struct { } // IconURL returns a URL to the guild's icon. -func (g *GuildPreview) IconURL() string { - if g.Icon == "" { - return "" - } - - if strings.HasPrefix(g.Icon, "a_") { - return EndpointGuildIconAnimated(g.ID, g.Icon) - } - - return EndpointGuildIcon(g.ID, g.Icon) +func (g *GuildPreview) IconURL(size string) string { + return iconURL(g.Icon, EndpointGuildIcon(g.ID, g.Icon), EndpointGuildIconAnimated(g.ID, g.Icon), size) } // GuildScheduledEvent is a representation of a scheduled event in a guild. Only for retrieval of the data. @@ -1139,24 +1130,19 @@ const ( ) // IconURL returns a URL to the guild's icon. -func (g *Guild) IconURL() string { - if g.Icon == "" { - return "" - } - - if strings.HasPrefix(g.Icon, "a_") { - return EndpointGuildIconAnimated(g.ID, g.Icon) - } - - return EndpointGuildIcon(g.ID, g.Icon) +// +// size: The size of the desired icon image as a power of two +// Image size can be any power of two between 16 and 4096. +func (g *Guild) IconURL(size string) string { + return iconURL(g.Icon, EndpointGuildIcon(g.ID, g.Icon), EndpointGuildIconAnimated(g.ID, g.Icon), size) } // BannerURL returns a URL to the guild's banner. -func (g *Guild) BannerURL() string { - if g.Banner == "" { - return "" - } - return EndpointGuildBanner(g.ID, g.Banner) +// +// size: The size of the desired banner image as a power of two +// Image size can be any power of two between 16 and 4096. +func (g *Guild) BannerURL(size string) string { + return bannerURL(g.Banner, EndpointGuildBanner(g.ID, g.Banner), EndpointGuildBannerAnimated(g.ID, g.Banner), size) } // A UserGuild holds a brief version of a Guild @@ -1387,9 +1373,10 @@ func (m *Member) Mention() string { } // AvatarURL returns the URL of the member's avatar -// size: The size of the user's avatar as a power of two -// if size is an empty string, no size parameter will -// be added to the URL. +// +// size: The size of the user's avatar as a power of two +// if size is an empty string, no size parameter will +// be added to the URL. func (m *Member) AvatarURL(size string) string { if m.Avatar == "" { return m.User.AvatarURL(size) diff --git a/util.go b/util.go index c48342c..957f301 100644 --- a/util.go +++ b/util.go @@ -107,3 +107,19 @@ func bannerURL(bannerHash, staticBannerURL, animatedBannerURL, size string) stri } return URL } + +func iconURL(iconHash, staticIconURL, animatedIconURL, size string) string { + var URL string + if iconHash == "" { + return "" + } else if strings.HasPrefix(iconHash, "a_") { + URL = animatedIconURL + } else { + URL = staticIconURL + } + + if size != "" { + return URL + "?size=" + size + } + return URL +}