以下代码曾经可以工作,但由于某种原因它不再有效,我不知道为什么 这是错误信息后的代码
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)
答案 0 :(得分:1)
有两个原因我能想到为什么这可能不像你预期的那样工作。我假设以下是正确的:
当您对 main.js 文件中的消息事件调用“执行”函数时,您可能没有传入所有三个变量。这将导致 Discord 也不会被传入,因此,在 index.js 文件中,这就是您的执行函数应该的样子
execute(message, args, Discord)
这将为您的代码提供合适的变量来运行。但是,请注意,您还需要将命令上的代码更新为完全相同的内容,以便正确接收所有变量
从错误来看,我认为这是我的两个原因中最有可能的一个。我强烈建议这样做,即使这不是错误,因为如果您添加更多包,这是一个很好的做法。
在我的假设中,我假设您没有在代码顶部定义 Discord。因此,没有定义 Discord,也没有创建 MessageEmbed,因为它无法获取 Discord 包。在modules.export
const { Discord } = require("discord.js");
请记住,一旦您这样做,您就不必重新定义客户端。您已经在 execute()