Home > Web Front-end > JS Tutorial > Discord.js v13 to v14 Upgrade: How Do I Fix the Common Errors?

Discord.js v13 to v14 Upgrade: How Do I Fix the Common Errors?

Mary-Kate Olsen
Release: 2025-01-04 05:43:40
Original
667 people have browsed it

Discord.js v13 to v14 Upgrade: How Do I Fix the Common Errors?

Discord.js v14: Dealing with Upgrading Errors

When upgrading from Discord.js v13 to v14, you may encounter various errors. Let's address each issue and provide solutions.

Message and Interaction Events

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) => {});
Copy after login

Intents

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];
Copy after login

Interactions

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) {}
Copy after login

Channels

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();
Copy after login

Builders and Embeds

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' });
Copy after login

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();
Copy after login

Enums

v14 requires numbers for enum parameters:

// Wrong
new ButtonBuilder()
  .setCustomId('verification')
  .setStyle('PRIMARY')

// Fixed
new ButtonBuilder()
  .setCustomId('verification')
  .setStyle(ButtonStyle.Primary)
Copy after login

Activity Types

setPresence activity type can only be set to "PLAYING."

Message Content

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template