无法读取未定义尝试创建嵌入的属性“MessageEmbed”

时间:2021-02-10 10:51:19

标签: javascript node.js discord discord.js bots

创建了一个禁止成员的不和谐机器人 我更喜欢在禁止某人后以嵌入的消息进行响应,但我所能做的只是一条普通消息我不知道为什么即使我有 discord.js 也无法创建嵌入迄今为止 (v12)

我得到的错误:

(node:9604) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'MessageEmbed' of undefined
    at Object.execute (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\commands\ping.js:7:40)
    at Client.<anonymous> (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\main.js:52:35)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Som3a12 RL\Desktop\Advanced coding\discord bot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9604) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9604) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我的代码:

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

module.exports = {
  name: 'ping',
  description: "this is a ping cmd",
  execute(message, args, Discord, client) {
    const embed = new Discord.MessageEmbed()
      .setTitle('Bots ping')
      .setColor('RANDOM')
      .setDescription(`Latency is ${Date.now() - message.createdTimestamp}ms. 
            API Latency is ${Math.round(client.ws.ping)}ms`); 

    message.channel.send(embed)              
  }
}

2 个答案:

答案 0 :(得分:1)

正如我在您的错误中看到的,您的主文件中也有该代码的问题。

首先,您将在主代码文件(如 index.js)中请求 discord.js。然后在您的命令行中设置执行,因此您需要返回主代码文件并将命令设置为:

  if (command === 'ping') {
            client.commands.get('ping').execute(message, args, Discord, client);
}

我们再次回到命令文件中,您将代码设置为:

module.exports = {
    name: 'ping',
    description: "this is a ping cmd",
execute(message, args, Discord, client){
             const embed = new Discord.MessageEmbed()
            .setTitle('Bots ping')
            .setColor('RANDOM')
            .setDescription(`Latency is ${Date.now() - message.createdTimestamp}ms. 
            API Latency is ${Math.round(client.ws.ping)}ms`); 

        message.channel.send(embed)              
    }
    }

这应该可以解决问题!

答案 1 :(得分:0)

您定义了 2 个 Discord,从第 6 行删除 Discord,然后重试

const Discord = require('discord.js');
        ^

execute(message, args, Discord, client){
                         ^