Abhijeet Sharma
Abhijeet Sharma

Reputation: 1

Module not found error using discord.js

My node application does not run when i run node index.js
Here are the files I have:

File index.js:

const command = require('discord.js-commando')
const bot = new commando.Client();

bot.registery.registerGroup('random', 'Random');
bot.register.registerDefaults();
bot.registery.regusterCommandsIn(__dirname + "/commands")

bot.login('zzzzz');

And the file random.js:

const commando = require('discord.js-commando')

class DiceRollCommand extends commando.Command {
   constructor(client) {
       super(client, {
            name: 'roll',
            group: 'random',
            memberName: 'roll',
            description: 'Rolls a die'
        });
    }

    async run(message, args) {
        var roll = Math.floor(Math.random() * 6) + 1;
        message.reply("You rolled a die") + roll);
    }
}

module.exports = DiceRollCommand;

This is the stacktrace of the error:

Error: Cannot find module 'C:\Users\dange\Desktop\bots\js\commands\random'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

Upvotes: 0

Views: 6584

Answers (1)

William Fleming
William Fleming

Reputation: 466

So I think this is possibly a combination of typos and directory structure. Try making the following changes:

const commando = require('discord.js-commando')
const bot = new commando.Client();

// Typo in the following: should be, bot.registry
bot.registry.registerGroup('random', 'Random');
bot.registry.registerDefaults();
// Typo Here: should be, registerCommandsIn
bot.registry.registerCommandsIn(__dirname + "/commands")


bot.login('zzzzz');

Your directory structure should look something like the following. registerCommandsIn loads all commands in a given directory.

/commands/random.js
/index.js

And you should be sure to be running index.js with the harmony flag and with no less than Node 7.0.

node index.js --harmony

Also, found this, on line 17 of random.js

message.reply("You rolled a die") + roll);
// I assume you want to send the roll
message.reply("You rolled a die" + roll);

Upvotes: 1

Related Questions