From 6bbaac2870bac5e1462467370517b550acd442ea Mon Sep 17 00:00:00 2001 From: mileswa1q22 Date: Fri, 10 Apr 2026 17:00:47 -0500 Subject: [PATCH] updated added webhooks to the server --- .gitignore | 3 ++- app.js | 2 +- server/server.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b523b81..1dd6713 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ server/runs.json server/messages.json # Environment variables -server/.env \ No newline at end of file +server/.env +server/config.json \ No newline at end of file diff --git a/app.js b/app.js index a3c9f85..26a44dc 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,7 @@ const page = document.body.dataset.page; const isFileProtocol = window.location.protocol === 'file:'; - const TESTING_MODE = false ; + const TESTING_MODE = false; const liveServerBase = TESTING_MODE ? 'http://127.0.0.1:8090/fedl' : 'https://server.fedl.site/fedl'; const canUseLiveServer = !isFileProtocol || !!liveServerBase; const liveApiUrl = `${liveServerBase}/api/list`; diff --git a/server/server.js b/server/server.js index f63b622..7ec055d 100644 --- a/server/server.js +++ b/server/server.js @@ -15,6 +15,41 @@ const resetTokensPath = path.join(__dirname, 'reset_tokens.json'); const postsPath = path.join(__dirname, 'posts.json'); const bugReportsPath = path.join(__dirname, 'bugreports.json'); const messagesPath = path.join(__dirname, 'messages.json'); +const configPath = path.join(__dirname, 'config.json'); + +const serverConfig = safeReadJsonFile(configPath, {}, 'config.json'); +const discordWebhookUrl = serverConfig.discordWebhookUrl || ''; + +async function sendDiscordNotification(message) { + if (!discordWebhookUrl || !discordWebhookUrl.startsWith('http')) { + return; + } + try { + const https = require('https'); + const payload = JSON.stringify({ content: message }); + const url = new URL(discordWebhookUrl); + const options = { + hostname: url.hostname, + port: 443, + path: url.pathname + url.search, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(payload) + } + }; + const req = https.request(options, (res) => { + if (res.statusCode >= 400) { + console.error(`[FEDL] Discord webhook error: ${res.statusCode}`); + } + }); + req.on('error', (e) => console.error(`[FEDL] Discord webhook error: ${e.message}`)); + req.write(payload); + req.end(); + } catch (e) { + console.error(`[FEDL] Discord notification failed: ${e.message}`); + } +} const port = Number(process.env.PORT) || 8090; const host = process.env.HOST || '127.0.0.1'; const BASE = '/fedl'; @@ -1782,8 +1817,19 @@ const server = http.createServer((req, res) => { try { const payload = JSON.parse(body || '{}'); const text = String(payload.text || '').trim(); + const oldText = readDataText(); + const oldLines = new Set(oldText.split('\n').map(l => l.trim()).filter(Boolean)); + const newLines = text.split('\n').map(l => l.trim()).filter(Boolean); + const addedLines = newLines.filter(l => !oldLines.has(l)); writeDataText(text); sendEvent('list-update', { updatedAt: new Date().toISOString() }); + if (addedLines.length > 0) { + const newLevels = addedLines.map(line => { + const parts = line.split('|'); + return parts[2] || line; + }).join(', '); + sendDiscordNotification(`🆕 **New levels added to the list:** ${newLevels}`); + } sendJson(res, 200, { ok: true }); } catch (error) { sendJson(res, 400, { error: 'Invalid list payload' });