'Discord.js API latency (TypeError: Cannot read properties of undefined (reading 'ws'))

At this point, I have tried for hours to try to get the answer to this, but to no avail. So I am asking a question for this. I keep getting the same error (TypeError: Cannot read properties of undefined (reading 'ws')), and I'm not sure why. Below is my code:

const { SlashCommandBuilder } = require("@discordjs/builders")
module.exports = {
    data: new SlashCommandBuilder()
        .setName("ping")
        .setDescription("Gets bot and API latency"),
    async execute(interaction, client) {
        interaction.reply({
            content:  `Latency is ${Date.now() - interaction.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`,
            ephemeral: false,
        })
    }
}


Solution 1:[1]

In the main bot.js file make sure your interactionCreate section looks like this:

client.on('interactionCreate', async interaction => {
    if (interaction.isCommand()) {
        const command = client.commands.get(interaction.commandName)
        try {
            command.execute(client, interaction)

// The above line is what I'm referencing
// If you don't pass client through here, it can't be retrieved in the command

        } catch (error) {
            console.error(error)
            return interaction.reply({
                content: `There was an error while executing this command.`,
                ephemeral: true
            })
        }
    }
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gh0st