我的机器人可以通过提及的方式发布角色,但是我希望通过名称来命名(这样就无需对整个角色执行ping操作。这是命令的语法:!role <@user> <role name>
我正在使用discord.js@v12
const Discord = require("discord.js");
module.exports.run = async(bot, message, args) => {
if (!message.member.roles.cache.has('706754890294099990')) return message.channel.send(`${message.author}, roles can only be assigned by admins`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
let rMember = message.mentions.members.first()
if (!rMember) return message.reply(`You didn't mention any user`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
let role = message.guild.roles.cache.find(role => role.name. == args[1]) || message.mentions.roles.first()
if (!role) return message.channel.send(`${message.author}, You haven't written any role name`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
if (rMember.roles.cache.has(role.id)) return message.reply(`This member already has this role!`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
if (rMember.roles.cache.has(role.id)) {
} else {
await (rMember.roles.add(role.id));
message.channel.send(`${message.author} Added __${role.name}__ to ${rMember}`);
}
}
module.exports.help = {
name: "role"
}
答案 0 :(得分:0)
如果您的角色名称中包含空格,则表示您的args
将如下所示:['@user', 'Name', 'in', 'parts']
要获得全名,您需要将所有参数(第一个参数即用户除外)连接在一起。为此,您可以使用Array.slice()
和Array.join()
;这是一个例子:
let roleName = args
.slice(1) // Take every element from index 1 to the end
.join(' ') // Join them back with spaces
let role = message.guild.roles.cache.find(r => r.name == roleName) || message.mentions.roles.first()