couteau_suisse/commands/utility/diagnostique-ws.js
2026-02-02 00:21:15 +01:00

98 lines
5.2 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { SlashCommandBuilder, EmbedBuilder, MessageFlags } = require('discord.js');
const { getWebSocketStatus, forceWebSocketReconnect } = require('../../consoleMonitor.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('diagnostique-ws')
.setDescription('Vérifier l\'état du WebSocket et forcer une reconnexion si nécessaire')
.addBooleanOption(option =>
option.setName('reconnect')
.setDescription('Forcer une reconnexion du WebSocket')
.setRequired(false)
),
async execute(interaction) {
try {
const forceReconnect = interaction.options.getBoolean('reconnect') || false;
if (forceReconnect) {
await interaction.reply({
content: '🔄 Reconnexion forcée du WebSocket en cours...',
flags: MessageFlags.Ephemeral
});
await forceWebSocketReconnect();
// Attendre un peu pour laisser le temps de se reconnecter
await new Promise(resolve => setTimeout(resolve, 3000));
}
const status = getWebSocketStatus();
const embed = new EmbedBuilder()
.setColor(status.hasWebSocket && status.wsState === 'OPEN' ? 0x00FF00 : 0xFF0000)
.setTitle('🔌 Diagnostic WebSocket')
.addFields(
{ name: '📊 Monitoring Actif', value: status.isMonitoring ? '✅ Oui' : '❌ Non', inline: true },
{ name: '🔗 WebSocket Présent', value: status.hasWebSocket ? '✅ Oui' : '❌ Non', inline: true },
{ name: '📡 État Connexion', value: status.wsState || 'N/A', inline: true },
{ name: '🔄 En cours de connexion', value: status.isConnecting ? '⏳ Oui' : '✅ Non', inline: true },
{ name: '💓 Heartbeat Actif', value: status.hasHeartbeat ? '✅ Oui' : '❌ Non', inline: true },
{ name: '⏱️ Timeout Heartbeat', value: status.hasHeartbeatTimeout ? '✅ Oui' : '❌ Non', inline: true },
{ name: '🔍 Vérification Auto', value: status.hasCheckInterval ? '✅ Oui' : '❌ Non', inline: true },
{ name: '⏱️ Reconnexion Planifiée', value: status.hasPendingReconnect ? '⏳ Oui' : '❌ Non', inline: true },
{ name: '🔁 Délai Reconnexion', value: `${Math.round(status.reconnectDelayMs / 1000)}s`, inline: true },
{ name: '🔄 Refresh Credentials', value: status.hasRefreshInterval ? '✅ Oui' : '❌ Non', inline: true }
)
.setTimestamp();
if (status.connectionTimestamp) {
embed.addFields({ name: '🕐 Dernière Connexion', value: status.connectionTimestamp, inline: false });
}
if (status.lastHeartbeatResponse) {
const timeSinceResponse = status.timeSinceLastResponse ? `${status.timeSinceLastResponse}s` : 'N/A';
embed.addFields({ name: '💓 Dernière Réponse Serveur', value: `${status.lastHeartbeatResponse}\n⏱️ Il y a ${timeSinceResponse}`, inline: false });
}
if (status.monitoringStartTimestamp) {
embed.addFields({ name: '🚀 Démarrage Monitoring', value: status.monitoringStartTimestamp, inline: false });
}
// Recommandations
let recommendations = '';
if (!status.isMonitoring) {
recommendations += '⚠️ Le monitoring n\'est pas actif. Redémarrer le bot.\n';
}
if (!status.hasWebSocket || status.wsState !== 'OPEN') {
recommendations += '⚠️ Le WebSocket n\'est pas connecté. Utilisez `/diagnostique-ws reconnect:True` pour forcer une reconnexion.\n';
}
if (status.hasPendingReconnect) {
recommendations += ` Une reconnexion est prévue dans ${Math.round(status.reconnectDelayMs / 1000)}s.\n`;
}
if (status.timeSinceLastResponse && status.timeSinceLastResponse > 45) {
recommendations += `⚠️ Aucune réponse du serveur depuis ${status.timeSinceLastResponse}s (timeout dans ${60 - status.timeSinceLastResponse}s)\n`;
}
if (recommendations) {
embed.addFields({ name: '💡 Recommandations', value: recommendations, inline: false });
} else {
embed.addFields({ name: '✅ Statut', value: 'Tout fonctionne normalement !', inline: false });
}
if (forceReconnect) {
await interaction.editReply({ content: null, embeds: [embed], flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral });
}
} catch (error) {
console.error('[DIAGNOSTIQUE-WS] Erreur:', error);
await interaction.reply({
content: '❌ Erreur lors du diagnostic',
flags: MessageFlags.Ephemeral
}).catch(() => {});
}
},
};