UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“执行”

时间:2021-01-08 07:36:05

标签: javascript node.js discord discord.js

所以我的机器人遇到了问题,每当我尝试使用 module.exports 运行外部命令文件时,错误不断出现:Cannot define property execute of undefiend

问题出在我尝试执行 learn.js 文件的地方。

index.js(查看和 bot.on 消息导致问题出在这里):

require("dotenv").config();

const Discord = require("discord.js");

const token = process.env.BOT_TOKEN;

const db = require("quick.db");

var economy = new db.table("economy");

const bot = new Discord.Client();

const learnHelp = new Discord.MessageEmbed()
    .setTitle(`Learn Commands`)
    .setDescription(`These are all the learn commands`)
    .addFields({ name: `Learn English`, value: `>learn english help` }, { name: `Learn Math`, value: `>learn math help` }, { name: `Learn Geography`, value: `>learn geography help` });

const fetch = require("node-fetch");

const PREFIX = ">";

const fs = require("fs");
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync("/Users/dhruvanchandrupatla/Desktop/schoolhelpbot/commands1/").filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
    const command = require(`/Users/dhruvanchandrupatla/Desktop/schoolhelpbot/commands1/${file}`);

    bot.commands.set(command.name, command);
}

bot.on("guildCreate", (guild) => {
    let defaultChannel = "";
    guild.channels.cache.forEach((channel) => {
        if (channel.type == "text" && defaultChannel == "") {
            if (channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
                defaultChannel = channel;
            }
        }
    });
    defaultChannel.send(`Hello, I am Acadamics Bot and I will help you with all your acadamics needs!!`, {
        embed: {
            title: ":x: Prefix",
            color: 0x2471a3,
            description: "The prefix for all my commands is '>', e.g: '>ping'.",
            fields: [
                {
                    name: ":books: Learn",
                    value: "learn english help, learn math help, learn geography help",
                },
                {
                    name: ":brain: Random",
                    value: "ping, hello",
                },
            ],

            footer: {
                text: "Acadamics Bot created and developed by DandyUltraman28#3186.",
            },
        },
    });
});

bot.on("ready", () => {
    console.log("This bot is ready");
}),
    //Command When User Joins
    bot.on("guildMemberAdd", (member) => {
        const channel = member.guild.channels.cache.find((channel) => channel.name === "general");
        if (!channel) return;

        const joinembed = new Discord.MessageEmbed().setTitle(`A new member just arrived!`).setDescription(`Welcome ${member} to ${member.guild.name} we hope you enjoy your stay here!`).setColor("#FF0000");

        channel.send(joinembed);
        economy.set(`${member.id}`, { bal: 100, bank: 0, guild: `${member.guild.id}`, items: [] });
    }),
    bot.on("message", async (message) => {
        let args = message.content.substring(PREFIX.length).split(" ");

        //Command on learn
        switch (args[0]) {
            case "learn":
                bot.commands.get("learn1").execute(message, args, economy);
                break;
            case "ping":
                message.channel.send("Pong!");
                break;
            case "hello":
                message.channel.send("Hello dear " + message.author.username);
                break;
            case "shop":
                var shopEmbed = new Discord.MessageEmbed().setTitle(`Shop`).setDescription(`You can buy any of these items with :coin:`).addFields(
                    {
                        name: `Coins Boost`,
                        value: `Gives you 20% more coins for 24 hours`,
                    },
                    {
                        name: `Admin for 24 hours`,
                    }
                );
                message.channel.send(shopEmbed);
                break;
        }
    }),
    bot.login(token);

learn.js:

const bot = new Discord.Client();
const fs = require("fs");
bot.commandLearn = new Discord.Collection();
const commandFiles = fs.readdirSync("/Users/dhruvanchandrupatla/Desktop/schoolhelpbot/commands1/learn").filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
    const command = require(`/Users/dhruvanchandrupatla/Desktop/schoolhelpbot/commands1/learn/${file}`);

    bot.commandLearn.set(command.name, command);
}

module.exports = {
    name: "learn",
    description: "learn something about a given subject",
    execute(message, args, learnHelp, economy) {
        switch (args[1]) {
            case "english":
                bot.commandLearn.get("english").execute(message, args);
                break;
            case "math":
                bot.commandLearn.get("math").execute(message, args);
                break;
            case "geography":
                bot.commandLearn.get("geography").execute(message, args);
                break;
            case "help":
                message.channel.send(learnHelp);
                break;
            case "test":
                var randomNum = Math.floor(Math.random() * 3) + 1;
                var randomNum2 = Math.floor(Math.random() * 3) + 1;
                const filter = (m) => m.author.id === message.author.id;
                const collector = new Discord.MessageCollector(message.channel, filter, { max: 1, time: 10000 });
                bot.commandsLearn.get("test").execute(message, args, economy, randomNum, randomNum2, collector, filter);
                break;
        }
    },
};

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为您试图从您的命令集合中获取名称为 learn1 的模块,但是,该模块不存在。您已在 learn 中将模块命名为 learn.js,因此正确的代码应该是

bot.commands.get("learn").execute(message, args, economy);