Discord.js v14升级后DiscordAPIError[50035]错误排查
错误信息
UNHANDLED_EXCEPTION: DiscordAPIError[50035]: Invalid Form Body
3.type[ENUM_TYPE_COERCE]: Value "6" is not a valid enum value.
Promise {DiscordAPIError[50035]: Invalid Form Body
3.type[ENUM_TYPE_COERCE]: Value "6" is not a valid enum value.
at SequentialHandler.runRequest (/home/runner/AIUC/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:667:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.queueRequest (/home/runner/AIUC/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:464:14)
at async REST.request (/home/runner/AIUC/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:910:22) {
requestBody: { files: undefined, json: [Array] },
rawError: { code: 50035, errors: [Object], message: 'Invalid Form Body' },
code: 50035,
status: 400,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/1069961347166646272/guilds/1069355488258560122/commands'
}
}
项目依赖配置(package.json)
{ "name": "nodejs", "version": "1.0.0", "main": "index.js", "scripts": { "start": "node index.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@discordjs/opus": "^0.8.0", "@discordjs/rest": "^1.5.1-dev.1676247033-0e4224b.0", "@discordjs/voice": "^0.14.0", "@distube/soundcloud": "^1.3.0", "@distube/spotify": "^1.5.1", "@distube/yt-dlp": "^1.1.3", "chalk": "4.1.2", "dayjs": "1.10.7", "discord.js": "^14.7.1", "distube": "^4.0.4", "express": "^4.18.2", "ffmpeg-static": "4.4.0", "glob": "7.2.0", "libsodium-wrappers": "^0.7.10", "lyrics-finder": "^21.7.0", "moment": "^2.29.4", "moment-duration-format": "^2.3.2", "mongoose": "^6.9.1", "ms": "2.1.3", "node-fetch": "^3.2.6", "opusscript": "^0.0.8", "pm2": "^5.2.2", "systeminformation": "^5.17.9", "ytdl-core": "^4.11.2" }, "description": "" }
相关代码文件
index.js
const { Client, Collection, GatewayIntentBits } = require('discord.js'); const { DisTube } = require('distube'); const { SpotifyPlugin } = require('@distube/spotify'); const { SoundCloudPlugin } = require('@distube/soundcloud'); const { YtDlpPlugin } = require('@distube/yt-dlp'); const client = new Client( { intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.MessageContent ] } ); const mongoose = require('mongoose'); const Logger = require('./utils/Logger'); ['commands', 'buttons', 'selects'].forEach(x => client[x] = new Collection()); ['CommandUtil', 'EventUtil', 'ButtonUtil', 'SelectUtil'].forEach(handler => { require(`./utils/handlers/${handler}`)(client) }); require('./utils/Functions')(client); mongoose.set('strictQuery', true); process.on('exit', code => { Logger.client(`Process shutdown with the following code: ${code}`) }); process.on('uncaughtException', (err, origin) => { Logger.error(`UNCAUGHT_EXCEPTION: ${err}`); console.error(`Origin: ${origin}`); }); process.on('unhandledRejection', (reason, promise) => { Logger.warn(`UNHANDLED_EXCEPTION: ${reason}`); console.log(promise); }); process.on('warning', (...args) => Logger.warn(...args)); mongoose.connect(process.env.DATABASE_URI, { autoIndex: false, maxPoolSize: 10, serverSelectionTimeoutMS: 5000, socketTimeoutMS: 45000, family: 4 }).then(() => { Logger.client('Connected to the database!') }) .catch(err => { Logger.error(err) }); client.distube = new DisTube(client, { leaveOnStop: false, emitNewSongOnly: true, emitAddSongWhenCreatingQueue: false, emitAddListWhenCreatingQueue: false, plugins: [ new SpotifyPlugin({ emitEventsAfterFetching: true }), new SoundCloudPlugin(), new YtDlpPlugin() ] }); client.login(process.env.DISCORD_TOKEN);
resident-evil.js(上下文命令文件)
const { EmbedBuilder, ApplicationCommandOptionType } = require('discord.js'); const { ResidentEvilRPGCharacter } = require('../../models'); module.exports = { name: 'resident-evil', category: 'context', permissions: ['SEND_MESSAGES'], ownerOnly: false, usage: 'Use context menu!', examples: ['Use context menu!'], type: ApplicationCommandOptionType.User, async runInteraction(client, interaction) { const member = await interaction.guild.members.fetch(interaction.targetId); const residentEvilData = await ResidentEvilRPGCharacter.findOne({ id: interaction.guild.id, member: member.id }); if (!residentEvilData) return interaction.reply({ content: 'You have no character in Resident Evil RPG' }) const embed = new EmbedBuilder() .setAuthor({ name: `${member.user.tag} (${member.id})` }) .setColor('#0047AB') .setImage(`${residentEvilData['picture']}`) .setThumbnail(member.user.displayAvatarURL()) .setDescription(`**+ Lastname**\n${residentEvilData['lastname']}\n\n**+ Firstname**\n${residentEvilData['firstname']}\n\n**+ Surname**\n${residentEvilData['surname']}\n\n**+ Age**\n${residentEvilData['age']}\n\n**+ Sexe**\n${residentEvilData['sexe']}\n\n**+ Family**\n${residentEvilData['family']}\n\n**+ Nationality**\n${residentEvilData['nationality']}\n\n**+ Sexual Orientation**\n${residentEvilData['sexualorientation']}\n\n**+ Political Party**\n${residentEvilData['politicalparty']}\n\n**+ Religion**\n${residentEvilData['religion']}\n\n**+ History**\n${residentEvilData['history']}\n\n${residentEvilData['other']}`); interaction.reply({ embeds: [embed] }); } }
问题说明
将Discord机器人从Discord.js v13升级到v14后,启动时触发上述50035错误,尝试添加register.js初始化REST API后问题依旧。最终定位错误来源为resident-evil.js上下文命令文件。
内容的提问来源于stack exchange,提问作者yotlan le crom

