commit
Add d$ping to show Discord and API response times
Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/commands/help.ts b/src/commands/help.ts
index 974b950..f2691d8 100644
--- a/src/commands/help.ts
+++ b/src/commands/help.ts
@@ -9,6 +9,7 @@ const helpText = [
"`d$reproxy <member>` resends the message you reply to, or your latest proxied message, as someone else, like `d$reproxy c1`. `d$rp` works too.",
"`d$switch <member>` sets who's fronting on the site, like `d$switch c1 c2`. `d$switch out` clears it. `d$sw` works too.",
"`d$member <member>` shows a member's card, like `d$member aether`. `d$m` works too.",
+ "`d$ping` shows how fast Discord and the system API respond.",
"-# React ❌ to delete a proxied message, ❓ to see who sent it.",
].join("\n");
diff --git a/src/commands/index.ts b/src/commands/index.ts
index e24fdfd..f0aaa58 100644
--- a/src/commands/index.ts
+++ b/src/commands/index.ts
@@ -8,6 +8,7 @@ import { explainCommand } from "./explain.ts";
import { helpCommand } from "./help.ts";
import { memberCommand } from "./member.ts";
import { parseCommand } from "./parse.ts";
+import { pingCommand } from "./ping.ts";
import { reproxyCommand } from "./reproxy.ts";
import { switchCommand } from "./switch.ts";
@@ -30,6 +31,7 @@ const commands: Record<string, CommandHandler> = {
sw: switchCommand,
member: memberCommand,
m: memberCommand,
+ ping: pingCommand,
help: helpCommand
};
diff --git a/src/commands/ping.ts b/src/commands/ping.ts
new file mode 100644
index 0000000..59400ec
--- /dev/null
+++ b/src/commands/ping.ts
@@ -0,0 +1,41 @@
+/* src/commands/ping.ts */
+
+import type { Message } from "discord.js";
+
+import { apiBase } from "../config/constants.ts";
+
+// Round trip to the system site's API, or why it failed
+async function apiPing(): Promise<string> {
+ const started = performance.now();
+
+ try {
+ const response = await fetch(`${apiBase}/system`);
+ await response.body?.cancel();
+ const took = Math.round(performance.now() - started);
+
+ return response.ok ? `${took} ms` : `HTTP ${response.status} after ${took} ms`;
+ } catch (error) {
+ return `failed (${error instanceof Error ? error.message : "unknown error"})`;
+ }
+}
+
+function report(heartbeat: number, discord: number | null, api: string): string {
+ return [
+ "🏓 Pong!",
+ `Discord heartbeat: ${heartbeat < 0 ? "not measured yet" : `${heartbeat} ms`}`,
+ `Discord round trip: ${discord === null ? "measuring…" : `${discord} ms`}`,
+ `System API: ${api}`,
+ ].join("\n");
+}
+
+export async function pingCommand(message: Message): Promise<void> {
+ const apiResult = apiPing();
+ const heartbeat = message.client.ws.ping;
+
+ // Send, then edit with the time the send took
+ const started = performance.now();
+ const reply = await message.reply(report(heartbeat, null, "measuring…"));
+ const discord = Math.round(performance.now() - started);
+
+ await reply.edit(report(heartbeat, discord, await apiResult));
+}