Home >Web Front-end >JS Tutorial >How to Successfully Migrate My Discord.js Bot from v11 to v12?
Discord.js v12 Migration Guide: Code Migration from v11 to v12
Migrating to Discord.js v12 can cause errors due to breaking changes. Here's a guide to address these issues and ensure a smooth migration.
Managers
Previously cached properties like client.users and Guild#roles are now managers. Access the cached collection through 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 uses functions for .find and .findKey:
// v11: collection.find('property', 'value') collection.find(item => item.property === 'value');
Methods like .exists, .deleteAll, and .filterArray have been removed:
// v11: collection.exists('property', 'value') collection.some(item => item.property === 'value'); // v11: collection.deleteAll() Promise.all(collection.map(item => item.delete()));
. now runs a function on the collection itself, not its items:
// v11: collection.tap(item => console.log(item)) collection.each(item => console.log(item)); // New .tap behaviour: collection.tap(coll => console.log(`${coll.size} items`));
RichEmbed/MessageEmbed
The RichEmbed class has been replaced by MessageEmbed, which is now used for all embeds:
const {MessageEmbed} = require('discord.js'); const embed = new MessageEmbed();
The addBlankField method has been removed. To add a blank field, use:
embed.addField('\u200B', '\u200B');
Voice
All play methods are now unified under a single play method:
const dispatcher = connection.play('./music.mp3');
Client#createVoiceBroadcast has moved to the ClientVoiceManager:
const broadcast = client.voice.createVoiceBroadcast();
StreamDispatcher extends Node.js' stream.Writable, so use dispatcher.destroy() instead of dispatcher.end().
Image URLs
Properties like User#displayAvatarURL and Guild#iconURL are now methods, returning an ImageURLOptions object for customization:
const avatar = user.displayAvatarURL(); const icon = mesage.guild.iconURL();
Further Information
For more details on the breaking changes, refer to the [updating guide](https://discordjs.guide/additional-info/changes/v12.html) and [changelog](https://github.com/discordjs/discord.js/blob/main/CHANGELOG.md#v1200). The [Discord.js documentation](https://discord.js.org/#/docs) also provides valuable assistance.
The above is the detailed content of How to Successfully Migrate My Discord.js Bot from v11 to v12?. For more information, please follow other related articles on the PHP Chinese website!