Home > Mobile >  why register slash commands with options doesn't work (discord.js)
why register slash commands with options doesn't work (discord.js)

Time:06-29

I was developing a discord bot and I wanted to register a slash command with some options for the ban command and when I tried to register the command gave me this error:

      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Invalid Form Body
options[0].name: Command name is invalid
options[1].name: Command name is invalid
    at RequestHandler.execute (/Users/me/Desktop/LavaBot/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RequestHandler.push (/Users/me/Desktop/LavaBot/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
    at async GuildApplicationCommandManager.create (/Users/me/Desktop/LavaBot/node_modules/discord.js/src/managers/ApplicationCommandManager.js:127:18)

in this file: /Users/me/Desktop/LavaBot/node_modules/discord.js/src/rest/RequestHandler.js:350

index.js code:

client.on('messageCreate', async (message) => {
    if (!client.application?.owner) {
        await client.application?.fetch();
    }

    if (message.content.toLowerCase() === 'lava.registra' && message.author.id === client.application?.owner.id) {
        const data = {
            name: 'ban',
            description: 'Banna un utente dal server permanentemente',
            options: [
                {
                    name: "UTENTE",
                    description: "Specifica l'utente da bannare",
                    type: "USER",
                    require: true,
                },
                {
                    name: "MOTIVO",
                    description: "Specifica il motivo del ban",
                    type: "STRING",
                    require: true,
                }
            ]
        };

        //Register a global command
        //const command = await client.application?.commands.create(data);
        //console.log(command);

        //Register a guild command
        const command = await client.guilds.cache.get('957317299289858108')?.commands.create(data);
        console.log(comando);
    }
})

and the file code:

if (res.status >= 400 && res.status < 500) {
      // Handle ratelimited requests
      if (res.status === 429) {
        const isGlobal = this.globalLimited;
        let limit, timeout;
        if (isGlobal) {
          // Set the variables based on the global rate limit
          limit = this.manager.globalLimit;
          timeout = this.manager.globalReset   this.manager.client.options.restTimeOffset - Date.now();
        } else {
          // Set the variables based on the route-specific rate limit
          limit = this.limit;
          timeout = this.reset   this.manager.client.options.restTimeOffset - Date.now();
        }

        this.manager.client.emit(
          DEBUG,
          `Hit a 429 while executing a request.
    Global  : ${isGlobal}
    Method  : ${request.method}
    Path    : ${request.path}
    Route   : ${request.route}
    Limit   : ${limit}
    Timeout : ${timeout}ms
    Sublimit: ${sublimitTimeout ? `${sublimitTimeout}ms` : 'None'}`,
        );

        await this.onRateLimit(request, limit, timeout, isGlobal);

        // If caused by a sublimit, wait it out here so other requests on the route can be handled
        if (sublimitTimeout) {
          await sleep(sublimitTimeout);
        }
        return this.execute(request);
      }

      // Handle possible malformed requests
      let data;
      try {
        data = await parseResponse(res);
      } catch (err) {
        throw new HTTPError(err.message, err.constructor.name, err.status, request);
      }

      throw new DiscordAPIError(data, res.status, request);
    }

What's wrong?

CodePudding user response:

Command names must be all lowercase, between 1 and 32 characters, so UTENTE should be utente, and MOTIVO should be motivo.

  • Related