Reputation: 13
I'm creating bot with discord.js. This is my code:
var Discord = require('discord.js');
const swarmConfig = require('./swarmConfig.json');
var swarmbot = new Discord.Client();
swarmbot.login('myToken');
fs.readdir('./commands/', (err, files) =>{
if(err) console.log(err);
var jsFiles = files.filter(f => f.split('.').pop() === 'js');
if(jsFiles.length <=0){
console.log('Cant find commands');
return;
}
jsFiles.forEach((f, i) =>{
let fileGet = require(`./commands/${f}`);
console.log(`${f} loaded!`);
swarmbot.commands.set(fileGet.help.name, fileGet);
});
});
swarmbot.on("message", async message =>{
if (message.author.swarmbot)
return;
var prefix = swarmConfig.prefix;
var command = messageArray[0];
var arguments = messageArray.slice(1);
var commands = swarmbot.commands.get(command.slice(prefix.length));
if(commands)
commands.run(swarmbot, message, arguments);
});
But if I try to start it, I get this error:
C:\Users\purul\Desktop\swarm-dsbot\swarm.js:25
swarmbot.commands.set(fileGet.help.name, fileGet);
^
TypeError: Cannot read property 'set' of undefined
at jsFiles.forEach (C:\Users\purul\Desktop\swarm-dsbot\swarm.js:25:23)
at Array.forEach (<anonymous>)
at fs.readdir (C:\Users\purul\Desktop\swarm-dsbot\swarm.js:22:11)
at FSReqWrap.oncomplete (fs.js:141:20)
I'm still learning JavaScript, Thanks to everyone who will help with this.
Upvotes: 1
Views: 4489
Reputation: 2145
If we look in the documentation for Discord.js, there is no such object called 'commands'. Link: https://discord.js.org/#/docs/main/stable/search?q=commands I'm guessing that you're loading them from a file, so you should maybe create a js file that has the commands, and load them to you main file.
Reference for possible solution: https://stackoverflow.com/a/51333277/7363404 (you should also check the question)
Upvotes: 2