AnIdiotsGuide/discordjs-bot-guide

TypeError: Cannot read property 'run' of undefined

jerkdavi opened this issue · 1 comments

Hi,

I'm quite new to the discord.js programming, and to the github repository, so please be gentle.
I'm working on my bot, and I'm currently working on 'creating a command handler'.

The first problem that I have is that every time I input the command I want, the function else runs, never function if.

The second problem that I have, that is probably an extension of the first problem, is that I put 'cmd.run(bot, message, args);' part under the else command, just to test it out, and I every time I input the command; I get the error from the title of the video.

What am I doing wrong?
Following is my code.

var Discord = require('discord.js');
var bot = new Discord.Client();
var fs = require('fs');

bot.commands = new Discord.Collection();
fs.readdir('./commands/', (err, files) => {
	if(err){ console.error(err); }
	
	var jsfiles = files.filter(f => f.split('.').pop() === 'js');
	if(jsfiles.length <= 0) { return console.log('No commands found!'); }
	else { console.log(jsfiles.length + ' commands found!'); }
	
	jsfiles.forEach((f, i) => {
		var cmds = require(`./commands/${f}`);
		console.log(`Command ${f} loading...`);
		bot.commands.set(cmds.config.command, cmds);
	});
});
var prefix = process.env.prefix;

bot.on('message', function(message){
	var input = message.content.toUpperCase();
	
	if(input.startsWith(prefix)){
		var cont = input.slice(prefix.length).split(' ');
		console.log('input: '+input);
		console.log('prefix: '+prefix);
		console.log('cont: '+cont);
		var args = cont.slice(1);
		console.log('args: '+args);

		var cmd = bot.commands.get(cont[0]);
		console.log('cont[0]: '+cont[0]);
		if(cmd) {
			cmd.run(bot, message, args);
			console.log('If passed!');
		}
		else{
			cmd.run(bot, message, args);
			console.log('Else passed!');
		}
	}
	else{
		console.log('Wrong prefix!');
		return;
	}
}

Any help would be much appreciated. :)

With kind regards,
David

For tech support please join our discord server, as this github is related to questions regarding the guide, not how to write your bots.