Search code examples
javascriptnode.jsdiscorddiscord.jsbots

How do I change Discord bot command code so it doesn't require specific permissions


const { MessageEmbed } = require("discord.js")

module.exports = {
name: 'help',
description: "Lists all the commands",
run: async function (message, args) {

        let command
        if (args.length) command = message.client.commands.get(args[0]) || message.client.commands.find(function(cmd) {
            aliases = cmd.aliases || []
            return aliases.includes(args[0])
        })
        if (command) {
            if (command.ownerOnly && message.author.id !== message.guild.ownerID) command = undefined
            if (command && command.permissions) {
                if (typeof command.permissions == 'string') command.permissions = [command.permissions]
                for (const permission of command.permissions) if (!message.member.permissions.has(permission)) command = undefined
            }
            if (command) {
                const embed = new MessageEmbed()
                .setTitle(`${message.client.config.prefix[0]}${command.name} ${command.expectedArgs ? ` ${command.expectedArgs}` : ''}${command.aliases ? ` [${command.aliases.join(' ')}]` : ''}`)
                .setDescription(`${command.description ? command.description : 'No description provided'}`)
                message.channel.send(embed)
            }
        }
    
        if (!command) {
            const embed = new MessageEmbed()
            .setDescription(`Prefixes: ${message.client.config.prefix.map(p => `\`${p}\``).join(' ')}\nTo get more information of a particular command, type \`${message.client.config.prefix[0]}${this.name} [command]\``)
            const categories = []
            const cmds = message.client.commands.array()
            for (const command of cmds) if (command.category && !categories.includes(command.category)) categories.push(command.category)
            for (const category of categories) {
                const commands = cmds.filter(function (cmd) {
                    let isOwner = true
                    let hasPerm = true
                    if (cmd.ownerOnly && message.author.id !== message.guild.ownerID) isOwner = false
                    if (cmd.permissions) {
                        if (typeof cmd.permissions == 'string') cmd.permissions = [cmd.permissions]
                        for (const permission of cmd.permissions) if (!message.member.permissions.has(permission)) hasPerm = false
                    }
                    return isOwner && hasPerm && cmd.category == category
                })
                if (commands.length) embed.addField(category.toUpperCase(), commands.map(command => `[\`${command.name}\`](https://google.com)`).join(' '), true) 
            }
            message.channel.send(embed)
        }
    }

}

I'm sure this is a simple fix, but everything I do gives me an error!

please help. I am a noob when it comes to node.js and am amazed that I even managed to get it to work in the first place. Last time I touched this project was 2 years ago and have abandoned it since. Please be kind I know this is a dumb question.


Solution

  • I apologize for the incoherent question and lack of information on the post. I wasn't quite sure how to make these posts so hopefully this one will be better.

    I removed the if statements that made it owner only but kept getting an error message:

    (node:48264) UnhandledPromiseRejectionWarning: ReferenceError: isOwner is not defined
    

    and

    (node:48264) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    

    To fix this, I had to add a return to const commands = cmds.filter(function (cmd) { so that it became:

    const commands = cmds.filter(function (cmd) {
       return cmd.category == category
    });
    

    Here is the updated version.

    const { MessageEmbed } = require("discord.js")
    
    module.exports = {
        name: 'help',
        description: "Lists all the commands",
        run: async function (message, args) {
    
            let command
            if (args.length) command = message.client.commands.get(args[0]) || message.client.commands.find(function(cmd) {
                aliases = cmd.aliases || []
                return aliases.includes(args[0])
            })
            if (command) {
                /*if (command.ownerOnly && message.author.id !== message.guild.ownerID) command = undefined
                if (command && command.permissions) {
                    if (typeof command.permissions == 'string') command.permissions = [command.permissions]
                    for (const permission of command.permissions) if (!message.member.permissions.has(permission)) command = undefined
                }*/
                if (command) {
                    const embed = new MessageEmbed()
                    .setTitle(`${message.client.config.prefix[0]}${command.name} ${command.expectedArgs ? ` ${command.expectedArgs}` : ''}${command.aliases ? ` [${command.aliases.join(' ')}]` : ''}`)
                    .setDescription(`${command.description ? command.description : 'No description provided'}`)
                    message.channel.send(embed)
                }
            }
    
            if (!command) {
                const embed = new MessageEmbed()
                .setDescription(`Prefixes: ${message.client.config.prefix.map(p => `\`${p}\``).join(' ')}\nTo get more information of a particular command, type \`${message.client.config.prefix[0]}${this.name} [command]\``)
                const categories = []
                const cmds = message.client.commands.array()
                for (const command of cmds) if (command.category && !categories.includes(command.category)) categories.push(command.category)
                for (const category of categories) {
                    const commands = cmds.filter(function (cmd) {
                        return cmd.category == category
                        /*let isOwner = true
                        let hasPerm = true
                        if (cmd.ownerOnly && message.author.id !== message.guild.ownerID) isOwner = false
                        if (cmd.permissions) {
                            if (typeof cmd.permissions == 'string') cmd.permissions = [cmd.permissions]
                            for (const permission of cmd.permissions) if (!message.member.permissions.has(permission)) hasPerm = false
                        }
                        return isOwner && hasPerm && cmd.category == category*/
                    });
                    if (commands.length) embed.addField(category.toUpperCase(), commands.map(command => `[\`${command.name}\`](https://google.com)`).join(' '), true) 
                }
                message.channel.send(embed)
            }
        }
    }