Discord.MessageEmbed 不是构造函数

2 javascript discord discord.js

所以最近我尝试在我的不和谐机器人中添加一个命令处理程序。所有具有 Embeds 的命令都会出现错误“Discord.MessageEmbed 不是构造函数”。

\n

仅当我添加命令处理程序时才会发生这种情况,但如果没有它,命令也可以正常工作。

\n

更多详细信息:\ndiscord.js 版本 [12]\nnode 版本 [14.15.3]

\n

代码:

\n
const { MessageEmbed } = require("discord.js");\n\nmodule.exports = {\n    name: 'help',\n    description: "Sets up a dynamic help message!",\n    async execute(message, args, Discord, client) {\n        const purge = ':one:';\n        const music = ':two:';\n        const ban = ':three:';\n    \n        const purgeembed = new MessageEmbed()\n\n                         \n\n        .setColor('RANDOM')\n        .setTitle('purge info')\n        .setDescription("this is a embed for more info about the purge command and how to use it")\n        .addFields(\n            {name: ' \xe2\x80\x8d ', value: "`this command allows a user to purge (delete) messages that are less than 14 days old`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: '%purge [number of messages < 1000]'}\n        )\n        .setFooter("check bellow for how it works  [unfinished]");\n\n\n        const musicembed = new Discord.MessageEmbed()\n                                            \n        .setColor('RANDOM')\n        .setTitle('music info')\n        .setDescription("this is a embed for more info about the music command and how to use it")\n        .addFields(\n            {name: ' \xe2\x80\x8d ', value: "`this command allows a user to listen to music in a voice channel with alot of options like playing music from a link or by searching for the song through yt there is also a Queuing for music`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`assoiciated commands:`",},\n            {name: ' \xe2\x80\x8d ', value: "`......`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`......`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`......`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`......`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`......`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`%...`"},\n        )\n        .setFooter("check bellow for how it works  [unfinished]"); \n        \n\n\n\n        const banembed = new Discord.MessageEmbed()\n                                            \n        .setColor('RANDOM')\n        .setTitle('ban info')\n        .setDescription("this is a embed for more info about the ban command and how to use it")\n        .addFields(\n            {name: ' \xe2\x80\x8d ', value: "`this command allows a moderator to ban a user`", inline: true},\n            {name: ' \xe2\x80\x8d ', value: "`you can ban a user if you are a moderator but you need to give a reason to the owner first`"},\n            {name: ' \xe2\x80\x8d ', value: "`%ban @user`"},\n        )\n        .setFooter("check bellow for how it works  [unfinished]");\n\n\n \n        let embed = new Discord.MessageEmbed()\n            .setColor('RANDOM')\n            .setTitle('Choose a reaction for more info about a command!')\n            .setDescription('Choosing a reaction will allow you to get more info about a specific command!\\n\\n'\n                + `${purge} for purge help\\n\\n`\n                + `${music} for music help\\n\\n`\n                + `${ban} for ban help\\n\\n`);\n \n                message.channel.send(embed).then(embedMessage => {\n                    embedMessage.react('1\xef\xb8\x8f\xe2\x83\xa3');\n                    embedMessage.react('2\xef\xb8\x8f\xe2\x83\xa3');\n                    embedMessage.react('3\xef\xb8\x8f\xe2\x83\xa3');\n                  });\n\n           \n                  message.client.on('messageReactionAdd', (reaction, user) => {\n                   \n                  if (user.id === '795613081295650837'){\n\n                   }else{\n                    \n                    \n\n                      if (reaction.emoji.name === '1\xef\xb8\x8f\xe2\x83\xa3') {\n                        \n                        message.channel.bulkDelete(1);\n\n                      setTimeout(function(){ \n                                    message.channel.send(purgeembed);\n                                }, 600);\n                                        \n                        }\n                        if (reaction.emoji.name === '2\xef\xb8\x8f\xe2\x83\xa3') {\n\n                            message.channel.bulkDelete(1);\n\n                          setTimeout(function(){ \n                                    message.channel.send(musicembed);\n                                }, 600);\n                        }\n                        if (reaction.emoji.name === '3\xef\xb8\x8f\xe2\x83\xa3') {\n\n                            message.channel.bulkDelete(1);\n\n                          setTimeout(function(){ \n                                    message.channel.send(banembed);\n                                }, 600);\n                            }\n                     else {\n                        return;\n                    }}\n \n        });\n    }\n \n}   \n
Run Code Online (Sandbox Code Playgroud)\n

完整错误:

\n
(node:15924) UnhandledPromiseRejectionWarning: TypeError: Discord.MessageEmbed is not a constructor\n    at Object.execute (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\commands\\help.js:25:28)\n    at module.exports (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\events\\guild\\message.js:9:24)\n    at Client.emit (events.js:315:20)\n    at MessageCreateAction.handle (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\node_modules\\discord.js\\src\\client\\actions\\MessageCreate.js:31:14)\n    at Object.module.exports [as MESSAGE_CREATE] (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\node_modules\\discord.js\\src\\client\\websocket\\handlers\\MESSAGE_CREATE.js:4:32)\n    at WebSocketManager.handlePacket (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\node_modules\\discord.js\\src\\client\\websocket\\WebSocketManager.js:384:31)\n    at WebSocketShard.onPacket (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\node_modules\\discord.js\\src\\client\\websocket\\WebSocketShard.js:444:22)\n    at WebSocketShard.onMessage (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\node_modules\\discord.js\\src\\client\\websocket\\WebSocketShard.js:301:10)\n    at WebSocket.onMessage (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\node_modules\\ws\\lib\\event-target.js:132:16)\n    at WebSocket.emit (events.js:315:20)\n(Use `node --trace-warnings ...` to show where the warning was created)\n(node:15924) 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)\n(node:15924) [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.\nC:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\events\\client\\ready.js:9\n              Client.user.setPresence({\n                          ^\n\nTypeError: Cannot read property 'setPresence' of undefined\n    at Timeout.presence [as _onTimeout] (C:\\Users\\x\\Desktop\\Advanced coding\\discord bot\\events\\client\\ready.js:9:27)\n    at listOnTimeout (internal/timers.js:554:17)\n    at processTimers (internal/timers.js:497:7)\n\n
Run Code Online (Sandbox Code Playgroud)\n

主文件:

\n
const Discord = require('discord.js');\nconst client = new Discord.Client({partials: ["MESSAGE", "CHANNEL", "REACTION"]});\nconst fs = require('fs');\n\nclient.commands = new Discord.Collection();\nclient.events = new Discord.Collection();\n\n['command_handler', 'event_handler'].forEach(handler =>{\n   require(`./handlers/${handler}`)(client, Discord);\n})\n\n\n\n\n/* const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));\n\nfor(const file of commandFiles){\n\n   const command = require(`./commands/${file}`);\n   client.commands.set(command.name, command);\n}\n\nclient.once('ready', () => {\n   function presence() {\n      let status = ['<help for help'] //you can add how many you like\n      let rstatus = Math.floor(Math.random() * status.length); //this chooses a status from the ones you typed up\n      client.user.setPresence({\n          status: "online", //you can change to offline, dnd\n          type: "WATCHING",\n          activity: {\n              name: `${status[rstatus]}`, //rhese sets the status                type: "WATCHING" //you can change to listening, streaming, playing (just a quick google search :) )\n          }\n      });\n  }\n  console.log(`Discord Bot ${client.user.tag} is online!`); // the presence needs to be like this, near the ready event\n  setInterval(presence, 30000) //you can reduce/add the time (milliseconds) if you reduce it l3ss then 15 seconds your bot will get ratelimited\n});\n\nclient.on('message', message =>{\n\n   if(!message.content.startsWith(prefix) || message.author.bot) return;  \n   const args = message.content.slice(prefix.length).split(/ +/);\n   const command = args.shift().toLowerCase();\n   message.delete();\n\n   if(command === 'help'){\n   client.commands.get('help').execute(message, args, Discord);\n   }else if(command == 'purge'){\n   client.commands.get('purge').execute(message, args, Discord);\n   }else if(command == 'play'){\n      client.commands.get('play').execute(message, args, Discord);\n   }else if(command == 'congrat'){\n      client.commands.get('congrat').execute(message, args, Discord);\n   }else if(command == 'kick'){\n      client.commands.get('kick').execut(emessage, args, Discord);\n   }else if(command == 'ban'){\n      client.commands.get('ban').execute(message, args, Discord);\n   }else if(command == 'ping'){\n      client.commands.get('ping').execute(message, args, Discord);\n   }\n}); */\n\nclient.login('token')\n
Run Code Online (Sandbox Code Playgroud)\n

任何帮助将不胜感激,谢谢。

\n

Zso*_*ros 6

更新:如果您使用的是 v14 并收到上述错误,那是因为MessageEmbedis now EmbedBuilder。有关更多信息,您可以查看此答案:Discord.js v13 code Breaks when Upgrade to v14

\n

看来您(没有)将Discord变量传递给您的execute方法并混合new Discord.MessageEmbed()and (您正在使用的new MessageEmbed()变量)。尝试全部替换为,它应该可以工作。requirediscord-jsnew Discord.MessageEmbed()newMessageEmbed()

\n
const { MessageEmbed } = require(\'discord.js\');\n\nmodule.exports = {\n  name: \'help\',\n  description: \'Sets up a dynamic help message!\',\n  async execute(message, args, Discord, client) {\n    const purge = \':one:\';\n    const music = \':two:\';\n    const ban = \':three:\';\n\n    const purgeembed = new MessageEmbed()\n\n      .setColor(\'RANDOM\')\n      .setTitle(\'purge info\')\n      .setDescription(\n        \'this is a embed for more info about the purge command and how to use it\',\n      )\n      .addFields(\n        {\n          name: \' \xe2\x80\x8d \',\n          value:\n            \'`this command allows a user to purge (delete) messages that are less than 14 days old`\',\n          inline: true,\n        },\n        { name: \' \xe2\x80\x8d \', value: \'%purge [number of messages < 1000]\' },\n      )\n      .setFooter(\'check bellow for how it works  [unfinished]\');\n\n    const musicembed = new MessageEmbed()\n\n      .setColor(\'RANDOM\')\n      .setTitle(\'music info\')\n      .setDescription(\n        \'this is a embed for more info about the music command and how to use it\',\n      )\n      .addFields(\n        {\n          name: \' \xe2\x80\x8d \',\n          value:\n            \'`this command allows a user to listen to music in a voice channel with alot of options like playing music from a link or by searching for the song through yt there is also a Queuing for music`\',\n          inline: true,\n        },\n        { name: \' \xe2\x80\x8d \', value: \'`assoiciated commands:`\' },\n        { name: \' \xe2\x80\x8d \', value: \'`......`\', inline: true },\n        { name: \' \xe2\x80\x8d \', value: \'`......`\', inline: true },\n        { name: \' \xe2\x80\x8d \', value: \'`......`\', inline: true },\n        { name: \' \xe2\x80\x8d \', value: \'`......`\', inline: true },\n        { name: \' \xe2\x80\x8d \', value: \'`......`\', inline: true },\n        { name: \' \xe2\x80\x8d \', value: \'`%...`\' },\n      )\n      .setFooter(\'check bellow for how it works  [unfinished]\');\n\n    const banembed = new MessageEmbed()\n\n      .setColor(\'RANDOM\')\n      .setTitle(\'ban info\')\n      .setDescription(\n        \'this is a embed for more info about the ban command and how to use it\',\n      )\n      .addFields(\n        {\n          name: \' \xe2\x80\x8d \',\n          value: \'`this command allows a moderator to ban a user`\',\n          inline: true,\n        },\n        {\n          name: \' \xe2\x80\x8d \',\n          value:\n            \'`you can ban a user if you are a moderator but you need to give a reason to the owner first`\',\n        },\n        { name: \' \xe2\x80\x8d \', value: \'`%ban @user`\' },\n      )\n      .setFooter(\'check bellow for how it works  [unfinished]\');\n\n    let embed = new MessageEmbed()\n      .setColor(\'RANDOM\')\n      .setTitle(\'Choose a reaction for more info about a command!\')\n      .setDescription(\n        \'Choosing a reaction will allow you to get more info about a specific command!\\n\\n\' +\n          `${purge} for purge help\\n\\n` +\n          `${music} for music help\\n\\n` +\n          `${ban} for ban help\\n\\n`,\n      );\n\n    message.channel.send(embed).then((embedMessage) => {\n      embedMessage.react(\'1\xef\xb8\x8f\xe2\x83\xa3\');\n      embedMessage.react(\'2\xef\xb8\x8f\xe2\x83\xa3\');\n      embedMessage.react(\'3\xef\xb8\x8f\xe2\x83\xa3\');\n    });\n\n    message.client.on(\'messageReactionAdd\', (reaction, user) => {\n      if (user.id === \'795613081295650837\') {\n      } else {\n        if (reaction.emoji.name === \'1\xef\xb8\x8f\xe2\x83\xa3\') {\n          message.channel.bulkDelete(1);\n\n          setTimeout(function () {\n            message.channel.send(purgeembed);\n          }, 600);\n        }\n        if (reaction.emoji.name === \'2\xef\xb8\x8f\xe2\x83\xa3\') {\n          message.channel.bulkDelete(1);\n\n          setTimeout(function () {\n            message.channel.send(musicembed);\n          }, 600);\n        }\n        if (reaction.emoji.name === \'3\xef\xb8\x8f\xe2\x83\xa3\') {\n          message.channel.bulkDelete(1);\n\n          setTimeout(function () {\n            message.channel.send(banembed);\n          }, 600);\n        } else {\n          return;\n        }\n      }\n    });\n  },\n};   \n
Run Code Online (Sandbox Code Playgroud)\n

其他选项是检查如何调用execute主文件中的方法。如果您Discord作为第三个参数传递,则不需要const { MessageEmbed } = require(\'discord.js\');在顶部使用,而只需new Discord.MessageEmbed()在各处使用 s 。

\n