Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeError:无法读取属性';forEach&x27;未定义discord.js的_Discord.js - Fatal编程技术网

TypeError:无法读取属性';forEach&x27;未定义discord.js的

TypeError:无法读取属性';forEach&x27;未定义discord.js的,discord.js,Discord.js,我遇到了以下问题:TypeError:在添加别名后,无法读取discord bot上未定义的属性“forEach”。在我给它添加别名之前它就工作了,所以我假设问题就在附近。我似乎找不到问题的根源,因此任何帮助都将不胜感激 代码: const botconfig = require("./botconfig.json"); const Discord = require("discord.js"); const fs = require("fs"); const bot = new Discord

我遇到了以下问题:TypeError:在添加别名后,无法读取discord bot上未定义的属性“forEach”。在我给它添加别名之前它就工作了,所以我假设问题就在附近。我似乎找不到问题的根源,因此任何帮助都将不胜感激

代码:

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const fs = require("fs");
const bot = new Discord.Client({disableEveryone: true});
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
let profile = require("./profiles.json");

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);
        props.help.aliases.forEach(alias => {
            bot.aliases.set(alias, props.help.name);
        });
    });
});



bot.on("ready", async () => {
    console.log(`${bot.user.username} is online`);
    bot.user.setActivity("$ Made by xkillerx15");
});

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)) || bot.commands.get(cmd.slice(prefix.length));
    if(commandfile) commandfile.run(bot,message,args);

    if(!profile[message.author.id]){
        profile[message.author.id] = {
            coins: 0
        };
    }

    let coinAmt = Math.floor(Math.random() * 15) + 1;
    let baseAmt = Math.floor(Math.random() * 15) + 1;
    if(coinAmt === baseAmt){
        profile[message.author.id] = {
            coins: profile[message.author.id].coins + coinAmt
        };
        fs.writeFile("./profiles.json", JSON.stringify(profile), (err) => {
            if(err) console.log(err)
        });
    }
});

bot.login(botconfig.token);

const botconfig=require(“./botconfig.json”);
const Discord=require(“Discord.js”);
常数fs=要求(“fs”);
const bot=new Discord.Client({disableEveryone:true});
bot.commands=new Discord.Collection();
bot.alias=new Discord.Collection();
let profile=require(“./profiles.json”);
fs.readdir(“./commands/”,(错误,文件)=>{
if(err)console.log(err);
让jsfile=files.filter(f=>f.split(“.”.pop()==“js”)
if(jsfile.length){
让props=require(`./commands/${f}`);
log(`${f}已加载!`);
bot.commands.set(props.help.name,props);
props.help.alias.forEach(别名=>{
bot.aliases.set(别名、props.help.name);
});
});
});
on(“就绪”,异步()=>{
log(`${bot.user.username}处于联机状态`);
bot.user.setActivity(“$makebyxkillerx15”);
});
on(“消息”,异步消息=>{
if(message.author.bot)返回;
if(message.channel.type==“dm”)返回;
让prefix=botconfig.prefix;
让messageArray=message.content.split(“”);
设cmd=messageArray[0];
设args=messageArray.slice(1);
让commandfile=bot.commands.get(cmd.slice(prefix.length))| | bot.commands.get(cmd.slice(prefix.length));
if(commandfile)commandfile.run(bot、message、args);
如果(!profile[message.author.id]){
配置文件[message.author.id]={
硬币:0
};
}
让coinant=Math.floor(Math.random()*15)+1;
设baseAmt=Math.floor(Math.random()*15)+1;
如果(货币金额===基本金额){
配置文件[message.author.id]={
coins:profile[message.author.id]。coins+coinAmt
};
fs.writeFile(“./profiles.json”,json.stringify(profile),(err)=>{
if(err)console.log(err)
});
}
});
bot.login(botconfig.token);
准确错误:

TypeError: Cannot read property 'forEach' of undefined
    at jsfile.forEach (C:\Users\Jordy\Desktop\jbot\index.js:23:28)
    at Array.forEach (<anonymous>)
    at fs.readdir (C:\Users\Jordy\Desktop\jbot\index.js:19:12)
    at FSReqWrap.args [as oncomplete] (fs.js:140:20)
TypeError:无法读取未定义的属性“forEach”
在jsfile.forEach(C:\Users\Jordy\Desktop\jbot\index.js:23:28)
在Array.forEach()处
在fs.readdir(C:\Users\Jordy\Desktop\jbot\index.js:19:12)
在FSReqWrap.args[as oncomplete](fs.js:140:20)

您的问题是
别名
未定义的
,这意味着它不是一个对象,因此您不能使用
forEach

有一种可能性:

  • 命令文件应包含
    别名
    帮助
    对象。
    所以应该是这样的

    exports.help = {
    aliases:[...props]
    }
    

  • 问题是“命令”是一个文件夹,而不是一个文件。如果我添加别名:“somealias”;对于每个文件,它也不起作用。我看了一段关于别名的youtube视频,我做的和youtube完全一样,但出于某种原因,我的文件仍然会出错。你是否尝试将别名声明为数组?因为这是您的代码所说的
    jsfile.forEach((f,i)=>{
    我们将检查每个js文件`let props=require(
    /commands/${f}
    )`我们将导入匹配的文件
    props.help.alias
    这里props应该是一个对象,应该包含一个名为help的属性,它应该是一个对象,它包含一个名为alias的属性,应该是一个数组您至少没有为一个命令定义别名。如果您想继续使用此代码,必须将别名声明为数组每个命令中的数组(如果没有别名,请将其保留为空)。如果不想麻烦更改所有命令文件,请将
    别名
    变量定义为
    props.help.alias | |[]
    ,如果未定义
    props.help.alias
    ,则该变量将使用空数组,并对其进行迭代。