#Handler Exported Function Not being registered globaly

22 messages · Page 1 of 1 (latest)

ancient mossBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Explain what exactly your issue is.
  • Not a discord.js issue? Check out #useful-servers.
  • Issue solved? Press the button!
ripe zealot
#

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 };
burnt sigil
#

you push all handlers into a handlers array. and then what?

#

you're not doing anything with it

ripe zealot
burnt sigil
#

because you're pushing it into an array which is indexed by numbers

#

you're trying to array[log] which doesn't exist

ripe zealot
#

my god it would be handlers[0] wouldnt it T-T

burnt sigil
#

yes, which is also not a really good practice either

ripe zealot
#

I can see that, Is there any better way of doing this?

burnt sigil
#

i would do something lke

#

with an object rather than an array

#

{ logger: log }, maybe something like that

wooden vapor
#

How about not importing your logger dynamically like that since you want to use it statically anyway

burnt sigil
#

^

#

Which is even better

ripe zealot
#

it's not just the logger

wooden vapor
#

A logger isn’t a handler though…

ripe zealot
wooden vapor
#

A handler exports nothing, it only gets passed something once and handles stuff on its own