Skip to content

Commit

Permalink
Create file_watcher_EVT.js
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ParallaxOnline authored Mar 19, 2024
1 parent 3f6dcd0 commit fb7d1c8
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions events/file_watcher_EVT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module.exports = {
name: 'File Watcher',

Check failure on line 2 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`
displayName: 'File Watch Event',

Check failure on line 3 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`
isEvent: true,

Check failure on line 4 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`

Check failure on line 5 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`
fields: [

Check failure on line 6 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Replace `··fields:·[⏎······'Directory·to·Watch·(Relative·to·bot·directory)',⏎······'Variable·Name·for·Filename',⏎····` with `fields:·['Directory·to·Watch·(Relative·to·bot·directory)',·'Variable·Name·for·Filename'`
'Directory to Watch (Relative to bot directory)',
'Variable Name for Filename',
],

Check failure on line 10 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`
mod(DBM) {

Check failure on line 11 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Replace `····` with `··`
const { Bot, Actions } = DBM;

Check failure on line 12 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`
const fs = require('fs');

Check failure on line 13 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Replace `······` with `····`
const path = require('path');

Check failure on line 14 in events/file_watcher_EVT.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `··`
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);
};
},
};

0 comments on commit fb7d1c8

Please sign in to comment.