编码嵌入消息

时间:2020-08-06 17:18:44

标签: javascript node.js discord discord.js

我正在浏览discord.js指南,并在嵌入我的消息时发现了这段代码 下面的代码

client.on('message', message => {
    // If the message is "how to embed"
    if (message.content === 'how to embed') {
      // We can create embeds using the MessageEmbed constructor
      // Read more about all that you can do with the constructor
      // over at https://discord.js.org/#/docs/main/master/class/MessageEmbed
      const embed = new MessageEmbed()
        .setTitle('A slick little embed')
        .setColor(0xff0000)      
        .setDescription('Hello, this is a slick embed!');
      message.channel.send(embed);
    }
  });

但是,当我运行命令时,出现以下错误消息 const embed =新的MessageEmbed() ^

ReferenceError: MessageEmbed is not defined
    at Client.<anonymous> (C:\Users\lol\Desktop\All Disc\Test all\index.js:29:21)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\lol\Desktop\All Disc\Test all\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\lol\Desktop\All Disc\Test all\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\lol\Desktop\All Disc\Test all\node_modules\ws\lib\websocket.js:797:20)

您能帮助我进一步进行吗?

1 个答案:

答案 0 :(得分:-1)

我自己找到了答案,您只需要将其添加到您的代码中

const Discord = require('discord.js');
const { MessageEmbed } = require("discord.js");

然后您可以继续输入代码

client.on('message', message => {
  // If the message is "how to embed"
  if (message.content === 'how to embed') {
    // We can create embeds using the MessageEmbed constructor
    // Read more about all that you can do with the constructor
    // over at https://discord.js.org/#/docs/main/master/class/MessageEmbed
    const embed = new MessageEmbed()
      // Set the title of the field
      .setTitle('A slick little embed')
      // Set the color of the embed
      .setColor(0xff0000)
      // Set the main content of the embed
      .setDescription('Hello, this is a slick embed!');
    // Send the embed to the same channel as the message
    message.channel.send(embed);
  }
});

为我工作也希望也为您工作