简体   繁体   中英

replacement for guild.roles.find() in newer versions of discord.js

I've been coding a discord bot in discord.js, and I need a way to recognize a role by its name, so I looked up how to do that in the documentation, but couldn't find exactly what I was looking for (I'm very new to javascript, so I might have seen what I was looking for but just not understood it. That's also why the code will likely make you cringe.) I looked up how to do this online, and everyone said to use the guild.roles.find() function, so I put it into my code and when it didn't work. I then looked for the guild.roles.find() function in the documentation, and it wasn't there at all, leading me to believe it had been deleted. If anyone knows the solution to this, it would be much appreciated.

let role = message.guild.roles.find(role => role.name === stringSplit[i]);

( stringSplit[i] is the name of the role which has been input by a user.)

If you want to get all the roles of a certain guild you can do

const roles = message.guild.roles.cache.array();

Now you can filter these roles to get the one you want with the name property.

message.guild.roles.cache returns a collection: you can take a look here for all the available methods: https://discord.js.org/#/docs/collection/master/class/Collection?scrollTo=find

You can access roles collection using cache .

Your solution would be:

let role = message.guild.roles.cache.find(role => role.name === stringSplit[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM