Migrating from Discord.js v11 to v12
Upgrading to Discord.js v12 may break existing code from v11. This article will highlight some of the most common breaking changes and provide solutions.
Managers
Previously cached collections like Client#users and Guild#roles are now managers. To access the cached collection, use the cache property:
const user = client.users.cache.get('123456789012345678'); const role = message.guild.roles.cache.find(r => r.name === 'Admin');
Methods like GuildMember#addRole, Guild#createChannel, and TextBasedChannel#fetchMessages have moved to their respective managers:
await message.member.roles.add(role); await message.guild.channels.create('welcome'); const messages = await message.channel.messages.fetch();
Collection
The Collection class now only accepts functions, not property keys and values, for .find and .findKey:
// v11: collection.find('property', 'value') collection.find(item => item.property === 'value');
Additional changes to Collection include the removal of .exists, .deleteAll, .filterArray, and .findAll.
RichEmbed/MessageEmbed
The RichEmbed class has been deprecated. Use the MessageEmbed class instead:
const {MessageEmbed} = require('discord.js'); const embed = new MessageEmbed();
The addBlankField method has also been removed. To add a blank field, use:
embed.addField('\u200B', '\u200B');
Voice
All VoiceConnection/VoiceBroadcast#play*** methods have been unified under a single play method:
const dispatcher = connection.play('./music.mp3');
Client#createVoiceBroadcast has been moved to the ClientVoiceManager:
const broadcast = client.voice.createVoiceBroadcast();
Image URLs
Properties like User#displayAvatarURL and Guild#iconURL are now methods that can take an ImageURLOptions object to customize size and format:
const avatar = user.displayAvatarURL(); const icon = message.guild.iconURL();
Additional Resources
For a more comprehensive overview of the breaking changes introduced in Discord.js v12, refer to the updating guide, changelog, and documentation.
The above is the detailed content of Discord.js v11 to v12 Migration: What are the Key Breaking Changes and How Do I Fix Them?. For more information, please follow other related articles on the PHP Chinese website!