From 4729d2c5ee3e09ccda1f57e3f5141fbbf0f8438a Mon Sep 17 00:00:00 2001 From: Dusty Pianalto Date: Sun, 19 Apr 2020 18:21:52 -0800 Subject: [PATCH] Add util for checking if caller has higher role than target --- utils.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/utils.go b/utils.go index a932bdc..0a1133f 100644 --- a/utils.go +++ b/utils.go @@ -9,6 +9,7 @@ package disgoman import ( "fmt" "github.com/bwmarrin/discordgo" + "sort" ) // GetDefaultStatusManager returns a default Status Manager @@ -21,6 +22,28 @@ func GetDefaultStatusManager() StatusManager { }, "10s"} } +// HasHigherRole checks if the caller has a higher role than the target in the given guild +func HasHigherRole(session *discordgo.Session, guildID string, callerID string, targetID string) bool { + caller, _ := session.GuildMember(guildID, callerID) + target, _ := session.GuildMember(guildID, targetID) + var callerRoles []*discordgo.Role + for _, roleID := range caller.Roles { + role, _ := session.State.Role(guildID, roleID) + callerRoles = append(callerRoles, role) + } + sort.Slice(callerRoles, func(i, j int) bool { return callerRoles[i].Position > callerRoles[j].Position }) + var targetRoles []*discordgo.Role + for _, roleID := range target.Roles { + role, _ := session.State.Role(guildID, roleID) + targetRoles = append(targetRoles, role) + } + sort.Slice(targetRoles, func(i, j int) bool { return targetRoles[i].Position > targetRoles[j].Position }) + if callerRoles[0].Position > targetRoles[0].Position { + return true + } + return false +} + // CheckPermissions checks the channel and guild permissions to see if the member has the needed permissions func CheckPermissions(session *discordgo.Session, memberID string, channel discordgo.Channel, perms Permission) bool { if perms == 0 {