升级到 v14 时 Discord.js v13 代码中断

Zso*_*ros 8 javascript node.js discord discord.js

我刚刚将 Discord.js 从 v13 更新到 v14,但有很多错误。

message和事件的错误interaction

message事件也都没有发生interaction

意图错误:

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.
Run Code Online (Sandbox Code Playgroud)

s的错误interaction

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
Run Code Online (Sandbox Code Playgroud)

通道错误:

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
Run Code Online (Sandbox Code Playgroud)

构建器和嵌入的错误:

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
Run Code Online (Sandbox Code Playgroud)

枚举错误:

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
Run Code Online (Sandbox Code Playgroud)

Zso*_*ros 13

Discord.js v14 包含许多重大更改。现在需要 Node 16.9 或更高版本才能使用,因此请确保升级到最新的 LTS 版本

此版本的 v14 使用Discord API v10

message和事件的错误interaction

和事件现已删除messageinteraction您可以使用messageCreateinteractionCreate事件来代替。

// 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`);
});
Run Code Online (Sandbox Code Playgroud)

意图错误:

// 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,
  ],
});
Run Code Online (Sandbox Code Playgroud)

有关完整列表GatewayIntentBits,您可以阅读此答案

s的错误interaction

一些交互类型的防护已被删除。您可以interaction.typeInteractionType枚举进行比较。

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) {}
Run Code Online (Sandbox Code Playgroud)

通道错误:

一些通道类型的防护已被移除。要缩小渠道,您可以channel.typeChannelType枚举进行比较。

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) {}
Run Code Online (Sandbox Code Playgroud)

有关 s 的完整列表ChannelType,您可以阅读此答案

此外,还有一些新型守卫:

channel.isDMBased();
channel.isTextBased();
channel.isVoiceBased();
Run Code Online (Sandbox Code Playgroud)

构建器和嵌入的错误:

MessageEmbed已更名为EmbedBuilder.

// v13
const embed = new MessageEmbed();
// v14
const { EmbedBuilder } = require('discord.js');
const embed = new EmbedBuilder();
Run Code Online (Sandbox Code Playgroud)

MessageAttachment已重命名为AttachmentBuilder,并且不再将名称作为第二个参数,而是接受AttachmentData 对象

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

MessageComponents已更名;它们不再有Message前缀,现在有Builder后缀。

// 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();
Run Code Online (Sandbox Code Playgroud)

枚举错误:

任何过去接受字符串或数字类型作为枚举参数的区域现在只接受数字。

// 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)
Run Code Online (Sandbox Code Playgroud)

活动类型错误:discord.js v14 中的 setPresence 活动类型只能设置为“PLAYING”

如果message.content没有任何值,请将 GatewayIntentBits.MessageContent 枚举添加到您的意图数组中

如需更多重大更改,您可以访问discord.js指南:https ://discordjs.guide/additional-info/changes-in-v14.html

  • 我还在discord.js 支持服务器中看到了很多东西,但这只适用于 TypeScript。在 ts 中创建操作行时,“new ActionRowBuilder()”是不够的。您需要“new ActionRowBuilder<ComponentType>()”。示例:“new ActionRowBuilder<ButtonBuilder>()”。这样,您只能将“ButtonBuilder”添加到组件中,并且不允许您将其发送到模式(因为那里不允许使用按钮) (3认同)
  • 另一个让人很烦恼的变化是,过去接受枚举或字符串的参数现在只接受枚举。我想说这很重要,可能应该添加到答案中(也可能是问题) (2认同)