NebulaViper
NebulaViper

Reputation: 1

Error: Cannot find module './commands/${f}1'

I've been trying to get my bot to come online for hours but I keep getting the same error.

Here's the error:

Error: Cannot find module './commands/${f}1'
Require stack:
- C:\Users\Apskaita\Desktop\Viper bot\main.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at C:\Users\Apskaita\Desktop\Viper bot\main.js:20:22
    at Array.forEach (<anonymous>)
    at C:\Users\Apskaita\Desktop\Viper bot\main.js:19:12
    at FSReqCallback.oncomplete (fs.js:156:23) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\Apskaita\\Desktop\\Viper bot\\main.js' ]
}

Here's my code for my main.js:

const Discord = require('discord.js');
const client = new Discord.Client();
const botconfig = require('./botconfig.json');
const fs = require("fs");

client.commands = new Discord.Collection();
client.alieses = new Discord.Collection();

// READ COMMANDS FOLDER
fs.readdir("./commands/", (err, files) => {
    if(err) console.log(err);

    let jsfile = files.filter(f => f.split(".").pop() === "js")
    if(jsfile.length <= 0) {
        console.log("couldin't file any commands!");
        return;
    }

    jsfile.forEach((f) => {
        let preops = require("./commands/${f}1")
        console.log('${f} loaded!');
        bot.commands.set(props.help.name, props);
        props.help.aliases.forEach(alias => {
            bot.aliases.set(alias, props.help.name);
        })
    })
})

// BOT ONLINE MESSAGE AND ACTIVITY MESSAGE
client.on('ready', async () => {
    console.log('${bot.user.username} is online on ${bot.guilds.size} servers!')
    bot.user.setActivity('with ${bot.guilds.size} servers!');
})

client.on('message', async message => {

    // CHECK CHANNEL TYPE
    if(message.channel.type === "dm") return;
    if(message.author.bot) return;

    // SET PREFIX
    let prefix = botconfig.prefix;

    // CHECK PREFIX, DEFINE ARGS & COMMAND
    if(message.content.startsWith(prefix)) return;
    let args = message.content.slice(prefix.length).trim().split(/ +/g);
    let cmd;
    cmd = args.shift().toLowerCase();
    let command;
    let commandfile = bot.commands.get(cmd.slice(prefix.length));
    if(commandfile) commandfile.run(bot, message, args);

    //RUN COMMANDS
    if(bot.commands.has(cmd)) {
        command = bot.commands.get(cmd);
    } else if (bot.aliases.has(cmd)) {
        command = bot.commands.get(bot.aliases.get(cmd));
    }
    try {
        command.run(bot, message, args);
    } catch (e) {
        return;
    }

client.login(botconfig.token)})

My ping.js:

module.exports.run = async (bot, message, args) => {
    const m = await message.channel.send("ping!");
    m.edit("Pong! ${m.createTimestamp - message.createdTimestamp}ms");
 
}

module.exports.help = {
    name: "",
    aliases: ["p"]
}

These errors are very confusing and I don't really know how to fix them so please can someone fix them?

I am very new to JavaScript so I don't have a clue what I'm typing... I copied the code from a tutorial so I don't what's going on.

Upvotes: 0

Views: 786

Answers (2)

ations
ations

Reputation: 101

It means that the path is incorrect. First of all, be sure to use good quotes as someone said before. Then, are you sure that there is a 1 at the end of your files' names ? It is what you ask ${f}1.

Upvotes: 0

BENARD Patrick
BENARD Patrick

Reputation: 30975

You're not using the correct quotes, you're using " instead of `

Replace

jsfile.forEach((f) => {
    let preops = require("./commands/${f}1")
    console.log('${f} loaded!');
    bot.commands.set(props.help.name, props);
    props.help.aliases.forEach(alias => {
        bot.aliases.set(alias, props.help.name);
    })
})

By

jsfile.forEach((f) => {
    let preops = require(`./commands/${f}1`)
    console.log(`${f} loaded!`);
    bot.commands.set(props.help.name, props);
    props.help.aliases.forEach(alias => {
        bot.aliases.set(alias, props.help.name);
    })
})

Upvotes: 1

Related Questions