无法使用startsWith Discord.js问题 [英] Can not use startsWith Discord.js question
本文介绍了无法使用startsWith Discord.js问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的角色命令。
我正在尝试使用 startsWith
来赋予角色:如果arg的首字母与
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`);
};
我更新了当前代码,但仍然无法根据args赋予角色依赖或首字母!
I updated the current code but still can not give roles depending or first letters on args !
推荐答案
使用 .find()
的函数版本是一个好主意,但是您的错误在于您使用 .startsWith()
:这不是属性,而是方法。
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()));
这篇关于无法使用startsWith Discord.js问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文