How do I create a Logger for every event?

I'm trying to create a logger for every event I cover, I alredy did it in the past but it was a bit messy. I had many lines of code in the events files. I was wondering if there was a better and cleaner way of doing so. I thought I (maybe) use a somewhat revisited event handler like the one I'm using:
async function registerEvents(client, dir) {
let files = await fs.readdir(path.join(__dirname, dir));
for (let file of files) {
let stat = await fs.lstat(path.join(__dirname, dir, file));
if (stat.isDirectory()) {
registerEvents(client, path.join(dir, file));
} else {
if (file.endsWith('.js')) {
let eventName = file.substring(0, file.indexOf('.js'));
try {
let eventModule = require(path.join(__dirname, dir, file));
if (eventModule.once) {
client.once(eventName, eventModule.bind(null, client));
} else {
client.on(eventName, eventModule.bind(null, client));
};
} catch(err) {
console.log(err);
};
};
};
};
};
async function registerEvents(client, dir) {
let files = await fs.readdir(path.join(__dirname, dir));
for (let file of files) {
let stat = await fs.lstat(path.join(__dirname, dir, file));
if (stat.isDirectory()) {
registerEvents(client, path.join(dir, file));
} else {
if (file.endsWith('.js')) {
let eventName = file.substring(0, file.indexOf('.js'));
try {
let eventModule = require(path.join(__dirname, dir, file));
if (eventModule.once) {
client.once(eventName, eventModule.bind(null, client));
} else {
client.on(eventName, eventModule.bind(null, client));
};
} catch(err) {
console.log(err);
};
};
};
};
};
But I can't really see how
2 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Klyde
KlydeOP8mo ago
I was thinking about making one big file with every event like
client.on('eventName' ...) {
// logger...
}
client.on('eventName' ...) {
// logger...
}
But I think yours make more sense now that I undestand it better
Want results from more Discord servers?
Add your server