首页 > 解决方案 > 从帮助列表中隐藏 nsfw 命令

问题描述

还没有看到任何答案。

我正在寻找一种方法来隐藏我的 discord.js 机器人的任何 nsfw 命令。我希望它仅在执行帮助命令的通道标记为 nsfw 时显示。

因此,如果通道标记为 nsfw,它会显示 nsfw 类别和命令。如果它没有被标记为 nsfw,它会隐藏 nsfw 类别和命令。

感谢您为我提供的任何帮助。

这显示了每个命令及其各自的类别

这是我的帮助命令代码 -

const { stripIndents } = require("common-tags");

module.exports = {
    name: "help",
    category: "info",
    description: "Tells you the commads currently able to be used",
    run: async (client, message, args) => {
        if(args[0]){
            return getCMD(client, message, args[0]);
        }else{
            return getALL(client, message);
        }
    }
}

function getALL(client, message){
    const embed = new RichEmbed()
        .setColor("RANDOM")
        .setThumbnail(message.guild.iconURL)

    const commands = (category) => {
        return client.commands
            .filter(cmd => cmd.category === category)
            .map(cmd => `**|-** \`${cmd.name}\``)
            .join("\n");
    }

    const info = client.categories
        .map(cat => stripIndents`**${cat[0].toUpperCase() + cat.slice(1)}** \n${commands(cat)}`)
        .reduce((string, category) => string + "\n" + category);

    return message.channel.send(embed.setDescription(info));
}

function getCMD(client, message, input){
    const embed = new RichEmbed()

    const cmd = client.commands.get(input.toLowerCase()) || client.commands.get(client.aliases.get(input.toLowerCase()));

    let info = `No Information found for command **${input.toLowerCase()}**`;

    if(!cmd){
        return message.channel.send(embed.setColor("#ff0000").setDescription(info));
    }

    if(cmd.name) info = `**Command Name -** ${cmd.name}`;
    if(cmd.aliases) info += `\n**Aliases -** ${cmd.aliases.map(a => `\`${a}\``).join(", ")}`;
    if(cmd.description) info += `**\nDescription -** ${cmd.description}`;
    if(cmd.category) info += `**\nCategory -** ${cmd.category}`;
    if(cmd.usage) {
        info += `**\nUsage -** ${cmd.usage}`
        embed.setFooter(`Syntax <> = Required, [] = Optional`);
    }

    embed.setThumbnail(message.guild.iconURL);

    return message.channel.send(embed.setColor("GREEN").setDescription(info));
}

标签: javascript

解决方案


一种创建排除 arr 的方法,如果命令通道 nsfw 标志 = false,则在此处推送“nsfw”,然后在过滤器上检查它。

 const { stripIndents } = require("common-tags");

module.exports = {
    name: "help",
    category: "info",
    description: "Tells you the commads currently able to be used",
    run: async (client, message, args) => {
        if(args[0]){
            return getCMD(client, message, args[0]);
        }else{
            return getALL(client, message);
        }
    }
}

function getALL(client, message){
    let excludeCategoryArr = [];
    if(!message.channel.nsfw) excludeCategoryArr.push('nsfw')
    const embed = new RichEmbed()
        .setColor("RANDOM")
        .setThumbnail(message.guild.iconURL)

    const commands = (category) => {
        return client.commands
            .filter(cmd => cmd.category === category && !excludeCategoryArr.includes(cmd.category))
            .map(cmd => `**|-** \`${cmd.name}\``)
            .join("\n");
    }

    const info = client.categories
        .filter(category => !excludeCategoryArr.includes(category))
        .map(cat => stripIndents`**${cat[0].toUpperCase() + cat.slice(1)}** \n${commands(cat)}`)
        .reduce((string, category) => string + "\n" + category);

    return message.channel.send(embed.setDescription(info));
}

function getCMD(client, message, input){

    const embed = new RichEmbed()

    const cmd = client.commands.get(input.toLowerCase()) || client.commands.get(client.aliases.get(input.toLowerCase()));

    let info = `No Information found for command **${input.toLowerCase()}**`;

    if(!cmd){
        return message.channel.send(embed.setColor("#ff0000").setDescription(info));
    }

    if(cmd.category === 'nsfw' && !message.channel.nsfw) {
        return message.reply('I can`t show NSWF command in not nsfw channel')
    }

    if(cmd.name) info = `**Command Name -** ${cmd.name}`;
    if(cmd.aliases) info += `\n**Aliases -** ${cmd.aliases.map(a => `\`${a}\``).join(", ")}`;
    if(cmd.description) info += `**\nDescription -** ${cmd.description}`;
    if(cmd.category) info += `**\nCategory -** ${cmd.category}`;
    if(cmd.usage) {
        info += `**\nUsage -** ${cmd.usage}`
        embed.setFooter(`Syntax <> = Required, [] = Optional`);
    }

    embed.setThumbnail(message.guild.iconURL);

    return message.channel.send(embed.setColor("GREEN").setDescription(info));
}

推荐阅读