Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Discord bot cannot send messages with slash commands

I am making a Discord bot using slash command, and i am stuck at this point. Here is the code of my index.js file

const commands = []
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
    
}
const rest = new REST({ version: '9' }).setToken(token);

(async () => {
    try {
        console.log('Started refreshing application (/) commands.');

        await rest.put(
            Routes.applicationGuildCommands(clientId, guildId),
            { body: commands },
        );

        console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
        console.error(error);
    }
})();


 client.on('ready' , () => {
     console.log('I am online')
     client.user.setActivity('MUSIC 🎧', {type:'LISTENING'})
 })

   client.on('interactionCreate', async interaction => {
       if(!interaction.isCommand())return
       const command = interaction.commands.get(interaction.commandName); 
        if(!command)return
        try {
            await command.execute(interaction)
        } catch (err) {
            console.error(err)
            await interaction.reply('There was an error trying to execute that command!')
        }
    })

And a ping.js file to send simple PING-PONG message.

const { SlashCommandBuilder } = require('@discordjs/builders')

module.exports = {
    data: new SlashCommandBuilder()
        .setName("ping")
        .setDescription("PING-PONG"),
        
    async execute (interaction) {
        await interaction.reply("Pong!")
    }
}

ERROR is:


         const command = interaction.commands.get(interaction.commandName);
                                                ^

TypeError: Cannot read properties of undefined (reading 'get')
    at Client.<anonymous> (C:\vs\Adele music bot\index.js:52:42)
    at Client.emit (node:events:520:28)

Everything is fine, it registers slash commands but as soon as i use /ping it just shows the above error.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It seems like you have copied and pasted code from different places and just hoped it worked. The error means interaction.commands is undefined, which it is.

From what I can see, you wanted a Discord.Collection with all your interaction commands within. For the purposes of this, we will use client.commands.

Code sample

/* Create the collection */
client.commands = new Discord.Collection()

const commands = []
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());

    /* Add data to the collection */
    client.commands.set(command.name, command);
}

...

/* Get the command */
const command = client.commands.get(interaction.commandName); 
about 4 years ago · Juan Pablo Isaza Report

0

const {Collection} = require('discord.js');

import the collection from discord package.

then after creating the client

   client.commands = new Collection();

use the above snippet

then

const command = client.commands.get(interaction.commandName);

write this line where the error originates.

basically you first need to make a new collection of commands for the client. then you need to add the data to add data to the collection but using below command.

client.commands.set();

then after this only you can use the get command

client.commands.set();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!