Search code examples
javascriptnode.jsdiscorddiscord.js

Discord.js v13 code breaks when upgrading to v14


I've just updated my discord.js from v13 to v14 and there are many errors.

Errors with message and interaction events:

Neither message nor interaction events fire.

Errors with intents:

const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
  ],
});
//     Intents.FLAGS.GUILDS,
//            ^
//
// TypeError: Cannot read properties of undefined (reading 'FLAGS')

const client = new Client({
  intents: ['GUILDS', 'GUILD_MEMBERS', 'GUILD_MESSAGES'],
});
//    throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
//
// RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.

Errors with interactions:

if (interaction.isCommand()) {}
// TypeError: interaction.isCommand is not a function

if (interaction.isAutocomplete()) {}
// TypeError: interaction.isAutocomplete is not a function

if (interaction.isMessageComponent()) {}
// TypeError: interaction.isMessageComponent is not a function

if (interaction.isModalSubmit()) {}
// TypeError: interaction.isModalSubmit is not a function

Errors with channels:

if (message.channel.isText()) {}
// TypeError: channel.isText is not a function

if (message.channel.isVoice()) {}
// TypeError: channel.isVoice is not a function

if (message.channel.isDM()) {}
// TypeError: channel.isDM is not a function

if (message.channel.isCategory()) {}
// TypeError: channel.isCategory is not a function

Errors with builders and embeds:

const embed = new MessageEmbed();
//  const embed = new MessageEmbed();
//                ^
//
// TypeError: MessageEmbed is not a constructor

const button = new MessageButton();
//  const button = new MessageButton();
//                 ^
//
// TypeError: MessageButton is not a constructor

const actionRow = new MessageActionRow();
//  const actionRow = new MessageActionRow();
//                    ^
//
// TypeError: MessageActionRow is not a constructor

const selectMenu = new MessageSelectMenu();
//  const selectMenu = new MessageSelectMenu();
//                     ^
//
// TypeError: MessageSelectMenu is not a constructor

const textInput = new TextInputComponent();
//  const textInput = new TextInputComponent();
//                    ^
//
// TypeError: TextInputComponent is not a constructor

const modal = new Modal();
//  const modal = new Modal();
//                ^
//
// TypeError: Modal is not a constructor

const attachment = new MessageAttachment();
//  const attachment = new MessageAttachment();
//                     ^
//
// TypeError: MessageAttachment is not a constructor

Errors with enums:

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

// UnknownEnumValueError: Expected the value to be one of the following enum values:
//     at NativeEnumValidator.handle

new TextInputBuilder()
  .setCustomId('verification')
  .setStyle('SHORT')

// UnknownEnumValueError: Expected the value to be one of the following enum values:
//     at NativeEnumValidator.handle

