TypeError:无法读取未定义的属性“ set”

时间:2020-04-26 21:41:48

标签: bots discord discord.js

我正在使用 discord.js v12.2.0 库创建一个Discord机器人。在我的文件 Command.js 中,我有以下代码:

const { readdirSync } = require("fs")

module.exports = (Bot) => {
    const load = dirs => {
        const Commands = readdirSync(`./SRC/Commands/${dirs}/`).filter(d => d.endsWith('.js'));
        for (let file of Commands) {
            let pull = require(`../Commands/${dirs}/${file}`);
            Bot.Commands.set(pull.config.name, pull);
            if (pull.config.aliases) pull.config.aliases.forEach(a => Bot.aliases.set(a, pull.config.name));
          };
        };
        ["Miscellaneous", "Moderation", "Owner"].forEach(x => load(x));
};

这是我的 Index.js 文件:

const { Client, Collection } = require("discord.js");
const { Token } = require("./Config.json");
const Bot = new Client();

["aliases", "commands"].forEach(x => Bot[x] = new Collection());
["console", "command", "event"].forEach(x => require(`./SRC/Handlers/${x}`)(Bot));

Bot.login(Token);

我从运行此代码中得到的错误是 TypeError:无法读取未定义的属性“ set”

这是我的Project Structure

1 个答案:

答案 0 :(得分:0)

您已将Bot.Commands初始化为Discord.Collection()吗?

index.js或任何主文件

const Discord = require("discord.js");
const Bot = new Discord.Client();
Bot.Commands = new Discord.Collection();

//or
const { Client, Collection } = require("discord.js");
const Bot = new Client();
Bot.Commands = new Collection();

我建议您使Bot和命令小写,因为这是js中的惯例,并且可以帮助其他人更好地理解。