类别帮助命令在 discord.js 中不起作用

时间:2021-06-17 10:30:31

标签: discord.js

我的类别帮助命令没问题,但是当我尝试执行帮助 [类别] 时,它不会显示类别中的命令,而是显示的是 Invalid command 但是当我执行帮助 [命令名称] 时它工作得很好。

const {
    MessageEmbed,
    Message,
    Client
} = require("discord.js");
const {
    readdirSync
} = require("fs");
const prefix = require("../../config.json").prefix;
let color = "#36393f"

module.exports = {
    name: "help",
    aliases: ['h'],
    description: "Shows all available bot commands.",
    timeout: 1000,
    /**
     * 
     * @param {Client} client 
     * @param {Message} message 
     * @param {String} args 
     * @returns 
     */
    run: async (client, message, args) => {

        if (!args[0]) {
            let categories = [];

            const emo = {
                info: '?',
                utility: '?',
                fun: '?',
                moderation: '?',
                misc: '?',
                support: '?',
                image: '?',
                config: '?',
                logging: '?',
                member_system: '?‍♂️',
                backup: '?',
                automod: '⚡'
            }

            readdirSync("./commands/").forEach((dir) => {
                const name = `${emo[dir.toLowerCase()]} ${dir.toUpperCase()}`
                let cats = new Object();

                cats = {
                    name: name,
                    value: `\`${prefix}help ${dir.toLowerCase()}\``,
                    inline: true
                }

                categories.push(cats);
                //cots.push(dir.toLowerCase());
            });

            const embed = new MessageEmbed()
                .setTitle("Help Menu:")
                .setDescription(`\`\`\`js\nPrefix: ${prefix}\nParameters: <> = required, [] = optional\`\`\`\nTo check out a category, use command ${prefix}help [category name] For more information go to the next page by reacting!\n\n__**Categories**__`)
                .addFields(categories)
                .setFooter(
                    `Requested by ${message.author.tag}`,
                    message.author.displayAvatarURL({
                        dynamic: true
                    })
                )
                .setTimestamp()
                .setColor(color);
            return message.channel.send(embed);
        } else {
            let cots = [];
            let catts = [];
            readdirSync("./commands/").forEach((dir) => {
                if (dir !== args[0].charAt(0).toUpperCase() + args[0].slice(1)) return;
                const commands = readdirSync(`./commands/${dir}/`).filter((file) =>
                    file.endsWith(".js")
                );

                const cmds = commands.map((command) => {
                    let file = require(`../../commands/${dir}/${command}`);

                    if (!file.name) return "No command name.";

                    let name = file.name.replace(".js", "");

                    let des = client.commands.get(name).description;

                    let obj = {
                        cname: `\`${name}\``,
                        des
                    }

                    return obj;
                });

                let dota = new Object();

                cmds.map(co => {
                    dota = {
                        name: `${cmds.length === 0 ? "In progress." : co.cname}`,
                        value: co.des ? co.des : 'No Description',
                        inline: true,
                    }
                    catts.push(dota)
                });
                cots.push(dir.toLowerCase());
            });

            console.log(cots);

            const command =
                client.commands.get(args[0].toLowerCase()) ||
                client.commands.find(
                    (c) => c.aliases && c.aliases.includes(args[0].toLowerCase())
                );


            //if (cots.includes(args[0].toLowerCase() && command)) return message.reply('There are two same names, one is a category and one is a command! Boopp... Confused!!')
            if (cots.includes(args[0].toLowerCase())) {
                const combed = new MessageEmbed()
                    .setTitle(`__${args[0].charAt(0).toUpperCase() + args[0].slice(1)} Commands!__`)
                    .setDescription(`Use \`${prefix}help\` followed by a command name to get more information on a command.\nFor example: \`${prefix}help ping\`.\n\n`)
                    .addFields(catts)
                    .setColor(color)

                return message.channel.send(combed)
            }

            if (!command) {
                const embed = new MessageEmbed()
                    .setTitle(`Invalid command! Use \`${prefix}help\` for all of my commands!`)
                    .setColor("FF0000");
                return message.channel.send(embed);
            }

            const embed = new MessageEmbed()
                .setTitle("Command Details:")
                .addField("My Prefix:", `\`${prefix}\``)
                .addField(
                    "Command:",
                    command.name ? `\`${command.name}\`` : "No name for this command."
                )
                .addField(
                    "Aliases:",
                    command.aliases ?
                    `\`${command.aliases.join("` `")}\`` :
                    "No aliases for this command."
                )
                .addField(
                    "Usage:",
                    command.usage ?
                    `\`${prefix}${command.name} ${command.usage}\`` :
                    `\`${prefix}${command.name}\``
                )
                .addField(
                    "Command Description:",
                    command.description ?
                    command.description :
                    "No description for this command."
                )
                .setFooter(
                    `Requested by ${message.author.tag}`,
                    message.author.displayAvatarURL({
                        dynamic: true
                    })
                )
                .setTimestamp()
                .setColor(color);
            return message.channel.send(embed);
        }
    },
};

0 个答案:

没有答案