siscodeorg / sisbase

An easy to use discord bot base for D#+. Discord Link :
https://discord.gg/ycvm6xJ
MIT License
1 stars 0 forks source link

Hierarchy Utils #38

Closed alikindsys closed 4 years ago

alikindsys commented 4 years ago

Problem

Currently on dsp, if a bot attempts to add a role which is higher on the higherarchy then itself an UnauthorizedException will be thrown. There exists an int | DiscordMember#Hierarchy property which gives the position of the member on the role hierarchy, but thats hidden and its more common to see a try-catch then that property being used.

Proposal

The creation of an HierarchyUtils that adds the following functions

DiscordRole GetHighestRole(this DiscordMember m);
bool IsAbove(this DiscordMember m, DiscordRole r);
bool IsAbove(this DiscordRole l, DiscordRole r);

with more being added in the future.

That would allow for the following code

try {
    await member.GrantRoleAsync(role);
} catch (UnauthorizedException ex) {
   //Send message
}

to be simplified to:

if(bot.IsAbove(role)){
   await member.GrantRoleAsync(role);
} else {
  //Send message
}