Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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;
question from:https://stackoverflow.com/questions/65859356/delete-message-command-its-work-only-for-owner-admin-and-moderator

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...