Okay guys so after I posted the first question and got no answer, I almost surfed the whole net, formatted the code but still couldn't find a fix.
I keep getting this error when I register about 13-14 slash commands using the commands.set() method:
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,
This is the source code of the ready.js file where I am registering these commands:
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
)
}
However, the code should work since the 'file' variable has all the required fields: name, description, type, defaultPermission, and options too.
So I outputted the object file and this is one of its iterations:
And the surprising thing is that the commands1 array, although having the same properties as that of arrayOfCommands array, WORKS! So could anyone please help me?
Here is the full error:
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...
CodePudding user response:
What the error is trying to tell you is that you can't have required options after non-required ones. In your requestData you can see all your options arrays. Check out the one for your timeout command:
{
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
}
Can you see that the first option (member) is required, the second one (reason) is not required, and the last one (time) is required again?
It should be ordered like this; first the two required fields, and last the non-required:
{
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
}
Also, the order is wrong for your embedcreate command. Change the order of the options there too:
{
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
}

