【问题标题】:Cannot read property 'MessageEmbed' of undefined trying to create an embed无法读取未定义的属性“MessageEmbed”尝试创建嵌入
【发布时间】:2021-02-10 22:33:54
【问题描述】:

创建了一个禁止成员的不和谐机器人我更喜欢在禁止某人后回复消息嵌入但我所能做的只是一条普通消息我不知道为什么即使我有 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)              
  }
}

【问题讨论】:

  • 您有两个 Discord 变量,似乎您没有将一个传递给 execute(message, args, Discord, client) 方法。您可能应该从该列表中删除Discord 并使用execute(message, args, client),但我不确定您如何称呼execute
  • @ZsoltMeszaros 我照你说的做了,但现在我收到一个新错误:const embed = new Discord.MessageEmbed() ^ ReferenceError: Discord is not defined

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


【解决方案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)              
    }
    }

这应该可以解决问题!

【讨论】:

    【解决方案2】:

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

    const Discord = require('discord.js');
            ^
    
    execute(message, args, Discord, client){
                             ^
    

    【讨论】:

      猜你喜欢
      • 2021-01-18
      • 2021-08-07
      • 2021-04-18
      • 2020-09-22
      • 2021-10-03
      • 1970-01-01
      • 2020-10-15
      • 2020-05-28
      • 2021-09-13
      相关资源
      最近更新 更多