javascript - discord.js TypeError: Cannot read property 'name' of undefined - Stack Overflow

const botconfig = require(".botconfig.json");const tokenfile = require(".token.json&q

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});
botmands = new Discord.Collection();

fs.readdir("./mands/", (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 mands.");
    return;
  }

  jsfile.forEach((f, i) =>{
    let props = require(`./mands/${f}`);
    console.log(`${f} loaded!`);
    botmands.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 mandfile = botmands.get(cmd.slice(prefix.length));
  if(mandfile) mandfile.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
    botmands.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.onplete (fs.js:135:15)
[nodemon] app crashed - waiting for file changes before starting...
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.mands = new Discord.Collection();

fs.readdir("./mands/", (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 mands.");
    return;
  }

  jsfile.forEach((f, i) =>{
    let props = require(`./mands/${f}`);
    console.log(`${f} loaded!`);
    bot.mands.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 mandfile = bot.mands.get(cmd.slice(prefix.length));
  if(mandfile) mandfile.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.mands.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.onplete (fs.js:135:15)
[nodemon] app crashed - waiting for file changes before starting...
Share Improve this question edited Mar 31, 2018 at 17:35 Tanveer Badar 5,5392 gold badges29 silver badges34 bronze badges asked Mar 31, 2018 at 16:01 RessiiRessii 12 gold badges2 silver badges4 bronze badges 2
  • How does a mand file look like? – Jonas Wilms Commented Mar 31, 2018 at 16:06
  • It means that ur prop object doesnt has help key in it – sridhar.. Commented Mar 31, 2018 at 16:07
Add a ment  | 

3 Answers 3

Reset to default 1

your mands 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.

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.

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(`./mands/${f}`);
  console.log(`${f} loaded!`);
  if (props.help && props.help.name) {
    bot.mands.set(props.help.name, props);
  } else {
    console.error(`file ${f} does not have .help or .help.name property!`);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744718601a4589771.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信