diff --git a/commands/ping.mjs b/commands/ping.mjs new file mode 100644 index 0000000..c2fea29 --- /dev/null +++ b/commands/ping.mjs @@ -0,0 +1,104 @@ +/** + * @module commands/ping + * @description Show the bot's latency: REST roundtrip, gateway heartbeat and audio node ping. + */ + +import { CommandBuilder } from "../src/commands/index.mjs"; +import { EmbedBuilder } from "@fluxerjs/core"; +import { getGlobalColor } from "../src/ui/index.mjs"; + +/** + * @type {CommandBuilder} + * @description Command definition for the ping command. + */ +export const command = new CommandBuilder() + .setName("ping") + .setDescription("Check the bot's latency and connection health.", "commands.ping") + .addAliases("latency", "pong") + .setCategory("util"); + +/** + * Safely read the gateway heartbeat latency (`client.ws.ping`). + * Returns -1 when unavailable (not logged in yet, or no heartbeat ACK received yet). + * @private + * @param {object} client - The bot client. + * @returns {number} Gateway latency in ms, or -1. + */ +function readGatewayPing(client) { + try { + const ping = client?.ws?.ping; + return typeof ping === "number" && Number.isFinite(ping) ? ping : -1; + } catch (_) { + return -1; + } +} + +/** + * Safely read the audio node latency from the connected NodeLink/Lavalink node. + * `node.stats.ping` is the node's own gateway heartbeat latency; it can be -1 + * or undefined while the node has no playing players. + * @private + * @param {object} lavalink - The LavalinkManager instance. + * @returns {object|null} `{ connected: true, ping: number|null }`, or null when no node is available. + */ +function readNodePing(lavalink) { + try { + const node = lavalink?.getNode?.(); + if (!node) return null; + const ping = node?.stats?.ping; + return { + connected: true, + ping: (typeof ping === "number" && Number.isFinite(ping) && ping >= 0) ? ping : null, + }; + } catch (_) { + return null; + } +} + +/** + * Run handler for the ping command. + * Replies with a placeholder embed (which measures the REST roundtrip), then + * edits it with the roundtrip time, the gateway heartbeat latency and the + * audio node ping when available. + * + * @param {object} message - The command message wrapper. + * @returns {Promise} + */ +export async function run(message) { + const t = (...a) => this.t(message, ...a); + + const waitingEmbed = new EmbedBuilder() + .setColor(getGlobalColor()) + .setDescription(t("responses.ping.measuring")); + + const t0 = Date.now(); + let sent; + try { + sent = await message.reply({ embeds: [waitingEmbed] }); + } catch (e) { + return; // couldn't even send the placeholder — nothing to edit + } + const roundtrip = Date.now() - t0; + + const lines = [ + `${t("responses.ping.roundtrip")} — \`${roundtrip}ms\``, + ]; + + const gateway = readGatewayPing(this.client); + if (gateway >= 0) lines.push(`${t("responses.ping.gateway")} — \`${gateway}ms\``); + + const node = readNodePing(this.lavalink); + if (node) { + lines.push(`${t("responses.ping.node")} — \`${node.ping !== null ? node.ping + "ms" : "connected"}\``); + } + + const embed = new EmbedBuilder() + .setColor(getGlobalColor()) + .setAuthor({ + name: t("responses.ping.title"), + iconURL: this.client?.user?.displayAvatarURL?.() || null, + }) + .setDescription(lines.join("\n")); + + sent.edit({ embeds: [embed] }).catch(() => {}); +} diff --git a/storage/locales/bot/ar-SA.json b/storage/locales/bot/ar-SA.json index 5e341be..d5f060c 100644 --- a/storage/locales/bot/ar-SA.json +++ b/storage/locales/bot/ar-SA.json @@ -355,6 +355,13 @@ "alreadyPaused": "❌ متوقف بالفعل!", "paused": "⏸️ تم الإيقاف المؤقت" }, + "ping": { + "title": "🏓 **Pong!**", + "measuring": "🏓 جارٍ قياس البينغ...", + "roundtrip": "📶 **زمن الاستجابة**", + "gateway": "🌐 **البوابة**", + "node": "🎧 **NodeLink**" + }, "play": { "searching": "🔍 جارٍ البحث...", "loading": "جارٍ التحميل...", diff --git a/storage/locales/bot/ckb.json b/storage/locales/bot/ckb.json index 045c3b3..0632d7d 100644 --- a/storage/locales/bot/ckb.json +++ b/storage/locales/bot/ckb.json @@ -355,6 +355,13 @@ "alreadyPaused": "❌ پێشتر وەستاوە!", "paused": "⏸️ وەستا" }, + "ping": { + "title": "🏓 **Pong!**", + "measuring": "🏓 پینگ دەپێورێت...", + "roundtrip": "📶 **ماوەی وەڵام**", + "gateway": "🌐 **دەروازە**", + "node": "🎧 **NodeLink**" + }, "play": { "searching": "🔍 گەڕان...", "loading": "بارکردن...", diff --git a/storage/locales/bot/de-DE.json b/storage/locales/bot/de-DE.json index c9834a0..a4e1b80 100644 --- a/storage/locales/bot/de-DE.json +++ b/storage/locales/bot/de-DE.json @@ -355,6 +355,13 @@ "alreadyPaused": "❌ Bereits pausiert!", "paused": "⏸️ Pausiert" }, + "ping": { + "title": "🏓 **Pong!**", + "measuring": "🏓 Ping wird gemessen...", + "roundtrip": "📶 **Latenz**", + "gateway": "🌐 **Gateway**", + "node": "🎧 **NodeLink**" + }, "play": { "searching": "🔍 Suche...", "loading": "Laden...", diff --git a/storage/locales/bot/en.json b/storage/locales/bot/en.json index d91ceeb..0a0b087 100644 --- a/storage/locales/bot/en.json +++ b/storage/locales/bot/en.json @@ -472,6 +472,13 @@ "alreadyPaused": "❌ Already paused!", "paused": "⏸️ Paused" }, + "ping": { + "title": "🏓 **Pong!**", + "measuring": "🏓 Pinging...", + "roundtrip": "📶 **Roundtrip**", + "gateway": "🌐 **Gateway**", + "node": "🎧 **NodeLink**" + }, "play": { "searching": "🔍 Searching...", "loading": "Loading...", diff --git a/storage/locales/bot/pt-BR.json b/storage/locales/bot/pt-BR.json index a635525..d64b5b7 100644 --- a/storage/locales/bot/pt-BR.json +++ b/storage/locales/bot/pt-BR.json @@ -355,6 +355,13 @@ "alreadyPaused": "❌ Já está pausado!", "paused": "⏸️ Pausado" }, + "ping": { + "title": "🏓 **Pong!**", + "measuring": "🏓 Medindo o ping...", + "roundtrip": "📶 **Latência**", + "gateway": "🌐 **Gateway**", + "node": "🎧 **NodeLink**" + }, "play": { "searching": "🔍 Buscando...", "loading": "Carregando...", diff --git a/storage/locales/bot/tr.json b/storage/locales/bot/tr.json index 5bf3750..32d5ff9 100644 --- a/storage/locales/bot/tr.json +++ b/storage/locales/bot/tr.json @@ -461,6 +461,13 @@ "alreadyPaused": "❌ Zaten duraklatılmış!", "paused": "⏸️ Duraklatıldı" }, + "ping": { + "title": "🏓 **Pong!**", + "measuring": "🏓 Ping ölçülüyor...", + "roundtrip": "📶 **Gecikme**", + "gateway": "🌐 **Gateway**", + "node": "🎧 **NodeLink**" + }, "play": { "searching": "🔍 Aranıyor...", "loading": "Yükleniyor...",