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
Mary-Kate OlsenOriginal
2025-01-04 05:43:40663browse

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

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];

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) {}

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();

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

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();

Enums

v14 requires numbers for enum parameters:

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

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

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!

Statement:
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
Previous article:React Lifecycle in inutesNext article:React Lifecycle in inutes