Reputation: 1
const botconfig = require("./botconfig.json");
const tokenfile = require("./token.json");
const Discord = require("discord.js");
const fs = require("fs");
const bot = new Discord.Client({disableEveryone: true});
bot.commands = new Discord.Collection();
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("Couldn't find commands.");
return;
}
jsfile.forEach((f, i) =>{
let props = require(`./commands/${f}`);
console.log(`${f} loaded!`);
bot.commands.set(props.help.name, props);
});
});
bot.on("ready", async () => {
console.log(`${bot.user.username} is online on ${bot.guilds.size} servers!`);
bot.user.setActivity("!help | website.xyz", {type: "WATCHING"});
//bot.user.setGame("on SourceCade!");
});
bot.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let prefix = botconfig.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
let commandfile = bot.commands.get(cmd.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args);
});
bot.login(tokenfile.token);
This is my index folder, when I try to run the bot I get this error. I've tried everything but I'm not the best at this as I am still learning so any help would be greatly appreciated! Thanks
C:\Users\Luca\Desktop\DiscordJS\RedHQ-Bot\index.js:21
bot.commands.set(props.help.name, props);
^
TypeError: Cannot read property 'name' of undefined
at jsfile.forEach (C:\Users\Luca\Desktop\DiscordJS\RedHQ-Bot\index.js:21:33)
at Array.forEach (<anonymous>)
at fs.readdir (C:\Users\Luca\Desktop\DiscordJS\RedHQ-Bot\index.js:18:10)
at FSReqWrap.oncomplete (fs.js:135:15)
[nodemon] app crashed - waiting for file changes before starting...
Upvotes: 0
Views: 19789
Reputation: 11
your commands handler does not have
exports.conf = {
aliases: ['Stuff', 'AlsoStuff']
};
exports.help = {
name: "More Stuff", description: "SillyStuff.", usage: ".SeriousStuff"
}
This is why you are returned the name not found error. Because in the code where it is looking, it doesn't exist.
Upvotes: 1
Reputation: 2705
When you are accessing a property's property, you should add check for the first property.
Your props.help
is undefined
. undefined
is not a Javascript object and name
property lookup on undefined will fail.
If you try looking up the properties of undefined
, you will get a TypeEror
Object.getOwnPropertyNames(undefined)
// prints 'Uncaught TypeError: Cannot convert undefined or null to object'
Especially since you are reading multiple files and accessing the fields in those files, you should take care of the case where the file is not in the right format, or file was not read correctly, etc.
jsfile.forEach((f, i) =>{
let props = require(`./commands/${f}`);
console.log(`${f} loaded!`);
if (props.help && props.help.name) {
bot.commands.set(props.help.name, props);
} else {
console.error(`file ${f} does not have .help or .help.name property!`);
});
Upvotes: 0
Reputation: 87
There is a problem with props.help
because it returns undefined
(the key "help" does not exist in props) as the error states. You should probably check exactly what you are assigning to props.
Upvotes: 0