【问题标题】:UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'MessageEmbed' of undefinedUnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“MessageEmbed”
【发布时间】:2021-04-18 15:38:21
【问题描述】:

以下代码曾经可以工作,但由于某种原因它不再工作我不知道为什么 这是错误消息后面的代码

module.exports = {
    name:  'stats',
    description: "This list shows the stats of the lol players",
    execute(message, args, Discord) {
       const newEmbed = new Discord.MessageEmbed()
       .setColor('#304281')
       .setTitle('League of Legends Stats')

       .setDescription('This is a list that shows you who has their stats coded into the bot, just copy and paste the command :)')
       .addFields({name: 'X', value: 'Y'},)
       .setImage('https://candid.technology/wp-content/uploads/2019/10/League-of-Legends-Wallpaper-5.jpg')
       .setFooter('if the stats seem wrong just click on update :)');

       message.channel.send(newEmbed)
    }
}
(node:2540) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'MessageEmbed' of undefined
    at Object.execute (C:\Users\A_AlA\Desktop\Aegir2\commands\stats.js:5:37)
    at Client.<anonymous> (C:\Users\A_AlA\Desktop\Aegir2\main.js:138:42)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\A_AlA\Desktop\Aegir2\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\A_AlA\Desktop\Aegir2\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\A_AlA\Desktop\Aegir2\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\A_AlA\Desktop\Aegir2\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\A_AlA\Desktop\Aegir2\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\A_AlA\Desktop\Aegir2\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
(node:2540) 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: 2)

【问题讨论】:

  • 你怎么称呼execute()?您可以将其添加到您的问题中吗?

标签: node.js discord discord.js


【解决方案1】:

我可以想到这可能无法按您预期的那样工作的两个原因。我假设以下是正确的:

  • 代码位于单独的文件中,但在名为“main.js”的文件上调用了执行函数
  • 提供的代码是文件中唯一的代码

Discord 没有在函数中正确传递

当您对 main.js 文件中的消息事件调用“执行”函数时,很可能您没有传入所有三个变量。这将导致 Discord 也不会被传入,因此,在 index.js 文件中,这就是您的执行函数 应该 的样子

execute(message, args, Discord)

这将为您的代码提供适当的变量来运行。但是,请注意,您需要将命令上的代码也更新为完全相同的内容,以便正确传递所有变量

不和谐是未定义的

从错误来看,我认为这是我的两个原因中最有可能的一个。我强烈建议这样做,即使这不是错误,因为如果您添加更多包,这是一个很好的做法。

在我的假设中,我假设您没有在代码顶部定义 Discord。因此,没有定义 Discord,也没有创建 MessageEmbed,因为它无法获取 Discord 包。将此添加到modules.export

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

请记住,一旦您这样做了,您就不必重新定义客户端。您已经在 execute()

中将其作为参数传递了

【讨论】:

  • 谢谢,不和谐没有传递到函数中
  • 不客气!请记住勾选复选标记以向其他人表明此答案对您有所帮助
猜你喜欢
  • 2021-01-18
  • 2021-08-07
  • 2021-05-20
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2021-05-18
  • 2021-01-15
相关资源
最近更新 更多