From 603114195afb77a17bc73fd9d644b9ac61df659c Mon Sep 17 00:00:00 2001 From: SamusAranX Date: Tue, 2 Jan 2024 09:09:44 +0100 Subject: [PATCH] feat(roles): add support for icons and unicode emojis (#1334) --------- Co-authored-by: Fedor Lapshin --- endpoints.go | 4 ++++ structs.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/endpoints.go b/endpoints.go index 8217876..f92cef1 100644 --- a/endpoints.go +++ b/endpoints.go @@ -42,6 +42,10 @@ var ( EndpointCDNChannelIcons = EndpointCDN + "channel-icons/" EndpointCDNBanners = EndpointCDN + "banners/" EndpointCDNGuilds = EndpointCDN + "guilds/" + EndpointCDNRoleIcons = EndpointCDN + "role-icons/" + EndpointRoleIcon = func(rID, cID string) string { + return EndpointCDNRoleIcons + rID + "/" + cID + ".png" + } EndpointVoice = EndpointAPI + "/voice/" EndpointVoiceRegions = EndpointVoice + "regions" diff --git a/structs.go b/structs.go index 6bea2e6..6d3b4fb 100644 --- a/structs.go +++ b/structs.go @@ -1349,6 +1349,12 @@ type Role struct { // This is a combination of bit masks; the presence of a certain permission can // be checked by performing a bitwise AND between this int and the permission. Permissions int64 `json:"permissions,string"` + + // The hash of the role icon. Use Role.IconURL to retrieve the icon's URL. + Icon string `json:"icon"` + + // The emoji assigned to this role. + UnicodeEmoji string `json:"unicode_emoji"` } // Mention returns a string which mentions the role @@ -1356,6 +1362,23 @@ func (r *Role) Mention() string { return fmt.Sprintf("<@&%s>", r.ID) } +// IconURL returns the URL of the role's icon. +// +// size: The size of the desired role icon as a power of two +// Image size can be any power of two between 16 and 4096. +func (r *Role) IconURL(size string) string { + if r.Icon == "" { + return "" + } + + URL := EndpointRoleIcon(r.ID, r.Icon) + + if size != "" { + return URL + "?size=" + size + } + return URL +} + // RoleParams represents the parameters needed to create or update a Role type RoleParams struct { // The role's name @@ -1368,6 +1391,12 @@ type RoleParams struct { Permissions *int64 `json:"permissions,omitempty,string"` // Whether this role is mentionable Mentionable *bool `json:"mentionable,omitempty"` + // The role's unicode emoji. + // NOTE: can only be set if the guild has the ROLE_ICONS feature. + UnicodeEmoji *string `json:"unicode_emoji,omitempty"` + // The role's icon image encoded in base64. + // NOTE: can only be set if the guild has the ROLE_ICONS feature. + Icon *string `json:"icon,omitempty"` } // Roles are a collection of Role