如何解决此问题,以向用户添加命令中提到的角色?

时间:2019-06-21 04:37:37

标签: javascript node.js

我正在为服务器编码一个自定义discord机器人,但遇到了这个问题。我想向未预定义的用户添加角色。 IE:我希望能够&addrole @user anyrole并将该角色添加到用户。这是代码:

const Discord = require("discord.js");

module.exports.run = async (bot, message, args) => {

//!addrole @andrew anyrole
if(!message.member.hasPermission("MANAGE_MEMBERS")) return 
message.reply("Sorry pal, you can't do that.");
let rMember = message.guild.member(message.mentions.users.first()) || 
message.guild.members.get(args[0]);
if(!rMember) return message.reply("Couldn't find that user, yo.");
let role = args.join(" ").slice(22);
if(!role) return message.reply("Specify a role!");
let gRole = message.guild.roles.find('name', any);
if(!gRole) return message.reply("Couldn't find that role.");

if(rMember.roles.has(gRole.id)) return message.reply("They already have 
that role.");
await(rMember.addRole(gRole.id));

try{
await rMember.send(`Congrats, you have been given the role 
${gRole.name}`)
}catch(e){
message.channel.send(`Congrats to <@${rMember.id}>, they have been given 
the role ${gRole.name}. We tried to DM them, but their DMs are locked.`)
}
}

module.exports.help = {
name: "addrole"
}

0 个答案:

没有答案