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) {

}
}
}
},
};
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) {

}
}
}
},
};
7 Replies
d.js toolkit
d.js toolkitā€¢4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - āœ… Marked as resolved by OP
X_DigitalNinja
X_DigitalNinjaOPā€¢4mo ago
ā”œā”€ā”€ @anitrack/[email protected] ā”œā”€ā”€ @discordjs/[email protected] ā”œā”€ā”€ @discordjs/[email protected] ā”œā”€ā”€ @discordjs/[email protected] ā”œā”€ā”€ @discordjs/[email protected] ā”œā”€ā”€ @discordjs/[email protected] ā”œā”€ā”€ @sapphire/[email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā”œā”€ā”€ [email protected] ā””ā”€ā”€ [email protected] Node version v21.6.2
X_DigitalNinja
X_DigitalNinjaOPā€¢4mo ago
I get
No description
Syjalo
Syjaloā€¢4mo ago
With /core you get raw API data which doesn't have client, helper methods and properties. * To calculate the created timestamp you should deconstruct the interaction.id eg with @sapphire/snowflake * The ws latency (ping) is emitted on WSWebSocketShardEvents.HeartbeatComplete event. Save it somewhere when you receive the event Btw why did you switch to /core?
X_DigitalNinja
X_DigitalNinjaOPā€¢4mo ago
Idk ngl I just thought I give it a try. someone said Core was good. So I just wen't with it What be better Core or discord.js
Syjalo
Syjaloā€¢4mo ago
It looks like you need the features of discord.js, so definitely discord.js is better for you
X_DigitalNinja
X_DigitalNinjaOPā€¢4mo ago
Ok Imma start the transition Thanks!
Want results from more Discord servers?
Add your server