diff --git a/projects/backend/src/index.ts b/projects/backend/src/index.ts index 0afb4f1..e2ce126 100644 --- a/projects/backend/src/index.ts +++ b/projects/backend/src/index.ts @@ -23,7 +23,8 @@ import LeaderboardController from "./controller/leaderboard.controller"; import { getAppVersion } from "./common/app.util"; import { connectScoresaberWebsocket } from "@ssr/common/websocket/scoresaber-websocket"; import { connectBeatLeaderWebsocket } from "@ssr/common/websocket/beatleader-websocket"; -import { initDiscordBot } from "./bot/bot"; +import { DiscordChannels, initDiscordBot, logToChannel } from "./bot/bot"; +import { EmbedBuilder } from "discord.js"; // Load .env file dotenv.config({ @@ -40,11 +41,23 @@ connectScoresaberWebsocket({ await ScoreService.trackScoreSaberScore(score); await ScoreService.notifyNumberOne(score); }, + onDisconnect: async error => { + await logToChannel( + DiscordChannels.backendLogs, + new EmbedBuilder().setDescription(`ScoreSaber websocket disconnected: ${JSON.stringify(error)}`) + ); + }, }); connectBeatLeaderWebsocket({ onScore: async score => { await ScoreService.trackBeatLeaderScore(score); }, + onDisconnect: async error => { + await logToChannel( + DiscordChannels.backendLogs, + new EmbedBuilder().setDescription(`BeatLeader websocket disconnected: ${JSON.stringify(error)}`) + ); + }, }); export const app = new Elysia(); diff --git a/projects/common/src/websocket/websocket.ts b/projects/common/src/websocket/websocket.ts index 0354b50..8acb159 100644 --- a/projects/common/src/websocket/websocket.ts +++ b/projects/common/src/websocket/websocket.ts @@ -1,6 +1,4 @@ import WebSocket from "ws"; -import { DiscordChannels, logToChannel } from "backend/src/bot/bot"; -import { EmbedBuilder } from "discord.js"; export type WebsocketCallbacks = { /** @@ -36,18 +34,6 @@ type Websocket = { export function connectWebSocket({ name, url, onMessage, onDisconnect }: Websocket) { let websocket: WebSocket | null = null; - /** - * Logs to the backend logs channel. - * - * @param error the error to log - */ - async function log(error: WebSocket.ErrorEvent | WebSocket.CloseEvent) { - await logToChannel( - DiscordChannels.backendLogs, - new EmbedBuilder().setDescription(`${name} websocket disconnected: ${JSON.stringify(error)}`) - ); - } - function connectWs() { websocket = new WebSocket(url); @@ -62,14 +48,12 @@ export function connectWebSocket({ name, url, onMessage, onDisconnect }: Websock } onDisconnect && onDisconnect(event); - log(event); }; websocket.onclose = event => { console.log(`Lost connection to the ${name} WebSocket. Attempting to reconnect...`); onDisconnect && onDisconnect(event); - log(event); setTimeout(connectWs, 5000); // Reconnect after 5 seconds };