Discord.js: Suggestion: Addition of server roles by hierarchy

Created on 16 Aug 2017  Â·  2Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:
Currently there is the guild.roles property which outputs the roles of the server sorted by ID. It would be nice if along with this we could get a guild.rolesHierarchy which outputs the roles by ID sorted on their hierarchy within the guild.

Include a reproducible code sample here, if possible:
Currently in order to get the highest role the easiest method that I have found is as follows. Possibly this may also be possible directly with Collection.Sort(), however I could not figure out how to apply the sorting to the Collection

message.guild.roles.find(
    'position',
    parseInt(message.guild.roles
        .map(rp => rp.position)
        .sort((a, b) => a - b)
        .slice(-1)))

Clarification for this code and how it works:

We want to find the role that has the highest position. To do that we use Collection.find() based on the position property of the role. For the value of the position we want the highest one possible. To find the highest possible position of all roles on the server we map all roles by their position using Collection.map(). This array can then be sorted using ES6 Array.Sort() and by slicing off all but the last we get the highest possible position. All of this has to be parsed to an integer to be usable for Collection.find('Position', ...) as position expects a number.

Further details:

  • discord.js version: 11.1
  • node.js version: 8.1.3
  • Operating system: Windows 10 Creators Update
  • Priority this issue should have – please be realistic and elaborate if possible: Low

  • [ ] I found this issue while running code on a __user account__
  • [ ] I have also tested the issue on latest master, commit hash:
invalid

Most helpful comment

We chose not to implement this because you can do it in one line:

collectionOfRolesOrChannels.sort((a, b) => a.position - b.position || a.id - b.id);

All 2 comments

We chose not to implement this because you can do it in one line:

collectionOfRolesOrChannels.sort((a, b) => a.position - b.position || a.id - b.id);

Oh.. well that shows my lacking understanding of the Collection.sort that I mentioned. Closing then I suppose.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kvn1351 picture kvn1351  Â·  3Comments

tiritto picture tiritto  Â·  3Comments

Lombra picture Lombra  Â·  3Comments

smchase picture smchase  Â·  3Comments

Blumlaut picture Blumlaut  Â·  3Comments