all repos — stealth-developers @ 2a451ccea8ab4d5e21eb8469ce571d46920ad06f

src/interactions/commands/ping.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
import {
	type ChatInputCommandInteraction,
	type Client,
	SlashCommandBuilder,
} from "discord.js";

const commandData = new SlashCommandBuilder()
	.setName("ping")
	.setDescription("ping the bot");

async function execute(
	client: Client,
	interaction: ChatInputCommandInteraction,
) {
	const pingMs = Date.now() - interaction.createdTimestamp;

	const initial = await interaction.reply({
		content: "calc'ing latency information...",
		flags: ["Ephemeral"],
	});

	const roundTripMs = Date.now() - initial.createdTimestamp;
	const wsHeartbeat = client.ws.ping;

	return interaction.editReply({
		content:
			`time to receive was ${pingMs}ms, ` +
			`round trip is ${roundTripMs}ms, ` +
			`ws heartbeat is ${wsHeartbeat}ms, ` +
			`uptime is ${formatUptime(client.uptime || 0)}`,
	});
}

function formatUptime(uptime: number): string {
	const totalSeconds = Math.floor(uptime / 1000);
	const days = Math.floor(totalSeconds / 86400);
	const hours = Math.floor((totalSeconds % 86400) / 3600);
	const minutes = Math.floor((totalSeconds % 3600) / 60);
	const seconds = totalSeconds % 60;

	const parts = [];
	if (days > 0) parts.push(`${days}d`);
	if (hours > 0) parts.push(`${hours}h`);
	if (minutes > 0) parts.push(`${minutes}m`);
	if (seconds > 0 || parts.length === 0) parts.push(`${seconds}s`);

	return parts.join(" ");
}

export default {
	data: commandData,
	execute,
};