Home >Web Front-end >JS Tutorial >Discord.js v13 to v14 Upgrade: How Do I Fix the Common Errors?
When upgrading from Discord.js v13 to v14, you may encounter various errors. Let's address each issue and provide solutions.
The message and interaction events have been removed. Instead, use messageCreate and interactionCreate:
// v13 client.on('message', (message) => {}); client.on('interaction', (interaction) => {}); // v14 client.on('messageCreate', (message) => {}); client.on('interactionCreate', (interaction) => {});
v14 uses the new GatewayIntentBits enum. Update your code accordingly:
// v13 const intents = [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]; // v14 const intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages];
Some interaction type guards have been removed. Compare interaction.type against the InteractionType enum:
// v13 if (interaction.isCommand()) {} if (interaction.isAutocomplete()) {} if (interaction.isMessageComponent()) {} if (interaction.isModalSubmit()) {} // v14 if (interaction.type === InteractionType.ApplicationCommand) {} if (interaction.type === InteractionType.ApplicationCommandAutocomplete) {} if (interaction.type === InteractionType.MessageComponent) {} if (interaction.type === InteractionType.ModalSubmit) {}
Type guards for channels have been removed. Use the ChannelType enum to narrow down channels:
// v13 if (message.channel.isText()) {} if (message.channel.isVoice()) {} if (message.channel.isDM()) {} if (message.channel.isCategory()) {} // v14 if (channel.type === ChannelType.GuildText) {} if (channel.type === ChannelType.GuildVoice) {} if (channel.type === ChannelType.DM) {} if (channel.type === ChannelType.GuildCategory) {} // New type guards channel.isDMBased(); channel.isTextBased(); channel.isVoiceBased();
MessageEmbed has been renamed to EmbedBuilder. MessageAttachment has been renamed to AttachmentBuilder and uses an AttachmentData object:
// v13 const embed = new MessageEmbed(); const attachment = new MessageAttachment(buffer, 'image.png'); // v14 const embed = new EmbedBuilder(); const attachment = new AttachmentBuilder(buffer, { name: 'image.png' });
MessageComponent builders have been renamed and have a Builder suffix:
// v13 const button = new MessageButton(); const actionRow = new MessageActionRow(); const selectMenu = new MessageSelectMenu(); const textInput = new TextInputComponent(); // v14 const button = new ButtonBuilder(); const actionRow = new ActionRowBuilder(); const selectMenu = new SelectMenuBuilder(); const textInput = new TextInputBuilder();
v14 requires numbers for enum parameters:
// Wrong new ButtonBuilder() .setCustomId('verification') .setStyle('PRIMARY') // Fixed new ButtonBuilder() .setCustomId('verification') .setStyle(ButtonStyle.Primary)
setPresence activity type can only be set to "PLAYING."
If message.content is empty, ensure GatewayIntentBits.MessageContent is included in your intent array.
Refer to the Discord.js guide for a comprehensive list of breaking changes: https://discordjs.guide/additional-info/changes-in-v14.html
The above is the detailed content of Discord.js v13 to v14 Upgrade: How Do I Fix the Common Errors?. For more information, please follow other related articles on the PHP Chinese website!