Discord.js v13升级到v14后代码会出错。

8

我刚刚将我的discord.js从v13更新到v14,但出现了许多错误。

messageinteraction事件相关的错误:

无论是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.

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

通道错误:

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

构建器和嵌入的错误:

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

枚举类型的错误:

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
2个回答

13

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

此版本的v14使用Discord API v10

messageinteraction事件出现错误:

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`);
});

意图相关的错误:

// 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,
  ],
});

若需完整的 GatewayIntentBits 列表,您可以閱讀此答案

interaction 相關錯誤:

某些 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) {}

频道错误:

某些频道类型的守卫已被移除。为了缩小频道范围,你可以将 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) {}

如需完整的ChannelType列表,您可以阅读此答案

此外,还有一些新的类型保护:

channel.isDMBased();
channel.isTextBased();
channel.isVoiceBased();

建造器和嵌入式的错误:

MessageEmbed 已更名为 EmbedBuilder

// v13
const embed = new MessageEmbed();
// v14
const { EmbedBuilder } = require('discord.js');
const embed = new EmbedBuilder();

MessageAttachment 已更名为 AttachmentBuilder,并且不再将名称作为第二个参数,而是接受一个 AttachmentData object

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

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();

枚举错误:

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

// 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)

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

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

有关更多重大更改,请访问discord.js指南:https://discordjs.guide/additional-info/changes-in-v14.html


2
另一个会让人们感到很烦恼的变化是,曾经接受枚举或字符串的参数现在只接受枚举。我认为这很重要,可能应该添加到答案(和问题)中。 - MrMythical
1
谢谢,你是对的。我刚刚添加了一些。不过我不确定哪些是最常见的 :) - Zsolt Meszaros
3
在 discord.js 的支持服务器上,我经常看到一些内容,但这仅适用于 TypeScript。在 ts 中创建一个行动行时,new ActionRowBuilder() 是不够的。你需要 new ActionRowBuilder<ComponentType>()。例如:new ActionRowBuilder<ButtonBuilder>()。这样你只能添加 ButtonBuilders 到组件中,并且它不会允许你将其发送到模态框中(因为按钮不允许在那里)。 - MrMythical
关于交互的类型保护,出于某种原因它们仍然对我有效,这正常吗?我已经仔细检查过了,我的版本是v14。 - IDcLuc
discord.js 的开发者们将它们重新加回来,仅供交互使用。这是因为存在不一致性。 - MrMythical
完整的意图列表可以在此处找到 意图列表 - Amanda

-1

在你的代码中,不要使用:

intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],

尝试使用:

intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],

当我阅读过时的文档时,添加网关意图的方法是记住删除下划线,并将每个单词的首字母大写(我们也不使用Intents.FLAGS.{INTENT},现在已经缩短为GatewayIntentBits.{INTENT})。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接