[英]DiscordAPIError: Invalid Form Body 3.options[1].type: This field is required
[英]DiscordAPIError: Invalid Form Body options[2]: Required options must be placed before non-required options
好的,伙計們,在我發布第一個問題但沒有得到答案后,我幾乎瀏覽了整個網絡,格式化了代碼,但仍然找不到修復程序。
當我使用commands.set()
方法注冊大約 13-14 個斜杠命令時,我不斷收到此錯誤:
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Invalid Form Body
options[2]: Required options must be placed before non-required options
at RequestHandler.execute (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async GuildApplicationCommandManager.set (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\managers\ApplicationCommandManager.js:146:18) {
method: 'put',
path: '/applications/922152514420363285/guilds/933052184432636014/commands',
code: 50035,
httpStatus: 400,
這是我在其中注冊這些命令的ready.js
文件的源代碼:
const { Constants } = require("discord.js")
const {promisify} = require('util')
const {glob} = require('glob')
const globPromise = promisify(glob);
//const slashregister = require('../../slashregistering')
module.exports = async(Discord , client) => {
console.log('Bot is ready')
const guild = client.guilds.cache.get('933052184432636014')
const commands = guild.commands
var arrayofCommands = new Array()
var commands1 = [
{
name : 'add',
description : 'Adds two numbers.',
options : [{
name : 'num1',
description : 'Number1',
required : true,
type : Constants.ApplicationCommandOptionTypes.INTEGER
},
{
name : 'num2',
description : 'Number2',
required : true,
type : Constants.ApplicationCommandOptionTypes.INTEGER
}
]
}
]
const slashcommands = await globPromise(`${process.cwd()}/slashcommands/**/*.js`)
slashcommands.map((file) => {
const value = require(file)
//delete value.execute
console.log(value)
arrayofCommands.push(value)
})
commands.set(
arrayofCommands
)
}
但是,代碼應該可以工作,因為 'file' 變量具有所有必需的字段: name
、 description
、 type
、 defaultPermission
和options
。
所以我輸出了 object file
,這是它的迭代之一:
令人驚訝的是, commands1
數組雖然具有與arrayOfCommands
數組相同的屬性,但可以工作嗎? 那么有人可以幫助我嗎?
這是完整的錯誤:
Server is online!
Bot is ready
C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Invalid Form Body
options[2]: Required options must be placed before non-required options
at RequestHandler.execute (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async GuildApplicationCommandManager.set (C:\Users\Mihir\Desktop\FunMod\node_modules\discord.js\src\managers\ApplicationCommandManager.js:146:18) {
method: 'put',
path: '/applications/922152514420363285/guilds/933052184432636014/commands',
code: 50035,
httpStatus: 400,
requestData: {
json: [
{
name: 'addrole',
description: 'Gives the member a role.',
type: 1,
options: [
{
type: 8,
name: 'role',
description: 'Role Name',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 6,
name: 'member',
description: 'Member ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'ban',
description: 'Ban the specified member.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'reason',
description: 'Reason ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'changenick',
description: 'Changes the nickname of a server member.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'nickname',
description: 'Nickname',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'kick',
description: 'Kicks the specified member.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'reason',
description: 'Reason',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'lock',
description: 'Locks the specified channel for the specified role.',
type: 1,
options: [
{
type: 7,
name: 'channel',
description: 'Channel',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 8,
name: 'role',
description: 'Role ',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'lockdown',
description: 'Enforces a complete lockdown on the server.',
type: 1,
options: [
{
type: 3,
name: 'toggle',
description: 'on/off',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 8,
name: 'role',
description: 'Role ',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'removerole',
description: 'Removes role from a member.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 8,
name: 'role',
description: 'Role ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'timeout',
description: 'Gives a member a timeout.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'reason',
description: 'Reason',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'time',
description: 'Duration',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'unlock',
description: 'Locks the specified channel for the specified role.',
type: 1,
options: [
{
type: 7,
name: 'channel',
description: 'Channel',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 8,
name: 'role',
description: 'Role',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'embedcreate',
description: 'Creates an embed.',
type: 1,
options: [
{
type: 7,
name: 'channel',
description: 'Channel ',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'color',
description: 'Hex-Code',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'title',
description: 'Title',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'ping',
description: 'Responds with a pong!',
type: 1,
options: undefined,
default_permission: false
},
{
name: 'setactivity',
description: 'Sets the Activity of the bot.',
type: 1,
options: [
{
type: 3,
name: 'activity',
description: 'Activity',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'type',
description: 'Type ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'setavatar',
description: "Sets the bot's avatar.",
type: 1,
options: [
{
type: 3,
name: 'link',
description: 'Image Link',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
},
{
name: 'setstatus',
description: 'Sets the status of the bot.',
type: 1,
options: [
{
type: 3,
name: 'status',
description: 'Status ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
}
],
files: []
}
}
[nodemon] app crashed - waiting for file changes before starting...
該錯誤試圖告訴您的是,在非必需選項之后您不能擁有必需的選項。 在您的requestData
,您可以看到所有options
arrays。 查看您的timeout
命令:
{
name: 'timeout',
description: 'Gives a member a timeout.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member ',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'reason',
description: 'Reason',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'time',
description: 'Duration',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
}
你能看到第一個選項( member
)是必需的,第二個( reason
)不是必需的,最后一個( time
)又是必需的嗎?
應該這樣排序; 首先是兩個必填字段,最后是非必填字段:
{
name: 'timeout',
description: 'Gives a member a timeout.',
type: 1,
options: [
{
type: 6,
name: 'member',
description: 'Member',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'time',
description: 'Duration',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'reason',
description: 'Reason',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
}
此外,您的embedcreate
命令的順序是錯誤的。 也改變那里的選項順序:
{
name: 'embedcreate',
description: 'Creates an embed.',
type: 1,
options: [
{
type: 3,
name: 'color',
description: 'Hex-Code',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 3,
name: 'title',
description: 'Title',
required: true,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
},
{
type: 7,
name: 'channel',
description: 'Channel ',
required: false,
autocomplete: undefined,
choices: undefined,
options: undefined,
channel_types: undefined,
min_value: undefined,
max_value: undefined
}
],
default_permission: false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.