From 5ae4461188c40d2aed0965983c5a620c4cb59d62 Mon Sep 17 00:00:00 2001 From: IO <48241519+IOxee@users.noreply.github.com> Date: Thu, 31 Jul 2025 12:58:48 +0200 Subject: [PATCH] refactor: externalize configuration --- README.md | 2 +- config.js | 17 ++++++++++++++++ server.js | 61 +++++++++++++++++++++++++++++++------------------------ 3 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 config.js diff --git a/README.md b/README.md index 1ac2e53..2771383 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ TrellordConnector bridges the gap between Trello and Discord, providing a unique ``` Now edit the .env file to fit your needs 4. **Set Up Webhooks** - - Configure Trello and Discord webhooks in `server.js`. + - Configure Trello and Discord webhooks in `config.js`. 5. **Run TrellordConnector** ```bash node server.js diff --git a/config.js b/config.js new file mode 100644 index 0000000..6058a74 --- /dev/null +++ b/config.js @@ -0,0 +1,17 @@ +module.exports = { + trellos: [ + { + boardName: 'ROADMAP', + boardId: '......', // This is the board trello id + disableComments: false, // This is to disable the comments in the console + timer_duration: 20 * 60 * 1000, // 20 minutes in milliseconds + check_interval: 5 * 60 * 1000, // 5 minutes in milliseconds + discord_configs: { + mentions: ['@everyone'], + webhookUrl: 'https://discord.com/api/webhooks/..../....', + username: 'TrellordConnector - ROADMAP', // Optional: Username of the Webhook User + avatar_url: '.....', // Optional: Link to a image file + } + } + ], +}; diff --git a/server.js b/server.js index 2d3f7ac..b6b78f5 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ const express = require('express'); const fetch = require('node-fetch'); +const fs = require('fs'); const i18n = require('./scripts/language'); const app = express(); let serverStart; @@ -12,24 +13,40 @@ let lastActionId = ''; let messagesSended = []; let messagesQueue = []; -const config = { - trellos: [ - { - 'boardName': 'ROADMAP', - 'boardId': '......', // This is the board trello id - 'disableComments': false, // This is to disable the comments in the console - 'timer_duration': 20 * 60 * 1000, // 20 minutes in milliseconds - 'check_interval': 5 * 60 * 1000, // 5 minutes in milliseconds - 'discord_configs': { - 'mentions': ['@everyone'], - 'webhookUrl': 'https://discord.com/api/webhooks/..../....', - 'username': 'TrellordConnector - ROADMAP', // Optional: Username of the Webhook User - 'avatar_url': '.....', // Optional: Link to a image file - } - } - ], +let config = require('./config'); +let checkIntervals = []; +let sendIntervals = []; + +function loadConfig() { + delete require.cache[require.resolve('./config')]; + try { + config = require('./config'); + restartIntervals(); + console.log('Configuration reloaded'); + } catch (err) { + console.error('Error loading configuration:', err); + } } +function restartIntervals() { + checkIntervals.forEach(clearInterval); + sendIntervals.forEach(clearInterval); + checkIntervals = []; + sendIntervals = []; + + for (let c of config.trellos) { + checkIntervals.push(setInterval(() => { + checkForTrelloUpdates(c); + }, c.check_interval)); + + sendIntervals.push(setInterval(() => { + sendMessagesToDiscord(c); + }, c.timer_duration)); + } +} + +fs.watchFile('./config.js', loadConfig); + app.listen(process.env.PORT, () => { console.clear(); serverStart = new Date(); @@ -49,17 +66,7 @@ app.listen(process.env.PORT, () => { ░╚═════╝░╚═╝░░╚═╝╚═╝░░░░░╚═╝╚══════╝  ╚═════╝░░░░╚═╝░░░░╚═════╝░╚═════╝░╚═╝░╚════╝░╚═════╝░ `); - for (let c of config.trellos) { - setInterval(() => { - checkForTrelloUpdates(c); - - }, c.check_interval); - - setInterval(() => { - sendMessagesToDiscord(c); - }, c.timer_duration); - - } + restartIntervals(); console.log(language.server_start + new Date().toLocaleTimeString()); });