简体   繁体   English

Discord,js TypeError:member.guild.roles.find 不是函数

[英]Discord,js TypeError: member.guild.roles.find is not a function

I keep on getting this error.我不断收到此错误。 I am making a welcomer bot for my discord server and I am trying to implement a role adding function when somebody joins and keep on getting that error.我正在为我的不和谐服务器制作一个欢迎机器人,当有人加入并继续收到该错误时,我正在尝试实现一个角色添加功能。

  
  const channel = member.channels.cache.find(ch => ch.name === 'welcome');
  
  if (!channel) return;
  
  channel.send(`Welcome to the Server, ${member}`);
});

bot.on('guildMemberAdd', member => {
role = member.addRole(member.guild.roles.find("name","member"));
});```

According to Discord.js Documentation , I think you can implement with:根据Discord.js Documentation ,我认为你可以实现:

member.roles.add(member.guild.roles.cache.find(x => x.name == "scriptkiddie"), "Reason");

Something like this, as you can fetch Guild Roles with像这样的东西,因为你可以用

member.guild.roles.cache

I believe roles is not the value you expect it to be.我相信roles不是你期望的价值。 If it was an array, or any object with a find method, you wouldn't get that error.如果它是一个数组或任何具有find方法的对象,则不会出现该错误。 Log the value of roles to determine what data type it is, then change accordingly.记录roles的值以确定它是什么数据类型,然后相应地更改。

console.log(member.guild.roles)

In the latest version of discord.js you must do member.guild.roles.cache to get the collection.在最新版本的member.guild.roles.cache ,您必须执行member.guild.roles.cache才能获取集合。

Then do member.guild.roles.cache.find(....) with whatever you want to find :)然后用你想找到的任何东西做member.guild.roles.cache.find(....) :)

Hope this helps!希望这可以帮助!

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

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