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 <fe.lap.prog@gmail.com>
This commit is contained in:
Sentinel 2023-01-17 22:59:00 +00:00 committed by GitHub
parent 345a9d6466
commit cb59c78e32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 29 deletions

View file

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

View file

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

16
util.go
View file

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