Search code examples
discord.js

SyntaxError: Invalid or unexpected token. Never got this error before


I was trying to run my discord bot when I got an error saying "SyntaxError: Invalid or unexpected token". I never got this before. I was able to narrow it down to my 01-regCmds.js (register commands) file.

I searched it up and saw that "SyntaxError: Unexpected token" means I used an invalid symbol, so I tried to look for when I used an invalid symbol, but I had no idea where it could be.

I was able to deduct that this line of code (const localCmds = getLocalCmds();) was the issue. When I commented it out, it ran past const appCmds = await getAppCmds(c, testserver);

Error:

/src/cmd/misc/.DS_Store:1



SyntaxError: Invalid or unexpected token
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1175:20)
    at Module._compile (node:internal/modules/cjs/loader:1219:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1137:19)
    at require (node:internal/modules/helpers:121:18)
    at module.exports (/src/utils/getLocalCmds.js:13:28)
    at module.exports (/src/events/interactionCreate/handleCmds.js:7:23)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21)

Node.js v20.2.0

01-regCmds.js file:

const { testServer } = require('../../../config.json');
const cmdsDiff = require('../../utils/cmdsDiff');
const getAppCmds = require('../../utils/getAppCmds');
const getLocalCmds = require('../../utils/getLocalCmds');

module.exports = async (c) => {
    try {
        console.log('working!')
        //const localCmds = getLocalCmds(); <- problem child
        const appCmds = await getAppCmds(c, testServer);
        console.log('working!')
        for (const localCmd of localCmds) {
            const { name, description, options } = localCmd;

            const cmdExist = await appCmds.cache.find((cmd) => cmd.name === name);
            console.log('working!')
            if (cmdExist) {
                if (localCmd.deleted) {
                    await appCmds.delete(cmdExist.id)
                    console.log(`🗑️ Banished "${name}"!`)
                    console.log('working!')
                    continue;
                }

                if (cmdsDiff(cmdExist, localCmd)) {
                    await appCmds.edit(cmdExist.id, {
                        description,
                        options,
                    });

                    console.log(`📝 Edited command "${name}"`)
                    console.log('working!')
                }
            } else {
                if (localCmd.deleted) {
                    console.log(`⏭️ Skipping command "${name}" - Set to delete`)
                    console.log('working!')
                    continue;
                }

                await appCmds.create({
                    name,
                    description,
                    options,
                });

                console.log(`👍 Registered command "${name}"`)
                console.log('working!')
            }
        }
    } catch (error) {
        console.log(`${error}`);
    }
};

getLocalCmds.js file:

const getAllFiles = require('./getAllFiles');

module.exports = (exc = []) => {
    let localCmds = [];

    const cmdCats = getAllFiles(path.join(__dirname, '..', 'cmd'), true)

    for (const cmdCat of cmdCats) {
        const cmdFiles = getAllFiles(cmdCat, false)

        for (const cmdFile of cmdFiles) {
            const cmdObj = require(cmdFile);

            if (exc.includes(cmdObj.name)) {
                continue;
            }

            localCmds.push(cmdObj);
        }
    }

    return localCmds;
}

Solution

  • Thanks to jasonharper's comment, I added an if statement that looks for if the name of the cmdFile ends with .js [ah the joys of .endsWith()] and then runs require(cmdFile). Boom! Perfectly working!