diff --git a/index.js b/index.js index 7b8b626..470f10a 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const colors = require("colors"); const sqlite3 = require("sqlite3").verbose(); const WebSocket = require('ws'); const events = require("./data/events.json"); - +const path = require("path"); satMessages = {}; // Setup Discord @@ -357,7 +357,7 @@ Image and product text are optional, but if they exist, they will be in the data var sent = {} -const handleDiscord = function (data) { +const handleDiscord = function (data, randStr) { const rawBody = data.data.body; const text = data.data.productText; const product_id_raw = data.data.raw @@ -374,7 +374,7 @@ const handleDiscord = function (data) { color: parseInt(config.priorityColors[evt.priority].replace("#", ""), 16) || 0x000000, timestamp: product_id.timestamp, footer: { - text: `Station: ${product_id.station} PID: ${product_id_raw} Channel: ${fromChannel}` + text: `${randStr} Station: ${product_id.station} PID: ${product_id_raw} Channel: ${fromChannel}` } } if (data.data.image) { @@ -593,11 +593,22 @@ function connectWebSocket() { }); ws.on('message', (rawData) => { + // Save copy of rawData for debugging to event-logs/year/month/dat/random6charstring-unixtimestamp.json + const timestamp = new Date().toISOString().replace(/:/g, '-'); + const randomString = generateRandomString({ lower: true, upper: true, number: true }, 6); + const logDir = path.join(__dirname, "event-logs", timestamp.substring(0, 4), timestamp.substring(5, 7), timestamp.substring(8, 10)); + if (!fs.existsSync(logDir)) { + fs.mkdirSync(logDir, { recursive: true }); + } + const logFile = path.join(logDir, `${randomString}-${Date.now()}.json`); + fs.writeFileSync(logFile, rawData); + try { const data = JSON.parse(rawData); + switch (data.type) { case "iem-message": - handleDiscord(data); + handleDiscord(data, randomString); break; case "internal-response": @@ -656,6 +667,7 @@ discord.on('ready', async () => { // Do slash command stuff commands = require("./data/commands.json"); + // Add dynamic commands (based on datas files) satCommand = { "name": "satellite",