-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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. * Update file_watcher_EVT.js
- Loading branch information
1 parent
3f6dcd0
commit effeebb
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
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); | ||
}; | ||
}, | ||
}; |