Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Guild Member Cover & Accent Color support #1117

Merged
merged 9 commits into from Mar 2, 2022
6 changes: 6 additions & 0 deletions endpoints.go
Expand Up @@ -54,6 +54,12 @@ var (
uDiscriminatorInt, _ := strconv.Atoi(uDiscriminator)
return EndpointCDN + "embed/avatars/" + strconv.Itoa(uDiscriminatorInt%5) + ".png"
}
EndpointUserBanner = func(uID, cID string) string {
return EndpointCDNBanners + uID + "/" + cID + ".png"
}
EndpointUserBannerAnimated = func(uID, cID string) string {
return EndpointCDNBanners + uID + "/" + cID + ".gif"
}

EndpointUserGuilds = func(uID string) string { return EndpointUsers + uID + "/guilds" }
EndpointUserGuild = func(uID, gID string) string { return EndpointUsers + uID + "/guilds/" + gID }
Expand Down
7 changes: 7 additions & 0 deletions structs.go
Expand Up @@ -1170,6 +1170,13 @@ func (m *Member) AvatarURL(size string) string {

}

// BannerURL returns the URL of the member's banner image.
// 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 (m *Member) BannerURL(size string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this would be necessary, since Member doesn't have Banner property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not actually implemented on Discord API v9 but custom banner per server is actually a feature of the Discord client (Available on Edit Server Profile). In Discord Client app, the app fallback to the user banner if the field banner of Member is null.

I just added the fallback strategy now and wait the banner field to be released

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to just implement it when it goes out. DiscordGo is 1:1 mapping, so if API doesn't have it, we don't as well.

return bannerURL(m.User.Banner, EndpointUserBanner(m.User.ID, m.User.Banner), EndpointUserBannerAnimated(m.User.ID, m.User.Banner), size)
}

// A Settings stores data for a specific users Discord client settings.
type Settings struct {
RenderEmbeds bool `json:"render_embeds"`
Expand Down
14 changes: 14 additions & 0 deletions user.go
Expand Up @@ -54,6 +54,12 @@ type User struct {
// Whether the user has multi-factor authentication enabled.
MFAEnabled bool `json:"mfa_enabled"`

// The hash of the user's banner image.
Banner string `json:"banner"`

// User's banner color, encoded as an integer representation of hexadecimal color code
AccentColor int `json:"accent_color"`

// Whether the user is a bot.
Bot bool `json:"bot"`

Expand Down Expand Up @@ -92,3 +98,11 @@ func (u *User) AvatarURL(size string) string {
return avatarURL(u.Avatar, EndpointDefaultUserAvatar(u.Discriminator),
EndpointUserAvatar(u.ID, u.Avatar), EndpointUserAvatarAnimated(u.ID, u.Avatar), size)
}

// BannerURL returns the URL of the member's banner image.
// 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 (u *User) BannerURL(size string) string {
// The default/empty avatar case should be handled by the above condition
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this belongs to Member.BannerURL

return bannerURL(u.Banner, EndpointUserBanner(u.ID, u.Banner), EndpointUserBannerAnimated(u.ID, u.Banner), size)
}
16 changes: 16 additions & 0 deletions util.go
Expand Up @@ -92,3 +92,19 @@ func avatarURL(avatarHash, defaultAvatarURL, staticAvatarURL, animatedAvatarURL,
}
return URL
}

func bannerURL(bannerHash, staticCoverURL, animatedCoverURL, size string) string {
var URL string
if bannerHash == "" {
return ""
} else if strings.HasPrefix(bannerHash, "a_") {
URL = animatedCoverURL
} else {
URL = staticCoverURL
}

if size != "" {
return URL + "?size=" + size
}
return URL
}
42atomys marked this conversation as resolved.
Show resolved Hide resolved