Solution

  • Discord.js v14 includes many breaking changes. It now requires Node 16.9 or higher to use, so make sure you upgrade to the latest LTS version.

    This version of v14 uses the Discord API v10.

    Errors with message and interaction events:

    The message and interaction events are now removed. You can use the messageCreate and interactionCreate events instead.

    // v13
    client.on('message', (message) => {
      console.log(`👎 doesn't fire`);
    });
    client.on('interaction', (interaction) => {
      console.log(`👎 doesn't fire`);
    });
    
    // v14
    client.on('messageCreate', (message) => {
      console.log(`👍 works as expected`);
    });
    client.on('interactionCreate', (interaction) => {
      console.log(`👍 works as expected`);
    });
    

    Errors with intents:

    // v13
    const client = new Client({
      intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
      ],
    });
    
    // OR
    const client = new Client({
      intents: ['GUILDS', 'GUILD_MESSAGES'],
    });
    
    // v14
    const { Client, GatewayIntentBits } = require('discord.js');
    const client = new Client({
      intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
      ],
    });
    

    For a full list of GatewayIntentBits, you can read this answer.

    Errors with interactions:

    Some interaction type guards have been removed. You can compare interaction.type against the InteractionType enum instead.

    const { InteractionType } = require('discord.js');
    
    // v13
    if (interaction.isCommand()) {}
    // v14
    if (interaction.type === InteractionType.ApplicationCommand) {}
    
    // v13
    if (interaction.isAutocomplete()) {}
    // v14
    if (interaction.type === InteractionType.ApplicationCommandAutocomplete) {}
    
    // v13
    if (interaction.isMessageComponent()) {}
    // v14
    if (interaction.type === InteractionType.MessageComponent) {}
    
    // v13
    if (interaction.isModalSubmit()) {}
    // v14
    if (interaction.type === InteractionType.ModalSubmit) {}
    

    Errors with channels:

    Some channel type guards have been removed. To narrow channels, you can compare channel.type to a ChannelType enum instead.

    const { ChannelType } = require('discord.js');
    // v13
    if (message.channel.isText()) {}
    // v14
    if (channel.type === ChannelType.GuildText) {}
    
    // v13
    if (message.channel.isVoice()) {}
    // v14
    if (channel.type === ChannelType.GuildVoice) {}
    
    // v13
    if (message.channel.isDM()) {}
    // v14
    if (channel.type === ChannelType.DM) {}
    
    // v13
    if (message.channel.isCategory()) {}
    // v14
    if (channel.type === ChannelType.GuildCategory) {}
    

    For a full list of ChannelTypes, you can read this answer.

    Also, there are some new type guards:

    channel.isDMBased();
    channel.isTextBased();
    channel.isVoiceBased();
    

    Errors with builders and embeds:

    MessageEmbed has been renamed to EmbedBuilder.

    // v13
    const embed = new MessageEmbed();
    // v14
    const { EmbedBuilder } = require('discord.js');
    const embed = new EmbedBuilder();
    

    MessageAttachment has been renamed to AttachmentBuilder and instead of taking the name as the second parameter, it accepts an AttachmentData object.

    // v13
    const attachment = new MessageAttachment(buffer, 'image.png');
    // v14 
    const { AttachmentBuilder } = require('discord.js');
    const attachment = new AttachmentBuilder(buffer, { name: 'image.png' });
    

    MessageComponents have been renamed; they no longer have the Message prefix and now have a Builder suffix.

    // v13
    const button = new MessageButton();
    // v14 
    const { ButtonBuilder } = require('discord.js');
    const button = new ButtonBuilder();
    
    // v13
    const actionRow = new MessageActionRow();
    // v14 
    const { ActionRowBuilder } = require('discord.js');
    const actionRow = new ActionRowBuilder();
    
    // v13
    const selectMenu = new MessageSelectMenu();
    // v14
    const { SelectMenuBuilder } = require('discord.js');
    const selectMenu = new SelectMenuBuilder();
    
    // v13
    const textInput = new TextInputComponent();
    // v14
    const { TextInputBuilder } = require('discord.js');
    const textInput = new TextInputBuilder();
    

    Errors with enums:

    Any areas that used to accept a string or number type for an enum parameter will now only accept exclusively numbers.

    // Wrong
    new ButtonBuilder()
      .setCustomId('verification')
      .setStyle('PRIMARY')
    
    // Fixed
    const { ButtonStyle } = require('discord.js');
    new ButtonBuilder()
      .setCustomId('verification')
      .setStyle(ButtonStyle.Primary)
    
    // Wrong
    new TextInputBuilder()
      .setCustomId('verification')
      .setStyle('SHORT')
    
    // Fixed
    const { TextInputStyle } = require('discord.js');
    new TextInputBuilder()
      .setCustomId('verification')
      .setStyle(TextInputStyle.Short)
    

    Errors with activity types: setPresence activity type in discord.js v14 can only be set to "PLAYING"

    If message.content doesn't have any value, add the GatewayIntentBits.MessageContent enum to your intents array

    For more breaking changes, you can visit the discord.js guide: https://discordjs.guide/additional-info/changes-in-v14.html