From 1782301df98914803af0f7d950b5482cd5524b6e Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 2 Jan 2024 21:06:47 -0800 Subject: [PATCH] discord: Add SortRolesByPosition helper function --- discord/guild.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/discord/guild.go b/discord/guild.go index 0ab4938..f2c7c09 100644 --- a/discord/guild.go +++ b/discord/guild.go @@ -1,6 +1,9 @@ package discord -import "time" +import ( + "sort" + "time" +) // https://discord.com/developers/docs/resources/guild#guild-object type Guild struct { @@ -363,6 +366,15 @@ func (r Role) IconURLWithType(t ImageType) string { return "https://cdn.discordapp.com/role-icons/" + r.ID.String() + "/" + t.format(r.Icon) } +// SortRolesByPosition sorts the roles by their position. +// Roles with a higher position will be first in the slice, similar to how +// Discord sorts roles in the client. +func SortRolesByPosition(roles []Role) { + sort.Slice(roles, func(i, j int) bool { + return roles[i].Position > roles[j].Position + }) +} + // https://discord.com/developers/docs/resources/guild#guild-member-object // // The field user won't be included in the member object attached to