SinisterRectus / Discordia

Discord API library written in Lua for the Luvit runtime environment
MIT License
697 stars 143 forks source link

GuildMember:getRoles() sorting #324

Open RiskoZoSlovenska opened 2 years ago

RiskoZoSlovenska commented 2 years ago

Currently, there is no easy way to get a sorted list of user's roles, despite the fact that GuildMember internally sorts the list of roles to get the role for :getHighestRole() and :getColor(). This Issue is actually two issues:

  1. It would be nice for GuildMember to provide a method to get a sorted list of roles.
  2. :getHighestRole() and :getColor() are doing unnecessary work when they sort the whole list (sorting is of O(n log n) complexity) just to get the highest role, for which a simple linear pass would work.

Just an idea here, but considering that :getRoles() (and thus all related functions) reference the role cache, a possible solution could be to have the cache always sorted (or always sort the result of State:getGuildRoles()), which would solve both problems.


For reference, my original comment from the community Discord server:

Yet another 3.0 suggestion: have GuildMember:getRoles() sort the roles according to their "height" from highest to lowest. Reason: Having the list sorted can't hurt, and it's helpful for, say, printing out a list of a user's roles in order. The GuildMember class already has a local sorter defined that it uses for :getHighestRole() and :getColor() (also, what's the point of sorting the whole list (O(n log n) iirc) when a simple linear pass to get the highest role will suffice?), so implementing this should be trivial (heck, it could even go on and replace :getHighestRole()). Or maybe not have :getRoles() sort the list, but rather make a :getRolesSorted() function.