forked from ServerRelay/CSR
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilewatch.js
More file actions
51 lines (50 loc) · 1.2 KB
/
filewatch.js
File metadata and controls
51 lines (50 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const fs = require('fs');
const path = require('path');
class FileWatch {
/**
*
* @param {import('./bot')} client
*/
constructor() {
this.watching = new Map();
}
/**
*
* @param {string} folder
* @param {(event:string,file:string)=>void} callback
*/
watch(folder, callback) {
let fsWait = false;
if (!this.watching.has(folder)) {
this.watching.set(folder, { last_changed: Date.now() });
}
const fldr = this.watching.get(folder);
fs.watch(path.resolve(folder),{recursive:true}, (event, file) => {
if (fsWait) return;
fldr.last_changed = Date.now();
this.watching.set(folder, fldr);
callback(event, file);
fsWait = true
setTimeout(() => {
fsWait = false;
}, 1000);
});
}
}
function reloadCommands(client, folder) {
folder = path.resolve(folder);
const commandFiles = fs
.readdirSync(folder)
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
delete require.cache[require.resolve(`${folder}/${file}`)];
try {
const command = require(`${folder}/${file}`);
client.commands.delete(command.help.name);
client.commands.set(command.help.name, command);
} catch (err) {
console.log(err);
}
}
}
module.exports = FileWatch;