#Cannot rest.put my application guild commands?

1 messages · Page 1 of 1 (latest)

eternal solar
#

I am trying to deploy my bot commands when I'm getting the Invalid Form Body error?

// Send the commands to the specified guild to be deployed
        console.log(cmdsData);
        rest.put(
            Routes.applicationGuildCommands(
                process.env.CLIENT_ACTIVE_ID as string,
                process.env.GUILD_ACTIVE_ID as string
            ),
            { body: cmdsData }
        ).then(() => {
            UtilHelpers.success("Successfully deployed commands.");
        }).catch((err: any) => {
            UtilHelpers.error(err);
            reject(new Error("Unable to deploy commands."));
        });

The cmdsData is an array of the RESTPostAPIApplicationGuildCommandsJSONBody type

This is what I push to the cmdsData array

function RollOutCommand(cmdsData: RESTPostAPIApplicationGuildCommandsJSONBody[], cmd: Command) {
    try {
        cmdsData.push(cmd.data.toJSON());
        UtilHelpers.success(`Successfully rolled-out command with name: '${cmd.data.name}'`);
    } catch (reason: any) {
        UtilHelpers.error(reason);
        UtilHelpers.error(`Unable to deploy command with name: '${cmd.data.name}' as the command data is invalid.`);
    }
}

My example command

import { Client, SlashCommandBuilder, CommandInteraction } from "discord.js";
import { Command } from "../Interfaces/Command";

module.exports = {
    data: new SlashCommandBuilder()
    .setName("test")
    .setDescription("This is a test command which is used for testing the first command"),
    execute: async (interaction: CommandInteraction) => {
        await interaction.reply("Test command recieved!");
    }
};
#

The output I'm receiving

[ERROR]: Invalid Form Body
0.name[BASE_TYPE_REQUIRED]: This field is required
 DiscordAPIError[50035]: [ERROR]: Invalid Form Body
