0

I don't know how to formule the question(title), so I'll just say it here, and maybe someone may help or provide a new point of view about it. I'm working with discord.js and I want to check if user has a role/roles from a message

client.on('message', message => {
    if (message.member.roles.find(r => r.name === 'ADMIN')) {
        console.log('I am admin');
    }
});

Now my idea is to make this a polymorphism functions, because if I want to check two roles I have to do this:

client.on('message', message => {
    if (message.member.roles.find(r => r.name === 'ADMIN') || message.member.roles.find(r => r.name === 'Other')) {
        console.log('I am admin');
    }
});

And I would like to move this if condition to a function and it will check it even if I want to check one or more `permissions.

For now I have tried to deal with it, but nothing, and stopped here... Maybe someone can give me some advice or other point of view. The idea is to prevent duplicate code and an if as long as the width of my monitor.

function hasRole(msg, permissions, condition = 'or') {
    if (Array.isArray(permission)) {
        if (condition === 'or') {
            permissions.forEach(function (element) {

            });
//            permission.join('||');
            return msg.member.roles.find(r => r.name === permissions) ||
                msg.member.roles.find(r => r.name === permissions);
        } else if (condition === 'and') {

        }
    }

    return msg.member.roles.find(r => r.name === permissions);
}

If you want to check DiscordJS DOCS.

Response:

function checkPermissions(msg, permissions, condition = 'or') {
    if (condition === 'or') {
        return msg.member.roles.some(r => permissions.includes(r.name));
    } else if (condition === 'and') {
        return msg.member.roles.every(r => permissions.includes(r.name));
    }
}

2 Answers 2

1

Something like this?

function hasRole(msg, permissions, condition = 'or') {

    //harmonise @permissions to array, if isn't already one
    permissions = Array.isArray(permissions) ? permissions : [permissions];

    //if or, at least 1 member role must be present in @permissions
    if (condition === 'or')
        return msg.member.roles.some(role => permissions.includes(role));

    //if and, all member roles must be found in @permissions
    else
        return permissions.every(perm => msg.member.roles.includes(perm));

}
Sign up to request clarification or add additional context in comments.

1 Comment

In this case includes is not a function. But I can follow you, I think I can use https://discord.js.org/#/docs/main/stable/class/Collection?scrollTo=every will try it. Thanks
0
function checkPermissions(msg, permissions, condition = 'or') {
    if (Array.isArray(permissions)) {
        if (condition === 'or') {
            return msg.member.roles.some(r => permissions.includes(r.name));
        } else if (condition === 'and') {
            let value = false;
            permissions.forEach(permission => {
                value = !!msg.member.roles.find(r => r.name === permission);
            });
            return value;
        }
    }
    return !!msg.member.roles.find(r => r.name === permissions);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.