简体   繁体   English

在 discord.js 的较新版本中替换 guild.roles.find()

[英]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.我一直在 discord.js 中编写 discord 机器人,我需要一种通过名称识别角色的方法,所以我在文档中查找了如何做到这一点,但找不到我正在寻找的确切内容(我'我对 javascript 非常陌生,所以我可能已经看到了我要找的东西但只是不明白。这也是为什么代码可能会让你畏缩的原因。)我在网上查了如何做到这一点,每个人都说使用公会.roles.find() function,所以我把它放到我的代码中,当它不起作用时。 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.然后我在文档中查找了 guild.roles.find() function,它根本不存在,让我相信它已被删除。 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.) stringSplit[i]是用户输入的角色名称。)

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.现在您可以过滤这些角色以使用 name 属性获得您想要的角色。

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 message.guild.roles.cache返回一个集合:您可以在这里查看所有可用的方法: https://discord.js.org/#/docs/collection/master/class/Collection?scrollTo=find

You can access roles collection using cache .您可以使用cache访问roles集合。

Your solution would be:您的解决方案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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