我有一段代码,如果没有提及,则返回带有提及用户或消息作者的头像的Rich Rich Embed,该代码无需提及即可工作,但提及某人时则无效。 (控制台中没有错误)
if (message.content === prefix + "avatar") {
const user = message.mentions.users.first()
if (!message.mentions.users.size) {
const Embed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.avatarURL())
.setColor("#13e2c0")
.setTitle("Avatar")
.setImage(message.author.avatarURL())
message.channel.send(Embed)
}
const Embed = new Discord.MessageEmbed()
.setAuthor(user.tag, user.avatarURL())
.setColor("#13e2c0")
.setTitle("Avatar")
.setImage(user.avatarURL())
message.channel.send(Embed)
}
答案 0 :(得分:1)
如果没有用户提及,message.mentions.users.size
将始终为0,而不是NULL
或undefined
。
if (message.content.toLowerCase().startsWith(prefix + "avatar")) {
const user = message.mentions.users.first() || message.author;
const Embed = new Discord.MessageEmbed()
.setAuthor(user.tag, user.avatarURL())
.setColor("#13e2c0").setTitle("Avatar")
.setImage(user.avatarURL())
return message.channel.send(Embed);
}