Search code examples
javascriptnode.jssyntaxdiscorddiscord.js

SyntaxError: Unexpected token 'const'


how can i solve my error in line 16 witch is syntax error can any one help me to fix it this is 'command handler' for discord js

const { Events } = require("../Validation/Events");
const { promisify } = require("util");
const { glob } = require("glob");
const PG = promisify(glob);
const Ascii = require("ascii-table");

module.exports = async (client) => {
    const Table = new Ascii("Events Loaded");

    (await PG(`${process.cwd()}/Events/*/*.js`)).map(async (file) => {
        const event = require(file);

        if (event.name) {
            if(!Events.includes(event.name))
            const L = file.split("/");
            return Table.addRow(`${event.name || "MISSING"}`, `⛔ Event name is either invalid or missing ${L[6] + `/` + L[7]}`);
        }

        if(event.once)
        client.once(event.name, (...args) => event.execute(...args, client));
        else
        client.on(event.name, (...args) => event.execute(...args, client));
        
        await Table.addRow(event.name, "✔️ SUCCESSFUL")
    });
    
    console.log(Table.toString());
}```

Solution

  • You are saying

    if(!Events.includes(event.name))
    const L = file.split("/");
    

    Which doesn't make sense because const is block scoped. You forgot {}

    if (event.name) {
       if(!Events.includes(event.name)) {
           const L = file.split("/");
           return Table.addRow(`${event.name || "MISSING"}`, `⛔ Event name is either invalid or missing ${L[6] + `/` + L[7]}`);
        }
    }