Hello so I wanted to refresh my events, I thought i knew how but i guess i didn't. It enters the forEach loop although it errors out at the part where it deletes the require cache
const fs = require('fs');
const path = require('path');
function reloadEvent() {
try {
const modelsPath = path.join(__dirname, '../src/events');
const modelFiles = fs.readdirSync(modelsPath).filter(file => file.endsWith('.js'));
modelFiles.forEach((file) => {
try {
console.log("Entered for each");
const filePath = path.join(modelsPath, file);
delete require.cache[require.resolve(filePath)];
console.log(`Deleting cache for: ${file}`);
const newEvent = require(filePath);
console.log(`Requiring event: ${file}`);
if (newEvent.once) {
client.once(newEvent.name, (...args) => newEvent.execute(...args));
} else {
client.on(newEvent.name, (...args) => newEvent.execute(...args));
}
console.log(`Reloaded event: ${newEvent.name}`);
} catch (error) {
console.error(`Failed to reload event: ${file}`, error);
}
})
console.log('events reloaded');
} catch (error) {
console.error(`Error refreshing events: ${error}`);
}
}
module.exports = reloadEvent;
Hello so I wanted to refresh my events, I thought i knew how but i guess i didn't. It enters the forEach loop although it errors out at the part where it deletes the require cache
const fs = require('fs');
const path = require('path');
function reloadEvent() {
try {
const modelsPath = path.join(__dirname, '../src/events');
const modelFiles = fs.readdirSync(modelsPath).filter(file => file.endsWith('.js'));
modelFiles.forEach((file) => {
try {
console.log("Entered for each");
const filePath = path.join(modelsPath, file);
delete require.cache[require.resolve(filePath)];
console.log(`Deleting cache for: ${file}`);
const newEvent = require(filePath);
console.log(`Requiring event: ${file}`);
if (newEvent.once) {
client.once(newEvent.name, (...args) => newEvent.execute(...args));
} else {
client.on(newEvent.name, (...args) => newEvent.execute(...args));
}
console.log(`Reloaded event: ${newEvent.name}`);
} catch (error) {
console.error(`Failed to reload event: ${file}`, error);
}
})
console.log('events reloaded');
} catch (error) {
console.error(`Error refreshing events: ${error}`);
}
}
module.exports = reloadEvent;
If there is an actual way of refreshing discord events, please tell me. I haven't seen anything in the guides
Share Improve this question asked Mar 17 at 19:27 2milliongunguy2milliongunguy 212 bronze badges1 Answer
Reset to default 0You can try for loop instead of forEach:
for (const file of modelFiles) {
const filePath = path.join(modelsPath, file);
delete require.cache[require.resolve(filePath)];
console.log(`Deleting cache for: ${file}`);
const newEvent = require(filePath);
if (newEvent.once) {
client.once(newEvent.name, (...args) =>
newEvent.execute(...args));
} else {
client.on(newEvent.name, (...args) => newEvent.execute(...args));
}
}