-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheventCache.js
More file actions
88 lines (67 loc) · 2.92 KB
/
eventCache.js
File metadata and controls
88 lines (67 loc) · 2.92 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require('fs').promises;
const path = require('path');
class EventCache {
constructor() {
this.eventFiles = new Map();
this.loadedEvents = new Map();
}
async loadEventFile(filePath) {
if (this.eventFiles.has(filePath)) {
return this.eventFiles.get(filePath);
}
try {
const eventModule = require(filePath);
this.eventFiles.set(filePath, eventModule);
return eventModule;
} catch (error) {
console.error('Erreur chargement event:', error);
throw error;
}
}
async attachEventsToClient(client) {
try {
client.removeAllListeners();
const eventsDir = path.resolve(__dirname, 'events');
const subDirs = await fs.readdir(eventsDir);
let totalEvents = 0;
for (const dir of subDirs) {
const dirPath = path.join(eventsDir, dir);
const stat = await fs.stat(dirPath);
if (stat.isDirectory()) {
const eventFiles = (await fs.readdir(dirPath)).filter(f => f.endsWith(".js"));
for (const file of eventFiles) {
const filePath = path.join(dirPath, file);
const eventName = file.split('.')[0];
try {
const evt = await this.loadEventFile(filePath);
const eventHandler = evt.run || evt.execute;
const eventActualName = evt.name || eventName;
if (!eventHandler) {
continue;
}
if (evt.once) {
client.once(eventActualName, (...args) => eventHandler(...args, client));
} else {
client.on(eventActualName, (...args) => eventHandler(...args, client));
}
totalEvents++;
} catch (error) {
console.error(` ❌ Erreur ${eventName}:`, error.message);
}
}
}
}
return totalEvents;
} catch (error) {
console.error('❌ Erreur chargement événements:', error.message);
return 0;
}
}
clearCache() {
for (const [filePath] of this.eventFiles) {
delete require.cache[require.resolve(filePath)];
}
this.eventFiles.clear();
}
}
module.exports = new EventCache();