How to add multiple slash commands to a Discord bot?

I’m working on a Discord bot and I’ve managed to set up one slash command. Now I want to add more commands but I’m stuck. Here’s what I have so far:

{
  "name": "cuddle",
  "description": "Get a cute animal picture",
  "options": [
    {
      "name": "species",
      "description": "Pick an animal",
      "type": 3,
      "required": true,
      "choices": [
        {"name": "Puppy", "value": "pup_pic"},
        {"name": "Kitten", "value": "kit_pic"},
        {"name": "Duckling", "value": "duck_pic"}
      ]
    },
    {
      "name": "baby_only",
      "description": "Show only baby animals?",
      "type": 5,
      "required": false
    }
  ]
}

I tried making this into an array and adding more objects, but it didn’t work. What’s the right way to add multiple slash commands to my bot? Any help would be great!

I had a similar challenge when expanding my bot. I ended up storing each slash command in a separate object within an array. Then I looped over that array and registered each command individually with my bot, which helped keep my code neatly organized. Instead of trying to merge multiple commands into one object, creating discrete command objects allowed for more flexibility. This method not only simplified troubleshooting but also made future additions easier. I hope this approach works well for your project.

To add multiple slash commands, you’ll want to create an array of command objects. Each object in the array should represent a single command, following the structure you’ve shown. Here’s a basic example:

const commands = [
  {
    name: 'cuddle',
    description: 'Get a cute animal picture',
    options: [
      // Your existing options here
    ]
  },
  {
    name: 'weather',
    description: 'Check the weather',
    options: [
      // New command options
    ]
  }
];

Then, use the Discord API’s bulk command registration method to register all commands at once. This approach is more efficient and easier to maintain as your bot grows. Remember to update your command handling logic to accommodate the new commands you’ve added.

hey, i’ve been there! what worked for me was putting each command in its own file, then using a loop to read and register them. it’s way easier to manage that way. just make sure you’re using the right discord.js version (v13+) for slash commands. good luck with ur bot!