#Handler Exported Function Not being registered globaly
22 messages · Page 1 of 1 (latest)
Index JS:
const { Client, GatewayIntentBits } = require('discord.js');
const fs = require('fs');
const path = require('path');
const config = require('./config.json');
const handlersDir = path.join(__dirname, 'handlers');
const commandsDir = path.join(__dirname, 'commands');
const handlers = [];
fs.readdir(handlersDir, (err, files) => {
if (err) {
handler.log(`Error reading directory: ${err}`, 3);
return;
}
files.forEach((file) => {
const handlerPath = path.join(handlersDir, file);
const handler = require(handlerPath);
handlers.push(handler);
});
});
const client = new Client({
intents: [Object.values(GatewayIntentBits).reduce((acc, value) => acc | value, 0)],
});
client.on('ready', () => {
handler.log(`Logged in as ${client.user.tag}`, 1);
});
client.on('messageCreate', (message) => {
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
handler.log(command, 2);
});
client.login(config.token);
Logging Handler: (In handler/loggingHandler.js)
const fs = require('fs');
const path = require('path');
const logColors = {
0: '\x1b[34m', // Blue (startup/shutdown)
1: '\x1b[32m', // Green (normal running/function)
2: '\x1b[33m', // Orange (warning)
3: '\x1b[31m', // Red (error/crash)
};
const logDir = path.join(__dirname, 'logs');
const maxLogs = 10;
// Ensure log directory exists
// if (!fs.existsSync(logDir)) {
// fs.mkdirSync(logDir);
// }
// // Get existing log files
// const existingLogs = fs.readdirSync(logDir);
// // Rename existing log files with the current date
// existingLogs.forEach((logFile) => {
// const oldPath = path.join(logDir, logFile);
// const newPath = path.join(logDir, `${getFormattedDate()}_${logFile}`);
// fs.renameSync(oldPath, newPath);
// });
// // Remove excess log files if necessary
// if (existingLogs.length >= maxLogs) {
// const logFilesToDelete = existingLogs.slice(0, existingLogs.length - maxLogs + 1);
// logFilesToDelete.forEach((logFile) => {
// const logFilePath = path.join(logDir, logFile);
// fs.unlinkSync(logFilePath);
// });
// }
// const logFilePath = path.join(logDir, `${getFormattedDate()}_log.txt`);
function log(message, color) {
const formattedDate = getFormattedDate();
const logMessage = `[${formattedDate}] ${logColors[color]}•\x1b[0m ${message}`;
// Log to console
console.log(logMessage);
// // Log to file
// fs.appendFile(logFilePath, `${logMessage}\n`, (error) => {
// if (error) {
// console.error('Error writing to log file:', error);
// }
// });
}
function getFormattedDate() {
const currentDate = new Date();
return currentDate.toLocaleString();
}
module.exports = { log };
you push all handlers into a handlers array. and then what?
you're not doing anything with it
It should be where you do
handlers.log()
but doing
handlers.log - Should be global
or handler.log - is not global
Can't find the function
because you're pushing it into an array which is indexed by numbers
you're trying to array[log] which doesn't exist
my god it would be handlers[0] wouldnt it T-T
yes, which is also not a really good practice either
I can see that, Is there any better way of doing this?
i would do something lke
with an object rather than an array
{ logger: log }, maybe something like that
How about not importing your logger dynamically like that since you want to use it statically anyway
Theres like 10 other handlers..
it's not just the logger
A logger isn’t a handler though…
it handles logs.
A handler exports nothing, it only gets passed something once and handles stuff on its own