From 9ddd4c3916955ad8168208ed73222cbe190ca517 Mon Sep 17 00:00:00 2001 From: vrtnd Date: Fri, 26 Jul 2024 12:02:29 +0300 Subject: [PATCH] Fix stale bridge notifications --- src/handlers/checkStaleBridges.ts | 22 ++++++++-------------- src/utils/discord.ts | 17 ++++++++++++----- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/handlers/checkStaleBridges.ts b/src/handlers/checkStaleBridges.ts index fb96aac5..44cd937e 100644 --- a/src/handlers/checkStaleBridges.ts +++ b/src/handlers/checkStaleBridges.ts @@ -56,35 +56,29 @@ const checkStaleBridges = async () => { console.log("Stale bridges report (3+ days stale):"); let hasStaleEntries = false; - let discordMessage = "Bridges stale for more than 3 days:\n"; + let discordMessage = "Bridges stale for 3+ days:\n"; for (const [bridgeName, chains] of Object.entries(result)) { - let bridgeHasStaleEntries = false; - let bridgeMessage = `\nBridge: ${bridgeName}\n`; + let staleChains = []; for (const [chain, staleDays] of Object.entries(chains)) { - if (staleDays >= 3 || staleDays === -1) { + if (staleDays >= 3) { hasStaleEntries = true; - bridgeHasStaleEntries = true; - const statusMessage = staleDays === -1 ? "Error occurred" : `${staleDays} days stale`; - bridgeMessage += ` ${chain}: ${statusMessage}\n`; - - if (staleDays > 3) { - discordMessage += `${bridgeName} on ${chain}: ${staleDays} days\n`; - } + staleChains.push(chain); } } - if (bridgeHasStaleEntries) { - console.log(bridgeMessage); + if (staleChains.length > 0) { + discordMessage += `${bridgeName}: [${staleChains.join(", ")}]\n`; } } if (!hasStaleEntries) { console.log("No bridges are 3 or more days stale."); + discordMessage = "No bridges are stale for 3+ days."; } - if (discordMessage !== "Bridges stale for more than 3 days:\n") { + if (discordMessage !== "Bridges stale for 3+ days:\n") { try { await sendDiscordText(discordMessage); console.log("Discord message prepared (not sent):", discordMessage); diff --git a/src/utils/discord.ts b/src/utils/discord.ts index f768eb00..affabad5 100644 --- a/src/utils/discord.ts +++ b/src/utils/discord.ts @@ -2,6 +2,11 @@ import fetch from "node-fetch"; export async function sendDiscordText(message: string): Promise { try { + if (message.length > 2000) { + console.warn("Discord message exceeds 2000 characters. Truncating..."); + message = message.substring(0, 1997) + "..."; + } + const res = await fetch(process.env.DISCORD_WEBHOOK as string, { method: "POST", headers: { @@ -9,13 +14,15 @@ export async function sendDiscordText(message: string): Promise { }, body: JSON.stringify({ content: message }), }); + if (!res.ok) { - console.error("Failed to send message to Discord:", res.statusText); + const errorText = await res.text(); + console.error(`Failed to send message to Discord: Status ${res.status} ${res.statusText}`); + console.error("Error details:", errorText); } - if (res.ok) { - console.log("Message sent to Discord:", message); - } - } catch (error) { + + console.log("Message sent to Discord successfully"); + } catch (error: any) { console.error("Failed to send message to Discord:", error); } }