-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathping.ts
48 lines (42 loc) · 1.64 KB
/
ping.ts
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
import { setTimeout } from "node:timers/promises";
import { inlineCode, time, RESTJSONErrorCodes, TimestampStyles, type ChatInputCommandInteraction } from "discord.js";
import type { Command } from "../../structures/command.js";
export default {
data: {
name: "ping",
description: "Pong!",
},
opt: {
userPermissions: ["SendMessages"],
botPermissions: ["SendMessages"],
category: "General",
cooldown: 5,
},
async execute(interaction: ChatInputCommandInteraction<"cached">) {
const currentTime = 1_000 * 75 - interaction.client.uptime;
const botReadyTimestamp = Math.round((Date.now() + currentTime) / 1_000);
// This is to prevent from using the command so the client has a chance to output a proper latency report
if (interaction.client.uptime < 1_000 * 75) {
await interaction.reply({
content: `The bot is still starting up. Run this command again ${time(botReadyTimestamp, TimestampStyles.RelativeTime)} to see statistical information.`,
ephemeral: true,
});
return;
}
const msg = await interaction.reply({
content: "🏓 Pinging...",
fetchReply: true,
});
try {
await setTimeout(3_000);
const ping = msg.createdTimestamp - interaction.createdTimestamp;
await interaction.editReply({
content: `Pong 🏓! \nRoundtrip Latency is ${inlineCode(`${ping}ms`)}. \nWebsocket Heartbeat is ${inlineCode(`${interaction.client.ws.ping}ms`)}`,
});
} catch (error) {
if (error.code === RESTJSONErrorCodes.UnknownMessage) {
console.error(`Failed to edit interaction: ${error.message}`);
}
}
},
} satisfies Command;