Reputation:
So recently I tried adding a command handler into my discord bot. All the commands that had Embeds came up with the error "Discord.MessageEmbed is not a constructor".
this only happened when i added the command handler but the commands worked fine without it.
further details: discord.js version [12] node version [14.15.3]
code:
const { MessageEmbed } = require("discord.js");
module.exports = {
name: 'help',
description: "Sets up a dynamic help message!",
async execute(message, args, Discord, client) {
const purge = ':one:';
const music = ':two:';
const ban = ':three:';
const purgeembed = new MessageEmbed()
.setColor('RANDOM')
.setTitle('purge info')
.setDescription("this is a embed for more info about the purge command and how to use it")
.addFields(
{name: ' ', value: "`this command allows a user to purge (delete) messages that are less than 14 days old`", inline: true},
{name: ' ', value: '%purge [number of messages < 1000]'}
)
.setFooter("check bellow for how it works 🔽 [unfinished]");
const musicembed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle('music info')
.setDescription("this is a embed for more info about the music command and how to use it")
.addFields(
{name: ' ', 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},
{name: ' ', value: "`assoiciated commands:`",},
{name: ' ', value: "`......`", inline: true},
{name: ' ', value: "`......`", inline: true},
{name: ' ', value: "`......`", inline: true},
{name: ' ', value: "`......`", inline: true},
{name: ' ', value: "`......`", inline: true},
{name: ' ', value: "`%...`"},
)
.setFooter("check bellow for how it works 🔽 [unfinished]");
const banembed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle('ban info')
.setDescription("this is a embed for more info about the ban command and how to use it")
.addFields(
{name: ' ', value: "`this command allows a moderator to ban a user`", inline: true},
{name: ' ', value: "`you can ban a user if you are a moderator but you need to give a reason to the owner first`"},
{name: ' ', value: "`%ban @user`"},
)
.setFooter("check bellow for how it works 🔽 [unfinished]");
let embed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle('Choose a reaction for more info about a command!')
.setDescription('Choosing a reaction will allow you to get more info about a specific command!\n\n'
+ `${purge} for purge help\n\n`
+ `${music} for music help\n\n`
+ `${ban} for ban help\n\n`);
message.channel.send(embed).then(embedMessage => {
embedMessage.react('1️⃣');
embedMessage.react('2️⃣');
embedMessage.react('3️⃣');
});
message.client.on('messageReactionAdd', (reaction, user) => {
if (user.id === '795613081295650837'){
}else{
if (reaction.emoji.name === '1️⃣') {
message.channel.bulkDelete(1);
setTimeout(function(){
message.channel.send(purgeembed);
}, 600);
}
if (reaction.emoji.name === '2️⃣') {
message.channel.bulkDelete(1);
setTimeout(function(){
message.channel.send(musicembed);
}, 600);
}
if (reaction.emoji.name === '3️⃣') {
message.channel.bulkDelete(1);
setTimeout(function(){
message.channel.send(banembed);
}, 600);
}
else {
return;
}}
});
}
}
full error:
(node:15924) UnhandledPromiseRejectionWarning: TypeError: Discord.MessageEmbed is not a constructor
at Object.execute (C:\Users\x\Desktop\Advanced coding\discord bot\commands\help.js:25:28)
at module.exports (C:\Users\x\Desktop\Advanced coding\discord bot\events\guild\message.js:9:24)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (C:\Users\x\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\x\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\x\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\x\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\x\Desktop\Advanced coding\discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\x\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: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)
(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.
C:\Users\x\Desktop\Advanced coding\discord bot\events\client\ready.js:9
Client.user.setPresence({
^
TypeError: Cannot read property 'setPresence' of undefined
at Timeout.presence [as _onTimeout] (C:\Users\x\Desktop\Advanced coding\discord bot\events\client\ready.js:9:27)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
main file:
const Discord = require('discord.js');
const client = new Discord.Client({partials: ["MESSAGE", "CHANNEL", "REACTION"]});
const fs = require('fs');
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler', 'event_handler'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord);
})
/* const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
function presence() {
let status = ['<help for help'] //you can add how many you like
let rstatus = Math.floor(Math.random() * status.length); //this chooses a status from the ones you typed up
client.user.setPresence({
status: "online", //you can change to offline, dnd
type: "WATCHING",
activity: {
name: `${status[rstatus]}`, //rhese sets the status type: "WATCHING" //you can change to listening, streaming, playing (just a quick google search :) )
}
});
}
console.log(`Discord Bot ${client.user.tag} is online!`); // the presence needs to be like this, near the ready event
setInterval(presence, 30000) //you can reduce/add the time (milliseconds) if you reduce it l3ss then 15 seconds your bot will get ratelimited
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
message.delete();
if(command === 'help'){
client.commands.get('help').execute(message, args, Discord);
}else if(command == 'purge'){
client.commands.get('purge').execute(message, args, Discord);
}else if(command == 'play'){
client.commands.get('play').execute(message, args, Discord);
}else if(command == 'congrat'){
client.commands.get('congrat').execute(message, args, Discord);
}else if(command == 'kick'){
client.commands.get('kick').execut(emessage, args, Discord);
}else if(command == 'ban'){
client.commands.get('ban').execute(message, args, Discord);
}else if(command == 'ping'){
client.commands.get('ping').execute(message, args, Discord);
}
}); */
client.login('token')
any help would be highly appreciated, thanks.
Upvotes: 2
Views: 23919
Reputation: 324
If anybody else landed here for the issue title:
Discord.MessageEmbed is not a constructor
discord.js
changed the class from MessageEmbed
to EmbedBuilder
in version 14.0.1
.
I personally had to do the following changes
OLD CODE
const { Client, Collection, Intents } = require('discord.js')
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING
]
})
NEW CODE
const { Client, Collection, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping
]
})
If you want to see the exact change look at the git diff in my repository
A full list of Intents can be found here Discord developer portal: list-of-intents
If you have more issues related to version change to v14 check out Discord.js v13 code breaks when upgrading to v14 as mentioned in the answer above.
Upvotes: 1
Reputation: 23141
Update: If you're using v14 and receive the error above, it's because MessageEmbed
is now EmbedBuilder
. For more info, you can check out this answer: Discord.js v13 code breaks when upgrading to v14.
It seems you're (not) passing down a Discord
variable to your execute
method and mix new Discord.MessageEmbed()
and new MessageEmbed()
(the one you're require
ing from discord-js
). Try to replace all new Discord.MessageEmbed()
with newMessageEmbed()
and it should work.
const { MessageEmbed } = require('discord.js');
module.exports = {
name: 'help',
description: 'Sets up a dynamic help message!',
async execute(message, args, Discord, client) {
const purge = ':one:';
const music = ':two:';
const ban = ':three:';
const purgeembed = new MessageEmbed()
.setColor('RANDOM')
.setTitle('purge info')
.setDescription(
'this is a embed for more info about the purge command and how to use it',
)
.addFields(
{
name: ' ',
value:
'`this command allows a user to purge (delete) messages that are less than 14 days old`',
inline: true,
},
{ name: ' ', value: '%purge [number of messages < 1000]' },
)
.setFooter('check bellow for how it works 🔽 [unfinished]');
const musicembed = new MessageEmbed()
.setColor('RANDOM')
.setTitle('music info')
.setDescription(
'this is a embed for more info about the music command and how to use it',
)
.addFields(
{
name: ' ',
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,
},
{ name: ' ', value: '`assoiciated commands:`' },
{ name: ' ', value: '`......`', inline: true },
{ name: ' ', value: '`......`', inline: true },
{ name: ' ', value: '`......`', inline: true },
{ name: ' ', value: '`......`', inline: true },
{ name: ' ', value: '`......`', inline: true },
{ name: ' ', value: '`%...`' },
)
.setFooter('check bellow for how it works 🔽 [unfinished]');
const banembed = new MessageEmbed()
.setColor('RANDOM')
.setTitle('ban info')
.setDescription(
'this is a embed for more info about the ban command and how to use it',
)
.addFields(
{
name: ' ',
value: '`this command allows a moderator to ban a user`',
inline: true,
},
{
name: ' ',
value:
'`you can ban a user if you are a moderator but you need to give a reason to the owner first`',
},
{ name: ' ', value: '`%ban @user`' },
)
.setFooter('check bellow for how it works 🔽 [unfinished]');
let embed = new MessageEmbed()
.setColor('RANDOM')
.setTitle('Choose a reaction for more info about a command!')
.setDescription(
'Choosing a reaction will allow you to get more info about a specific command!\n\n' +
`${purge} for purge help\n\n` +
`${music} for music help\n\n` +
`${ban} for ban help\n\n`,
);
message.channel.send(embed).then((embedMessage) => {
embedMessage.react('1️⃣');
embedMessage.react('2️⃣');
embedMessage.react('3️⃣');
});
message.client.on('messageReactionAdd', (reaction, user) => {
if (user.id === '795613081295650837') {
} else {
if (reaction.emoji.name === '1️⃣') {
message.channel.bulkDelete(1);
setTimeout(function () {
message.channel.send(purgeembed);
}, 600);
}
if (reaction.emoji.name === '2️⃣') {
message.channel.bulkDelete(1);
setTimeout(function () {
message.channel.send(musicembed);
}, 600);
}
if (reaction.emoji.name === '3️⃣') {
message.channel.bulkDelete(1);
setTimeout(function () {
message.channel.send(banembed);
}, 600);
} else {
return;
}
}
});
},
};
Other option is to check how you call the execute
method in the main file. If you pass Discord
as the third argument, you don't need to use const { MessageEmbed } = require('discord.js');
on top and only use new Discord.MessageEmbed()
s everywhere.
Upvotes: 6