如何忽略命令处理程序帮助命令中的某些命令?

时间:2021-06-27 02:03:26

标签: node.js discord.js

按照 [the discord js guide][1] 中的指南,我设法制作了动态帮助命令。现在,当我运行 help 命令时,它确实列出了所有命令,但我希望它隐藏一些在命令文件中包含 ownerOnly: true 的命令。现在,当 ownerOnly 为真时,只有机器人所有者才能执行该命令,但它仍会显示在帮助命令中。这是我使用的帮助命令:

module.exports = {
    name: 'help', //command name
    description: 'Lists commands or gets info about a specific command.',
    usage: `(command name)`,
    guildOnly: false,
//  ownerOnly: true,      this is where I would put the ownerOnly thing.
    cooldown: 5,
    execute(message, args, prefix) { //inside here command stuff
        const data = [];
        const { commands } = message.client;

        if (!args.length) {
            data.push(commands.map(command => command.name).join('\n'));
            data.push(`\n**\nYou can send \`${prefix}help [command name]\` to get info on a specific command!`);
            const helpembed = new Discord.MessageEmbed()
            .setTitle("Here's a list of all my commands:")
            .setDescription('**' + data, { split: true})
            .setColor('RANDOM')

            return message.author.send(helpembed)
            .then(() => {
            if (message.channel.type === 'dm') return;
            message.react('?');
            })
            .catch(error => {
            console.error(`Could not send help DM to ${message.author.tag}.\n`, error);
            message.reply('it seems like I can\'t DM you! Do you have DMs disabled?');
        });
        }
        const name = args[0].toLowerCase();
        const command = commands.get(name) || commands.find(c => c.aliases && c.aliases.includes(name));

        if (!command) {
            return message.reply('that\'s not a valid command!');
        }

        if (command.aliases) data.push(`**Aliases:** ${command.aliases.join(', ')}`);
        if (command.description) data.push(`**Description:** ${command.description}`);
        if (command.usage) data.push(`**Usage:** ${prefix}${command.name} ${command.usage}`);
        data.push(`**Cooldown:** ${command.cooldown || 3} second(s)`);

        const helpembed = new Discord.MessageEmbed()
        .setTitle(`Name: ${command.name}`)
        .setDescription(data, { split: true})
        .setColor('RANDOM')
        message.channel.send(helpembed)
    },
};

和命令处理程序:

const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName))
command.execute(message, args, prefix, client);

我希望帮助命令忽略包含 ownerOnly: true 的命令,有什么想法吗?

编辑:如果有帮助,只有所有者的命令存储在单独的类别/文件夹中。 [1]:https://discordjs.guide/command-handling/adding-features.html#a-dynamic-help-command

1 个答案:

答案 0 :(得分:1)

这一行:

            data.push(commands.map(command => command.name).join('\n'));

可以在 .filter() 之前添加 .map() 并以您想要的任何方式对其进行过滤。这不是 discord.js 的事情,只是 Javascript 的事情。试试

            data.push(commands.filter(command => !command.ownerOnly).map(command => command.name).join('\n'));

并告诉我们这是否适合您。请注意,它不会阻止人们猜测命令并明确要求该命令的帮助,或尝试使用它;它仅过滤列出的数组以排除具有真实 ownerOnly 属性的项目。