问题
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