I'm currently building a discord verification bot, one of the features of this bot if to check if you have 1 role from each required category and then produce of summary of your roles at the end. Currently I have a working solution that functions exactly how I want it too but it uses a large stack of if's, I'm quite new to js but have been told to stay away from using large if/if else if stacks where possible. I've looked into switch cases but cant really see how I would apply those here, so I'm wondering if there's a more optimized way to achieve what I need. My current code is as follows:
var hasrole = false;
var role = "";
var subrole = "";
var istype1 = false;
var istype2 = false;
var istype3 = false;
var istype4 = false;
var counter = 0;
if (message.member.roles.cache.find(r => r.id === "736032207248031765")) {
istype1 = true;
role = "Type 1";
hasrole = true;
counter ++;
}
if (message.member.roles.cache.find(r => r.id === "736032822053175306")) {
istype2 = true;
role = "Type 2";
hasrole = true;
counter ++;
}
if (message.member.roles.cache.find(r => r.id === "736032670013980702")) {
role = "Type 3";
hasrole = true;
counter++
if (message.member.roles.cache.find(r => r.id === "736032716411371581")) {
istype1 = true;
subrole = "Type 1";
} else if (message.member.roles.cache.find(r => r.id === "736032780122849340") {
istype2 = true;
subrole = "Type 2";
} else {
istype3 = true;
}
}
if (message.member.roles.cache.find(r => r.id === "736032870703038534")) {
istype4 = true;
role = "Type 4";
hasrole = true;
counter ++;
}
if (message.member.roles.cache.find(r => r.id === "736033471474172025")) {
istype2 = true;
role = "Type 5";
hasrole = true;
counter ++;
}
if (counter >= 1) {
message.channel.send("More than 1 main role, please have only 1");
return;
} else if (hasrole == false) {
message.channel.send("No main role please get one")
return;
}