從 Discord.js v13 升級到 v14 時,您可能會遇到各種錯誤。讓我們解決每個問題並提供解決方案。
訊息和互動事件已被刪除。相反,請使用 messageCreate 和 interactionCreate:
// v13 client.on('message', (message) => {}); client.on('interaction', (interaction) => {}); // v14 client.on('messageCreate', (message) => {}); client.on('interactionCreate', (interaction) => {});
v14 使用新的 GatewayIntentBits 枚舉。相應地更新您的程式碼:
// v13 const intents = [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]; // v14 const intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages];
一些互動類型防護已被刪除。將 interaction.type 與 InteractionType 枚舉進行比較:
// 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) {}
頻道的類型保護已被刪除。使用 ChannelType 枚舉縮小通道範圍:
// 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();
MessageEmbed 已重新命名為 EmbedBuilder。 MessageAttachment 已重新命名為 AttachmentBuilder 並使用 AttachmentData 物件:
// 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 建構器已重新命名並具有 Builder 後綴:
// 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();
// Wrong new ButtonBuilder() .setCustomId('verification') .setStyle('PRIMARY') // Fixed new ButtonBuilder() .setCustomId('verification') .setStyle(ButtonStyle.Primary)
訊息內容
請參閱Discord.js 指南,提供完整的重大更改清單:https://discordjs.guide/additional-info/changes-in-v14.html
以上是Discord.js v13 到 v14 升級:如何修復常見錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!