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

标签 javascript discord discord.js

我刚刚将我的 discord.js 从 v13 更新到 v14,并且有很多错误。
意图错误:

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 的错误s:
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
channel 错误:
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
枚举错误:
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

最佳答案

Discord.js v14 包含许多重大更改。它现在需要使用 Node 16.9 或更高版本,因此请确保您使用 upgrade to the latest LTS version .
此版本的 v14 使用 Discord API v10 .
意图错误:

// 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 , 你可以 read this answer .interaction 的错误年代:
一些交互类型的守卫已被删除。你可以比较interaction.type反对 InteractionType enum反而。
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 错误:
一些 channel 类型的保护已被删除。要缩小 channel ,可以比较channel.type ChannelType enum反而。
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 s, 你可以read this answer .
此外,还有一些新类型的守卫:
channel.isDMBased();
channel.isTextBased();
channel.isVoiceBased();
构建器和嵌入的错误:MessageEmbed已重命名为 EmbedBuilder .
// v13
const embed = new MessageEmbed();
// v14
const { EmbedBuilder } = require('discord.js');
const embed = new EmbedBuilder();
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)
事件类型错误:setPresence activity type in discord.js v14 can only be set to "PLAYING"
有关更多重大更改,您可以访问 discord.js 指南:https://discordjs.guide/additional-info/changes-in-v14.html

关于javascript - 升级到 v14 时 Discord.js v13 代码中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73028854/

相关文章:

javascript - 更改discord.js 中的 channel 权限

javascript - Discord.js:如何获取对带有特定表情符号的消息使用react的所有用户,然后将用户列表设置为变量?

javascript - 递增 1,然后 10,然后 100?

javascript - Discord.js - 如何为数组字符串设置值?

javascript - Angular 形式的日期选择器

heroku - 如何在 Heroku 中安装 FFMPEG?

java - 从 ID 获取用户名 - Discord JDA

python - Discord Bot - "Attribute Error: ' NoneType' 对象没有属性 'strip.'

javascript - 使用jquery在其中按整数拆分字符串

javascript - 在 Javascript 中控制音频平衡 (L/R)