Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
Error detection before code execution starts in JavaScript

Is it possible to detect errors before the code starts to run?
I have a Discord bot, and I would like the command handler that prints all loaded commands to the console to show the status for errors in advance. Command handler at the moment:

const { readdirSync } = require("fs");
const ascii = require("ascii-table");
const list = new ascii('Commands');
list.setHeading('Command', 'Loaded');
module.exports = (bot) => {
    let commands = readdirSync(`./commands/`).filter(file => file.endsWith(".js"));
    for (let file of commands) {
        let command = require(`../commands/${file}`);
        if (command.name) {
            bot.commands.set(command.name, command);
            list.addRow(file, '✅');
        } else {
            list.addRow(file, '❌');
            continue;
        }
    }
    console.log(list.toString());
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can simply use the try and catch statements of Javascript. In this way, if an error occurs still it will not break your code or bot. It will continue running without any problem.

If you don't want to show anything and want to continue running the bot:

try {
  const { readdirSync } = require("fs");
  const ascii = require("ascii-table");
  const list = new ascii("Commands");
  list.setHeading("Command", "Loaded");
  module.exports = (bot) => {
    let commands = readdirSync(`./commands/`).filter((file) =>
      file.endsWith(".js")
    );
    for (let file of commands) {
      let command = require(`../commands/${file}`);
      if (command.name) {
        bot.commands.set(command.name, command);
        list.addRow(file, "✅");
      } else {
        list.addRow(file, "❌");
        continue;
      }
    }
    console.log(list.toString());
  };
} catch (e) {
  // Don't do anything
}

Incase you want to print out the error to the console and continue running the bot. Then you can add a console.log() under the catch statement:

try {
  const { readdirSync } = require("fs");
  const ascii = require("ascii-table");
  const list = new ascii("Commands");
  list.setHeading("Command", "Loaded");
  module.exports = (bot) => {
    let commands = readdirSync(`./commands/`).filter((file) =>
      file.endsWith(".js")
    );
    for (let file of commands) {
      let command = require(`../commands/${file}`);
      if (command.name) {
        bot.commands.set(command.name, command);
        list.addRow(file, "✅");
      } else {
        list.addRow(file, "❌");
        continue;
      }
    }
    console.log(list.toString());
  };
} catch (e) {
  console.log(e)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda