Gang.findOne({leaderUserID: message.author.id, leaderUserName: message.member.user.tag, serverID: message.guild.id}, (err, gang) => {
if(err) console.log(err);
let embed = new Discord.MessageEmbed()
.setTitle("Gang")
.setColor("4000FF")
.setThumbnail(message.author.displayAvatarURL)
gangName = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
if(!gang){
const newGang = new Gang({
leaderUserID: message.author.id,
leaderUserName: message.member.user.tag,
serverID: message.guild.id,
gangName: gangName,
points: "1"
});
guild.roles.create({
data: {
name: gangName,
color: `GREY`
},
reason: `Gang created by ${message.member.user.tag} at ${message.timestamp}`
});
let role = message.guild.roles.cache.find(guild => guild.name === gangName);
console.log(`role = ${role}`);
member.roles.add(role);
newGang.save().catch(err => console.log(err));
embed.addField("Gang Creation", "-15 points", true);
embed.addField("Gang Name", gangName, true);
message.channel.send(embed);
return;
除分配角色外,其他所有操作均有效。我将帮派名称设置为命令的第二部分,使用该名称创建角色,然后将角色设置为
let role = message.guild.roles.cache.find(guild => guild.name === gangName);
但随后,角色的console.log显示为“未定义”。
它如何未定义?我刚刚创建了角色,然后进行搜索。
答案 0 :(得分:2)
guild.roles.create是一个异步函数,因此您可以使用await
let role = await guild.roles.create({
data: {
name: gangName,
color: `GREY`
},
reason: `Gang created by ${message.member.user.tag} at ${message.timestamp}`
});
console.log(`role = ${role}`);
member.roles.add(role);