Ping command

So I am a bit confused so I so I am new to using d.js/@core and d.js/@rest Witch I am fully using and I am having issues making my ping command work

import { SlashCommandBuilder } from "@discordjs/builders";
import { MessageFlags, InteractionType } from "@discordjs/core";
import { config } from "../../../config.js";
import guildModel from "../../models/guild.js";

export default {
    data: new SlashCommandBuilder().setName("ping").setDescription("Get the bot's ping").setDMPermission(false),

    async execute(interaction, api) {
        try {
            if (interaction.type !== InteractionType.ApplicationCommand) {
                throw new Error("Unsupported interaction type.");
            }

            let guildData = await guildModel.findOne({ guildId: interaction.guild_id }).catch(() => null);

            const wsPing = interaction.client?.ws?.ping;
            const apiLatency = Date.now() - interaction.createdTimestamp;

            const fields = [];

            if (typeof wsPing === "number") {
                fields.push({ name: "WebSocket Latency", value: `${wsPing}ms`, inline: true });
            } else {
                fields.push({ name: "WebSocket Latency", value: `Unable to fetch`, inline: true });
            }

            if (!isNaN(apiLatency)) {
                fields.push({ name: "API Latency", value: `${apiLatency}ms`, inline: true });
            } else {
                fields.push({ name: "API Latency", value: `Unable to fetch`, inline: true });
            }

            const pingEmbed = {
                color: parseInt(guildData?.color ?? config.colors.main),
                title: `Pong! šŸ“`,
                fields: fields,
                footer: {
                    text: "Cosmo Bot",
                },
            };

            await api.interactions.reply(interaction.id, interaction.token, {
                embeds: [pingEmbed],
                flags: MessageFlags.Ephemeral | MessageFlags.SuppressNotifications,
            });

        } catch (err) {
            if (!interaction.replied && !interaction.deferred) {
                try {
                    await api.interactions.reply(interaction.id, interaction.token, {
                        content: `An error occurred while executing the command. Please try again later.`,
                        flags: MessageFlags.Ephemeral | MessageFlags.SuppressNotifications,
                    });
                } catch (replyError) {
                
                }
            }
        }
    },
};
Was this page helpful?