首页 > 解决方案 > 如何从数组 discord.js 添加嵌入消息?

问题描述

我制作了一个关于真心话大冒险的机器人。当我键入 +t 时,它会发送我添加的事实之一。现在我想发送真相或敢于嵌入喜欢这个 嵌入真相或敢于

那么我该怎么做呢?请记住,我将使用嵌入我的代码来回复数组中的随机数据

// Array of possible truth replies
const t = [
    "If you could be invisible, what is the first thing you would do?", 
    "What's the strangest dream you've ever had?",
    "What are the top three things you look for in a boyfriend/girlfriend?",
    "What is your worst habit?",
    "How many stuffed animals do you own?",
    "What is your biggest insecurity?"
];

// Array of possible dare replies
const d = [
    "Do a free-style rap for the next minute.",
    "Let another person post a status on your behalf.",
    "Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
    "Let the other players go through your phone for one minute.",
    "Smell another player's armpit",
    "Smell another player's barefoot.",
    "Tell everyone your honest opinion of the person who sent this command."
];

// Handle all commands here
client.on('message', message => {

    // Don't reply to itself
    if (message.author.id === client.user.id) return;

    // If there is no + (prefix) at the beginning of the message, exit function
    if (!message.content.startsWith(prefix)) return;

    // Remove the prefix from the message -> our command
    const command = message.content.substring(prefix.length);

    // Match the command
    if (command === "t") { // Truth
        const truth = t[Math.floor(Math.random() * t.length)];
        message.channel.send(truth);
    } else if (command === "d") { // Dare
        const dare = d[Math.floor(Math.random() * d.length)];
        message.channel.send(dare);
    } else if (command === "help") { // Help

        const help = new Discord.MessageEmbed()
            .setColor('#72dfa3')
            .setTitle(`Truth Or Dare`)
            .addFields(
                { name: '``+help``', value: 'For help' },
                { name: '``+t``', value: 'For Truth' },
                { name: '``+d``', value: 'For Your Dare' },
                { name: '``Created By``', value: 'AlpHa Coder [Labib Khan]' },
            )
            .setTimestamp()
            .setFooter(`${message.author.username}`, message.author.displayAvatarURL());

        message.channel.send(help);

    } else { // No match found, invalid command
        message.channel.send("Invalid command, type `+help` for help.");
    }

});

标签: javascriptdiscorddiscord.js

解决方案


好吧,您只需要像在帮助命令中那样使用嵌入。

这里的代码:

if (command === "t") { // Truth
    const truth = t[Math.floor(Math.random() * t.length)];
    message.channel.send(new Discord.MessageEmbed()
        .setTitle("Truth")
        .setDescription(truth));
} else if (command === "d") { // Dare
    const dare = d[Math.floor(Math.random() * d.length)];
    message.channel.send(new Discord.MessageEmbed()
        .setTitle("Dare")
        .setDescription(dare));
}

推荐阅读