0.name[BASE_TYPE_REQUIRED]: This field is required
    at SequentialHandler.runRequest (E:\MyDev\_Projects\Discord\dscrd-TheDirector\node_modules\@discordjs\rest\dist\index.js:667:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SequentialHandler.queueRequest (E:\MyDev\_Projects\Discord\dscrd-TheDirector\node_modules\@discordjs\rest\dist\index.js:464:14)
    at async REST.request (E:\MyDev\_Projects\Discord\dscrd-TheDirector\node_modules\@discordjs\rest\dist\index.js:910:22) {
  requestBody: { files: undefined, json: [ <1 empty item>, [Object] ] },
  rawError: {
    code: 50035,
    errors: { '0': [Object] },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'PUT',
  url: 'https://discord.com/api/v10/applications/redacted/guilds/redacted/commands'
}
#

When I log the cmdsData this is what I get

[
  <1 empty item>,
  {
    options: [],
    name: 'test',
    name_localizations: undefined,
    description: 'This is a test command which is used for testing the first command',
    description_localizations: undefined,
    default_permission: undefined,
    default_member_permissions: undefined,
    dm_permission: undefined
  }
]
teal hawk
eternal solar
#

Yeah I couldn't figure out where it was coming from

teal hawk
#

If you use delete, or some function which leaves undefined holes it'll cause problem,
Instead use splice

eternal solar
#
const cmdsData: RESTPostAPIApplicationGuildCommandsJSONBody[] = [];
        cmdsData.length = cmdsToDeploy.length;

        // Check if the first element of cmdsToDeploy is a wildcard '*'
        if (cmdsToDeploy[0] === "*") {
            // Load all commands
            UtilHelpers.log("Deploying all commands...\n");
            for (const cmd of CommandsList.values()) { RollOutCommand(cmdsData,cmd); }
        } else {
            // Verify the cmd data
            for (const cmdName of cmdsToDeploy) {
                // Try getting the command from the CommandList
                const cmd: Command | undefined = CommandsList.get(cmdName);
                if (cmd === undefined) {
                    UtilHelpers.log(`Command '${cmdName}' unable to be deployed, it is missing from the list of commands.`);
                    continue;
                }
                RollOutCommand(cmdsData,cmd);
            }
        }

I just create it here and start the process

teal hawk
#

Could you stop spamming all of your codes

eternal solar
#

Well I'm sending them so everyone can be aware of the context

#

if I'm missing code

teal hawk
eternal solar
#

and I don't know exactly where the problem lies. I will try figuring out where the undefined is coming from.

teal hawk
#

Check your command files properly

teal hawk
eternal solar
#

I just started doing this you don't gonna be like that

teal hawk
#

A fucked up cmd handler very bad logic

#

No

#

Delete it

frigid frost
#

please don't spoonfeed code

teal hawk
#

No spoonfeed

#

Delete

#

I told what the error is, now it's upto them to fix it, they continue after getting the error instead of returning and it errors when putting the commands

eternal solar
#

I'm continuing so that if one command did error I still wanted to deploy my other ones

#

it was the only reason

#

also you don't have to think everyone asking for help wants to be spoonfed

#

I have his code already copied just as a point but I still want to fix mine

#

that's not my point anyway

#

I do want to fix mine but I just regret asking tbh the way you guys went at me

teal hawk
eternal solar
#

continue should go to the next iteration of the loop correct? Unless your saying it's different in js for some reason

#

All I'm trying to say is that if the cmd is undefined I never add it to the cmdsData

#

It gets added to cmdsData in RollOutCommand

#

also

if (cmd === undefined) {
                    UtilHelpers.log(`Command '${cmdName}' unable to be deployed, it is missing from the list of commands.`);
                    continue;
                }

That isn't even entering scope since I am not receiving Command '${cmdName}' unable to be deployed, it is missing from the list of commands. in my output.

teal hawk
#

Show your first file of your commands

#

What's that logic?? ```js
if (firstCommand === "*") {
//You're deploying all the commands including the undefined ones
}

#

Improve your logic

eternal solar
#

well no because that would deploy all the commands in my commands list

#

which is what loads the command files

#

and stores them with their names

#
const CommandsList: Collection<string,Command> = new Collection();
#

So with that it loops through the values in the collection and rolls out those commands

#

but I'm not deploying with an asterisk

#

only 'test' right now

teal hawk
#

I didn't say you're deploying your asterisk OMEGAlul
You're deploying all of your commands including the undefined ones

eternal solar
#

Ok the thing is, is that I've only shown code relevant to my issue with my current conditions

npm run start -- -develop -- -deploy-commands=test
#

That's what I mean when I say I'm not deploying with an asterisk

#

meaning only this block of code is running

else {
            // Verify the cmd data
            for (const cmdName of cmdsToDeploy) {
                // Try getting the command from the CommandList
                const cmd: Command | undefined = CommandsList.get(cmdName);
                if (cmd === undefined) {
                    UtilHelpers.log(`Command '${cmdName}' unable to be deployed, it is missing from the list of commands.`);
                    continue;
                }
                RollOutCommand(cmdsData,cmd);
            }
        }
#

the first element in that loop should be 'test'

#

which it would find in my command list otherwise I would get the log

#

then it goes on to RollOutCommand

#

which just pushes the cmd.data.toJSON to the cmdsData array

teal hawk
#

Why are you making your handler so complex OMEGAlul
Just create a simple and clean handler

eternal solar
#

I can respectfully consider that but I don't see this as complex I'm just having troubles with the undefined in the array

#

but as of now if I went and removed everything I had I would of wasted a bunch of time

#

while this way allows me when I run my bot to deploy certain commands of choice or even all if needed

#

because I have experimental commands which I only want to deploy when I'm ready to use them

#

to test them before use

#

I guess using would fall under normal deployment

#

Plus later I want to make something that will track when they were last deployed so I'm only deploying commands that need to be and I will base that time around the modified date on the command files

#

I'm not saying I won't modify and improve this code later but all the code I provided is still part of testing you know like you guys criticized and joked about me starting my first bot all these things I learn as I do

#

no one just knows how to write perfect code and handlers until you've done it.

eternal solar
#

My problem all along was since I pre-allocated the array length push was being placed after the undefined

cmdsData.push(cmd.data.toJSON());
#

nothing was wrong with handler or the logic just a miss understanding of the push method.

#

You guys were the worst I won't ask for help here again my mistake.

#

part of the problem arose from the undefined so Shadow gets a point