Home > Software design >  Two if statements to make cmd differentiate between roles
Two if statements to make cmd differentiate between roles

Time:12-23

I'm wondering if it is possible to use if statements this way so that the command gives separate messages depending on the users role. This is one attempt I've made but the second if statement is unreachable.


module.exports = {
    name: "train",
    description: "Train to earn some reputation!",
    async execute(client, message, args, cmd, discord, profileData) {

        const events1 = [
            "sample event",
            "sample event",
            "sample event",
        ];

        const events2 = [
            "sample event",
            "sample event",
            "sample event",
        ];

        const injuries = [
            "sample injury",
            "sample injury",
            "sample injury",
        ];

        const chosenEvent1 = events1.sort(() => Math.random() - Math.random()).slice(0, 1);
        const chosenEvent2 = events2.sort(() => Math.random() - Math.random()).slice(0, 1);
        const chosenInjury = injuries.sort(() => Math.random() - Math.random()).slice(0, 1);

        const randomNumber1 = Math.floor(Math.random() * 29)   5;
        const randomNumber2 = Math.floor(Math.random() * 9)   1;

        if((!message.member.roles.cache.has('roleid#1'))); {
            if(Math.floor(Math.random() * 3) === 0) {
                await profileModel.findOneAndUpdate(
                    {
                        userID: message.author.id,
                    },
                    {
                        $inc: {
                        health: -randomNumber2,
                    },
                    }
                );
            return message.channel.send(`${chosenInjury} You lost ${randomNumber2} health and gained no reputation.`);
                } else {
                await profileModel.findOneAndUpdate(
                    {
                    userID: message.author.id,
                }, 
                {
                    $inc: {
                    reputation: randomNumber1,
                },
                }
            );
            return message.channel.send(`${chosenEvent1} You earned ${randomNumber1} reputation!`);
            }
        if((!message.member.roles.cache.has('roleid#2'))); {
            return message.channel.send(`${chosenEvent2} You earned ${randomNumber1} reputation!`);
        }
    }}
};

So ideally if you have RoleID #1, you have a chance of injury or your reputation increases and you get a message with Event1 prompts. If you have RoleID #2, your reputation just increases and you get a message with Event2 prompts. I hope this is clear.

CodePudding user response:

It seems the brackets are bit off and that is why the code is unreachable.

I have also attached an image of the locations where I made code changes. A key consideration is in javascript try to avoid placing semicolons right after an if statement

enter image description here Please see the small bracket changes I made in your code example:

module.exports = {
name: 'train',
description: 'Train to earn some reputation!',
async execute(client, message, args, cmd, discord, profileData) {
    const events1 = ['sample event', 'sample event', 'sample event'];

    const events2 = ['sample event', 'sample event', 'sample event'];

    const injuries = ['sample injury', 'sample injury', 'sample injury'];

    const chosenEvent1 = events1.sort(() => Math.random() - Math.random()).slice(0, 1);
    const chosenEvent2 = events2.sort(() => Math.random() - Math.random()).slice(0, 1);
    const chosenInjury = injuries.sort(() => Math.random() - Math.random()).slice(0, 1);

    const randomNumber1 = Math.floor(Math.random() * 29)   5;
    const randomNumber2 = Math.floor(Math.random() * 9)   1;

    if (!message.member.roles.cache.has('roleid#1')) {
        if (Math.floor(Math.random() * 3) === 0) {
            await profileModel.findOneAndUpdate(
                {
                    userID: message.author.id,
                },
                {
                    $inc: {
                        health: -randomNumber2,
                    },
                }
            );
            return message.channel.send(
                `${chosenInjury} You lost ${randomNumber2} health and gained no reputation.`
            );
        } else {
            await profileModel.findOneAndUpdate(
                {
                    userID: message.author.id,
                },
                {
                    $inc: {
                        reputation: randomNumber1,
                    },
                }
            );
            return message.channel.send(`${chosenEvent1} You earned ${randomNumber1} reputation!`);
        }
    } else if (!message.member.roles.cache.has('roleid#2')) {
        return message.channel.send(`${chosenEvent2} You earned ${randomNumber1} reputation!`);
    }
},
};
  • Related