从 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();
v14 需要枚举的数字参数:
// Wrong new ButtonBuilder() .setCustomId('verification') .setStyle('PRIMARY') // Fixed new ButtonBuilder() .setCustomId('verification') .setStyle(ButtonStyle.Primary)
setPresence 活动类型只能设置为“PLAYING”。
如果 message.content 为空,确保 GatewayIntentBits.MessageContent 包含在您的意图数组中。
请参阅Discord.js 指南,提供完整的重大更改列表:https://discordjs.guide/additional-info/changes-in-v14.html
以上是Discord.js v13 到 v14 升级:如何修复常见错误?的详细内容。更多信息请关注PHP中文网其他相关文章!