Home > OS >  Discord.js : commands.map is not a function at ApplicationCommandManager.set
Discord.js : commands.map is not a function at ApplicationCommandManager.set

Time:12-02

I am encountering a TypeError while trying to set up the commands for my bot. This happens when trying to use commands.set

function setCommands(): void {
  var slashCommandFiles = fs.readdirSync('./commands').filter((file: string) => (file.endsWith('.js') && !forbiddenCommands.includes(file.slice(0, -3))));
  for (const file of slashCommandFiles) {
    const command = require(`./commands/${file}`);
    slashCommands.push(command.data.toJSON());
    if (client.application)
      client.application.commands.set(command.data.name, command); //happens here
    else {
      console.error(client);
      throw new Error("Unable to load the commands");
    }
  }
}

The error message is the following:

C:\Overbot\node_modules\discord.js\src\managers\ApplicationCommandManager.js:147
      data: commands.map(c => this.constructor.transformCommand(c)),
                     ^

TypeError: commands.map is not a function
    at ApplicationCommandManager.set (C:\Overbot\node_modules\discord.js\src\managers\ApplicationCommandManager.js:147:22)
    at setCommands (C:\Overbot\index.js:68:41)
    at C:\Overbot\index.js:108:17
    at step (C:\Overbot\index.js:34:23)
    at Object.next (C:\Overbot\index.js:15:53)
    at C:\Overbot\index.js:9:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Overbot\index.js:5:12)
    at Client.<anonymous> (C:\Overbot\index.js:98:43)
    at Object.onceWrapper (node:events:514:26)

Thing is, it's definitely a function that does exist, since I verified the code and found it with no problem. I don't think this is a semicolon shenanigan either, because I already tested it with and without semicolons everywhere, to the same result.

Is there any way I can make this work ?

CodePudding user response:

That's not how you set the commands. Use this instead:

client.application.commands.set(slashCommands) 
//I assume slashCommands is an array of ApplicationCommandData, according to your code
  • Related