admin管理员组文章数量:1391981
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
3 Answers
Reset to default 1your 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!`);
});
本文标签: javascriptdiscordjs TypeError Cannot read property 39name39 of undefinedStack Overflow
版权声明:本文标题:javascript - discord.js TypeError: Cannot read property 'name' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744718600a2621556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论