From fb7d1c89101f72c883c1830ed29e6374f6e4e646 Mon Sep 17 00:00:00 2001 From: Jamie <83149767+ParallaxOnline@users.noreply.github.com> Date: Tue, 19 Mar 2024 20:29:26 +0000 Subject: [PATCH 1/2] Create file_watcher_EVT.js Simple file watch event. Triggers when a file is created in a dir. I had a use, Someone else might hence why i'm requesting a merg. --- events/file_watcher_EVT.js | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 events/file_watcher_EVT.js diff --git a/events/file_watcher_EVT.js b/events/file_watcher_EVT.js new file mode 100644 index 00000000..59277744 --- /dev/null +++ b/events/file_watcher_EVT.js @@ -0,0 +1,58 @@ +module.exports = { + name: 'File Watcher', + displayName: 'File Watch Event', + isEvent: true, + + fields: [ + 'Directory to Watch (Relative to bot directory)', + 'Variable Name for Filename', + ], + + mod(DBM) { + const { Bot, Actions } = DBM; + const fs = require('fs'); + const path = require('path'); + DBM.Events = DBM.Events || {}; + const fileWatcher = {}; + DBM.Events.fileWatcher = {}; + fileWatcher.watchers = {}; + + fileWatcher.setupWatchers = function setupWatchers() { + if (!Bot.$evts['File Watcher']) return; + + for (const event of Bot.$evts['File Watcher']) { + try { + if (!event.temp || !event.temp2) return; + const eventName = event.name; + const watchDir = path.resolve(__dirname, '..', event.temp); + const variableName = event.temp2; + + const watcher = fs.watch(watchDir, (eventType, filename) => { + if (eventType === 'rename' && filename) { + const filePath = path.join(watchDir, filename); + fs.stat(filePath, (err, stats) => { + if (!err && stats.isFile()) { + const payload = {}; + payload[variableName] = filename; + Actions.invokeEvent(event, null, payload); + } + }); + } + }); + + fileWatcher.watchers[eventName] = watcher; + console.log(`[File Watcher] Event '${eventName}' has been set up for directory '${watchDir}'.`); + } catch (error) { + console.error(`Event error: ${error}`); + } + } + }; + + const { onReady } = DBM.Bot; + DBM.Bot.onReady = function fileWatcherOnReady(...params) { + fileWatcher.setupWatchers(); + onReady.apply(this, ...params); + }; + }, + }; + From c116fd51bf0f78a0f2cd2c8553de10fe279102f9 Mon Sep 17 00:00:00 2001 From: Jamie <83149767+ParallaxOnline@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:50:29 +0000 Subject: [PATCH 2/2] Update file_watcher_EVT.js --- events/file_watcher_EVT.js | 112 ++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/events/file_watcher_EVT.js b/events/file_watcher_EVT.js index 59277744..d16ff5f3 100644 --- a/events/file_watcher_EVT.js +++ b/events/file_watcher_EVT.js @@ -1,58 +1,58 @@ module.exports = { - name: 'File Watcher', - displayName: 'File Watch Event', - isEvent: true, - - fields: [ - 'Directory to Watch (Relative to bot directory)', - 'Variable Name for Filename', - ], - - mod(DBM) { - const { Bot, Actions } = DBM; - const fs = require('fs'); - const path = require('path'); - DBM.Events = DBM.Events || {}; - const fileWatcher = {}; - DBM.Events.fileWatcher = {}; - fileWatcher.watchers = {}; - - fileWatcher.setupWatchers = function setupWatchers() { - if (!Bot.$evts['File Watcher']) return; - - for (const event of Bot.$evts['File Watcher']) { - try { - if (!event.temp || !event.temp2) return; - const eventName = event.name; - const watchDir = path.resolve(__dirname, '..', event.temp); - const variableName = event.temp2; - - const watcher = fs.watch(watchDir, (eventType, filename) => { - if (eventType === 'rename' && filename) { - const filePath = path.join(watchDir, filename); - fs.stat(filePath, (err, stats) => { - if (!err && stats.isFile()) { - const payload = {}; - payload[variableName] = filename; - Actions.invokeEvent(event, null, payload); - } - }); - } - }); - - fileWatcher.watchers[eventName] = watcher; - console.log(`[File Watcher] Event '${eventName}' has been set up for directory '${watchDir}'.`); - } catch (error) { - console.error(`Event error: ${error}`); - } + name: 'File Watcher', + displayName: 'File Watch Event', + isEvent: true, + + fields: ['Directory to Watch (Relative to bot directory)', 'Variable Name for Filename'], + + mod(DBM) { + const { Bot, Actions } = DBM; + const fs = require('fs'); + const path = require('path'); + DBM.Events = DBM.Events || {}; + const fileWatcher = {}; + DBM.Events.fileWatcher = {}; + fileWatcher.watchers = {}; + + fileWatcher.setupWatchers = function setupWatchers() { + if (!Bot.$evts['File Watcher']) return; + + for (const event of Bot.$evts['File Watcher']) { + try { + if (!event.temp || !event.temp2) return; + const eventName = event.name; + const watchDir = path.resolve(__dirname, '..', event.temp); + const variableName = event.temp2; + + const watcher = fs.watch(watchDir, (eventType, filename) => { + if (eventType === 'rename' && filename) { + const filePath = path.join(watchDir, filename); + fs.stat(filePath, (err, stats) => { + if (err) { + console.error(err); + return; + } + if (stats.isFile()) { + const payload = {}; + payload[variableName] = filename; + Actions.invokeEvent(event, null, payload); + } + }); + } + }); + + fileWatcher.watchers[eventName] = watcher; + console.log(`[File Watcher] Event '${eventName}' has been set up for directory '${watchDir}'.`); + } catch (error) { + console.error(`Event error: ${error}`); } - }; - - const { onReady } = DBM.Bot; - DBM.Bot.onReady = function fileWatcherOnReady(...params) { - fileWatcher.setupWatchers(); - onReady.apply(this, ...params); - }; - }, - }; - + } + }; + + const { onReady } = DBM.Bot; + DBM.Bot.onReady = function fileWatcherOnReady(...params) { + fileWatcher.setupWatchers(); + onReady.apply(this, params); + }; + }, +};