Discord.js: Feature Request: <Role>.exists();

Created on 31 Aug 2020  路  2Comments  路  Source: discordjs/discord.js

Is your feature request related to a problem? Please describe.
I made a add role command and it is a very complicated code, the command can take 3 roles max to add to a user and if the person only supplies 1 role and other 2 are undefined, and so when I try to check if any of the roles exists in the server by using <Guild>.roles.get(); it would return undefined for the other 2 and it can be frustrating sometimes. If there is <Role>.exists which returns true or false would make things easier for a lot of people.

Describe the ideal solution
When you do <Role>.exists(role.id) it would return boolean. Making everything easier

Describe alternatives you've considered
I tried using <Role>.get() it is not that reliable.

question (please use Discord instead)

Most helpful comment

First of all <Role> being used to denote a Role class instance both #get and #exists make absolutely no sense here. If it didn't exist you wouldn't have an instance for it (unless you mean the Role is already deleted, for which Role#deleted exists - which does not seem to be your intention here)

Assuming by <Role> you actually mean a role collection (for example from <Guild>.roles.cache - which would make more sense in the context you provide):

Collection (the structure we use for our caches) extends the native JS Map structure. Map#get is a hashtable lookup and about as reliable as you can get - i'm not too sure what you mean by "it is not that reliable". The boolean alternative to Map#get is Map#has and also exists on the native JS Map structure implementation we extend for Collection.

If you want to check for multiple roles to exists you can utilize Collection#some with an according callback provided

All 2 comments

You can simply check if the returned value is undefined or not.

const role = guild.roles.get(id);
if (role) console.log("The role exist.");
else console.log("The role does not exist.");

First of all <Role> being used to denote a Role class instance both #get and #exists make absolutely no sense here. If it didn't exist you wouldn't have an instance for it (unless you mean the Role is already deleted, for which Role#deleted exists - which does not seem to be your intention here)

Assuming by <Role> you actually mean a role collection (for example from <Guild>.roles.cache - which would make more sense in the context you provide):

Collection (the structure we use for our caches) extends the native JS Map structure. Map#get is a hashtable lookup and about as reliable as you can get - i'm not too sure what you mean by "it is not that reliable". The boolean alternative to Map#get is Map#has and also exists on the native JS Map structure implementation we extend for Collection.

If you want to check for multiple roles to exists you can utilize Collection#some with an according callback provided

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

LLamaFTL picture LLamaFTL  路  3Comments

tiritto picture tiritto  路  3Comments

xCuzImPro picture xCuzImPro  路  3Comments

kvn1351 picture kvn1351  路  3Comments