提到用户时无法使用命令

时间:2020-08-13 20:37:52

标签: javascript node.js discord discord.js

我有一段代码,如果没有提及,则返回带有提及用户或消息作者的头像的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)
}

1 个答案:

答案 0 :(得分:1)

如果没有用户提及,message.mentions.users.size将始终为0,而​​不是NULLundefined


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);
}