Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit cb59c78

Browse files
Add size parameters to IconURL and BannerURL (bwmarrin#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 <[email protected]>
1 parent 345a9d6 commit cb59c78

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

endpoints.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ var (
9797
EndpointGuildEmojis = func(gID string) string { return EndpointGuilds + gID + "/emojis" }
9898
EndpointGuildEmoji = func(gID, eID string) string { return EndpointGuilds + gID + "/emojis/" + eID }
9999
EndpointGuildBanner = func(gID, hash string) string { return EndpointCDNBanners + gID + "/" + hash + ".png" }
100+
EndpointGuildBannerAnimated = func(gID, hash string) string { return EndpointCDNBanners + gID + "/" + hash + ".gif" }
100101
EndpointGuildStickers = func(gID string) string { return EndpointGuilds + gID + "/stickers" }
101102
EndpointGuildSticker = func(gID, sID string) string { return EndpointGuilds + gID + "/stickers/" + sID }
102103
EndpointStageInstance = func(cID string) string { return EndpointStageInstances + "/" + cID }

structs.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"math"
1818
"net/http"
1919
"regexp"
20-
"strings"
2120
"sync"
2221
"time"
2322

@@ -917,16 +916,8 @@ type GuildPreview struct {
917916
}
918917

919918
// IconURL returns a URL to the guild's icon.
920-
func (g *GuildPreview) IconURL() string {
921-
if g.Icon == "" {
922-
return ""
923-
}
924-
925-
if strings.HasPrefix(g.Icon, "a_") {
926-
return EndpointGuildIconAnimated(g.ID, g.Icon)
927-
}
928-
929-
return EndpointGuildIcon(g.ID, g.Icon)
919+
func (g *GuildPreview) IconURL(size string) string {
920+
return iconURL(g.Icon, EndpointGuildIcon(g.ID, g.Icon), EndpointGuildIconAnimated(g.ID, g.Icon), size)
930921
}
931922

932923
// GuildScheduledEvent is a representation of a scheduled event in a guild. Only for retrieval of the data.
@@ -1139,24 +1130,19 @@ const (
11391130
)
11401131

11411132
// IconURL returns a URL to the guild's icon.
1142-
func (g *Guild) IconURL() string {
1143-
if g.Icon == "" {
1144-
return ""
1145-
}
1146-
1147-
if strings.HasPrefix(g.Icon, "a_") {
1148-
return EndpointGuildIconAnimated(g.ID, g.Icon)
1149-
}
1150-
1151-
return EndpointGuildIcon(g.ID, g.Icon)
1133+
//
1134+
// size: The size of the desired icon image as a power of two
1135+
// Image size can be any power of two between 16 and 4096.
1136+
func (g *Guild) IconURL(size string) string {
1137+
return iconURL(g.Icon, EndpointGuildIcon(g.ID, g.Icon), EndpointGuildIconAnimated(g.ID, g.Icon), size)
11521138
}
11531139

11541140
// BannerURL returns a URL to the guild's banner.
1155-
func (g *Guild) BannerURL() string {
1156-
if g.Banner == "" {
1157-
return ""
1158-
}
1159-
return EndpointGuildBanner(g.ID, g.Banner)
1141+
//
1142+
// size: The size of the desired banner image as a power of two
1143+
// Image size can be any power of two between 16 and 4096.
1144+
func (g *Guild) BannerURL(size string) string {
1145+
return bannerURL(g.Banner, EndpointGuildBanner(g.ID, g.Banner), EndpointGuildBannerAnimated(g.ID, g.Banner), size)
11601146
}
11611147

11621148
// A UserGuild holds a brief version of a Guild
@@ -1387,9 +1373,10 @@ func (m *Member) Mention() string {
13871373
}
13881374

13891375
// AvatarURL returns the URL of the member's avatar
1390-
// size: The size of the user's avatar as a power of two
1391-
// if size is an empty string, no size parameter will
1392-
// be added to the URL.
1376+
//
1377+
// size: The size of the user's avatar as a power of two
1378+
// if size is an empty string, no size parameter will
1379+
// be added to the URL.
13931380
func (m *Member) AvatarURL(size string) string {
13941381
if m.Avatar == "" {
13951382
return m.User.AvatarURL(size)

util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,19 @@ func bannerURL(bannerHash, staticBannerURL, animatedBannerURL, size string) stri
107107
}
108108
return URL
109109
}
110+
111+
func iconURL(iconHash, staticIconURL, animatedIconURL, size string) string {
112+
var URL string
113+
if iconHash == "" {
114+
return ""
115+
} else if strings.HasPrefix(iconHash, "a_") {
116+
URL = animatedIconURL
117+
} else {
118+
URL = staticIconURL
119+
}
120+
121+
if size != "" {
122+
return URL + "?size=" + size
123+
}
124+
return URL
125+
}

0 commit comments

Comments
 (0)