Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way I can make this work ?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading