Search code examples
discord.js

Getting -1ms trying to make a Ping Command in DiscordJS


Ping Command:

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('See the ping of the bot.'),
    async execute(interaction) {
        const embedPinging = new EmbedBuilder()
            .setTitle(`Ping`)
            .setDescription(`Pinging...`)
            .setColor(`#ff00c3`)

        const sent = await interaction.reply({ embeds: [embedPinging], fetchReply: true })

        const embedPing = new EmbedBuilder()
            .setTitle(`Ping`)
            .setDescription(`
            Bot: ${sent.createdTimestamp - interaction.createdTimestamp}ms
            API: ${interaction.client.ws.ping}ms`)
            .setColor(`#ff00c3`)
        
            interaction.editReply({ embeds: [embedPing] })
    },
};

My index.js:

const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
    ],
});

client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require(filePath);
        if ('data' in command && 'execute' in command) {
            client.commands.set(command.data.name, command);
        } else {
            console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
        }
    }
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

client.login(token);

Interaction Create:

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

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isChatInputCommand()) return;

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

        if (!command) {
            console.error(`No command matching ${interaction.commandName} was found.`);
            return;
        }

        try {
            await command.execute(interaction);
        } catch (error) {
            console.error(`Error executing ${interaction.commandName}`);
            console.error(error);
        }
    },
};

Basically, when running the command, the API Ping shows as -1ms always. The Bot ping shows fine, but the API Ping don't. I'm pretty lost at this point and I don't know what is happening, I'm pretty new using JS and Discord.js.

Thanks in advance for the help!


Solution

  • Your bot hasn't hit its first heartbeat yet. You usually have to wait around 1 minute for the first heartbeat after which you can see your bot's ping.