Delete message command, its work only for Owner, Admin and Moderator

Viewed 431

I am code the Discord bot for my server. I want to add a command for my bot, the command will clear a text channel, and will only allow people with Owner, Admin, Moderator role.

In the code, it's have if(!message.member.roles.cache.some(r => r.name === "**Owner**"), I have this role but I can't delete message. But when I give 3 roles (Owner, Admin and Moderator) for me, I can use it.

So I need to fix: 1 of 3 roles can still use it.

The code is here:

case 'clear': 
    if(!args[1]) 
        return message.reply('Please specify the number of messages to clear `e.g. !clear 10`')
            .then(message => message.delete({timeout: 3500}));

    if(!message.member.roles.cache.some(r => r.name === "Owner") || 
            !message.member.roles.cache.some(r => r.name === "Admin") || 
            !message.member.roles.cache.some(r => r.name === "Moderator")) 
        return message.reply('You do not have permissions to clear!')
            .then(message => message.delete({timeout: 3500}));

    message.channel.bulkDelete(args[1]);
    break;
1 Answers

You need to make it check if the user has neither of the three roles. Since you gave || between the if statements and not &&, it will check if the user doesn't have one or more of the three roles. If the user doesn't have one or more of them, it will output the message.reply.

If you give && it will check if the user has none of the three roles.

P.S: there were some bracket errors and quotation errors in your provided code.

UPDATE: "**Owner**" has been changed to "Owner"

Here's the updated code:

case 'clear': 
            if(!args[1]) return message.reply('Please specify the number of messages to clear `e.g. !clear 10`')
            .then(message => message.delete({timeout: 3500}));
            if(!message.member.roles.cache.some(r => r.name === "Owner") && (!message.member.roles.cache.some(r => r.name === "Admin")) && (!message.member.roles.cache.some(r => r.name === "Moderator")) 
               {
                 return message.reply('You do not have permissions to clear!')
                 .then(message => message.delete({timeout: 3500}));
               }
            message.channel.bulkDelete(args[1]);
            break;
Related