Can not use startsWith Discord.js question

☆樱花仙子☆ 提交于 2019-12-13 10:28:41

问题


this is my role command.
I'm trying to use startsWith to give roles: if the arg's first letters are the same as the role, it should be assigned to the user.

exports.run = async function (msg,args) {
  if(!msg.member.hasPermission('MANAGE_ROLES')) return;
  const member = msg.mentions.members.first();
  const name = msg.mentions.users.size ? args.replace(/ +/, ' ').split(' ')[1] : args;
  const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(name.toLowerCase()));
  if (name && !role.size) return msg.channel.send("Role Not found");
  if (name && role.size > 1) return msg.channel.send("There is similar roles , Please supply more letters");
  await member.addRole(role);
  msg.channel.send(`**Role \`${role}\`  Given to **${member} Succesflly`);
};

I updated the current code but still can not give roles depending or first letters on args !


回答1:


Using the function version of .find() was a good idea, but your mistake is in the way you use .startsWith(): it's not a property, it's a method.

const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(args.split(/ +/g).slice(1).join(' ').toLowerCase()));


来源:https://stackoverflow.com/questions/54579465/can-not-use-startswith-discord-js-question

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