Collection#find: pass a function instead

痴心易碎 提交于 2019-12-13 08:58:46

问题


I'm fairly new to node.js and I'm working on a discord bot with discord.js, I'm am trying to do assigned roles with commands. When I do the code and type in the command it works successfully but pops up with "DeprecationWarning: Collection#find: pass a function instead" in the console, how can I get rid of this?

https://i.imgur.com/agKFNsF.png


回答1:


This warning is caused by the following line:

var role = message.guild.roles.find('name', 'Epic Gamer');

On an earlier version of Discord.js, this would be valid, but they have now redone the find function. Instead of taking in a property and a value, you pass in a filtering function instead. This should work:

var role = message.guild.roles.find(role => role.name === "Epic Gamer")

Instead of passing in 'name' (the property), and 'Epic Gamer' (the value we want to search for/filter out), we pass in the arrow function role => role.name === 'Epic Gamer'. This is like mapping. find passes every role from message.guild.roles into the function as role, and then checks if the property we want equals the value we want.

If you would like to learn more about the find function, please check out the official documentation.




回答2:


Pass a predicate function in find method, take a look at the discord.js document on the find function.

Change the find statement to

var role = message.guild.roles.find(role => role.name === 'Epic Gamer');

Hope this will help!



来源:https://stackoverflow.com/questions/52247482/collectionfind-pass-a-function-instead

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!