Merge pull request #265 from ProfessorDey/patch-2

Simplify rolesetter.js Control Logic
This commit is contained in:
Ralph 2019-10-09 20:42:31 -04:00 committed by GitHub
commit 4ab49b85b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,47 +13,46 @@ exports.addrole = {
usage: '<role>', usage: '<role>',
description: 'Adds you to specified role', description: 'Adds you to specified role',
process: function(bot, msg, suffix) { process: function(bot, msg, suffix) {
//rolelist.allowedroles = rolelist.allowedroles.map(v => v.toLowerCase());
// Here the bot, msg and suffix is avaible, this function can be async if needed. // Here the bot, msg and suffix is avaible, this function can be async if needed.
let newrole = msg.guild.roles.find('name', suffix); // Make sure to eliminate case sensitivity, do this here to only perform the sweep once.
let newrole = msg.guild.roles.find(role => role.name.toLowerCase() === suffix.toLowerCase());
// Baserole is assumed to already be case accurate as it's handled in the config itself.
let baserole = msg.guild.roles.find('name', rolelist.baserole); let baserole = msg.guild.roles.find('name', rolelist.baserole);
// Checks if the user put a role in the message.
if (inPrivate(msg)) { // Provide shortened syntax for the sake of code cleanliness
msg.channel.send('You can not set roles in DMs! Please go to the Discord server to do this.'); let send = (msgTxt) => { msg.channel.send(msgTxt); return; };
let rolecmd = botconfig.prefix + "roles";
// Checks if the user has messaged the bot via Direct Message
if (inPrivate(msg))
return send('You can not set roles in DMs! Please go to the Discord server to do this.');
// Checks if the user included a role in their message
if (!suffix)
return send("Please specify a role. Type " + rolecmd + " to see which you may add yourself!");
// Checks if there is a matching role found on the server
if(!newrole)
return send("The role specified `" + suffix + "` does not exist on this server!");
// Checks that the allowed roles and base role against the matched role's name, since this eliminates case sensitivity issues
if(!rolelist.allowedroles.includes(newrole.name) && !rolelist.baserole.includes(newrole.name))
return send("That role isn't one you can add yourself to! Type " + rolecmd + " command to find out which ones are allowed.");
// Use the matched name to check against the member's existing roles
if(msg.member.roles.find('name', newrole.name))
return send("It seems you already have the " + newrole.name + "role");
// Assuming all these factors succeed, add the role
msg.member.addRole(newrole).then(send(msg.member + " has been added to the " + newrole.name + " role!"));
// Check if a baserole is actually set
if(!rolelist.baserole)
return; return;
} else { // Confirm that the role exists on the server and if not then be sure to send a nag message
if (suffix) { if(!baserole)
//suffix = suffix.toLowerCase(); return send("The base role of " + rolelist.baserole + " has been set in config but is missing from the server");
// Checks if the role mentioned in the message is in the allowed roles listed in the wunderbot config. // Confirm if the user has the baserole already, including if it was added just now
if (rolelist.allowedroles.includes(suffix) || rolelist.baserole.includes(suffix)) { if(msg.member.roles.find('name', baserole.name))
// Checks if the role even exists in the discord server return;
if (newrole !== null) { // Add the base role and avoid spamming the user by only mentioning them in the previous message
// Checks if the member has the role that they are trying to add msg.member.addRole(baserole).then(send("We also added the base " + rolelist.baserole + " role for you!"));
if (!msg.member.roles.find('name', suffix)) {
msg.member.addRole(newrole).then(msg.channel.send(msg.member + ' has been added to the ' + suffix + ' role!'));
if (rolelist.baserole !== ' ') {
if (baserole !== null) {
// Checks if Member has the baserole, and also checks if they just added the baserole
if (!msg.member.roles.find('name', rolelist.baserole) && suffix !== rolelist.baserole) {
msg.member.addRole(baserole).then(msg.channel.send(msg.member + ' has been added to the ' + rolelist.baserole + ' role!'));
}
} else {
msg.channel.send('The ' + rolelist.baserole + " Role doesn't exist. Please add that role first!");
}
}
} else {
msg.channel.send('It seems that you already have that role! Try removing it first with the ' + botconfig.prefix + 'delrole command!');
}
} else {
msg.channel.send('The role ' + '`' + suffix + '`' + ' does not exist!');
}
} else {
msg.channel.send("That role isn't one you can add yourself to! Please run the " + botconfig.prefix + 'roles command to find out which ones are allowed.');
}
} else {
msg.channel.send('Please specify a role. Type ' + botconfig.prefix + 'roles to see which you may add!');
}
}
} }
}; };
exports.delrole = { exports.delrole = {
@ -61,33 +60,30 @@ exports.delrole = {
description: 'Deletes the specified role from your account', description: 'Deletes the specified role from your account',
process: function(bot, msg, suffix) { process: function(bot, msg, suffix) {
// Here in the bot, msg and suffix are available, this function can be async if needed. // Here in the bot, msg and suffix are available, this function can be async if needed.
let oldrole = msg.guild.roles.find('name', suffix); // Make sure to eliminate case sensitivity, do this here to only perform the sweep once.
// Checks if the user put a role in the message. let oldrole = msg.guild.roles.find(role => role.name.toLowerCase() === suffix.toLowerCase());
if (inPrivate(msg)) { // Provide shortened syntax for the sake of code cleanliness
msg.channel.send('You can not set roles in DMs! Please go to the Discord server to do this.'); let send = (msgTxt) => { msg.channel.send(msgTxt); return; };
return; let rolecmd = botconfig.prefix + "roles";
} else {
if (suffix) { // Checks if the user has messaged the bot via Direct Message
// Checks if the role mentioned in the message is in the allowed roles listed in the Wunderbot config. if (inPrivate(msg))
if (rolelist.allowedroles.includes(suffix)) { return send('You can not set roles in DMs! Please go to the Discord server to do this.');
// Checks if the role exists in the Discord server // Checks if the user included a role in their message
if (oldrole !== null) { if (!suffix)
// Checks if the member has the role that they are trying to add return send("Please specify a role. Type " + rolecmd + " to see which you may remove yourself!");
if (msg.member.roles.find('name', suffix)) { // Checks if there is a matching role found on the server
msg.member.removeRole(oldrole).then(msg.channel.send(msg.member + ' has been removed from the ' + suffix + ' role!')); if(!newrole)
} else { return send("The role specified `" + suffix + "` does not exist on this server!");
msg.channel.send("You don't seem to have that role! Try adding it first with the " + botconfig.prefix + 'addrole command!'); // Checks that the allowed roles against the matched role's name, since this eliminates case sensitivity issues
} if(!rolelist.allowedroles.includes(oldrole.name))
} else { return send("That role isn't one you can remove yourself! If you need it removed, please ask a moderator!");
msg.channel.send('The role ' + '`' + suffix + '`' + ' does not exist!'); // Use the matched name to check against the member's existing roles
} if(!msg.member.roles.find('name', oldrole.name))
} else { return send("It seems you don't actually have the " + oldrole.name + "role! Mission accomplished!");
msg.channel.send("That role isn't one you can add yourself to! Please run the " + botconfig.prefix + 'roles command to find out which ones are allowed.');
} // Assuming all these factors succeed, add the role
} else { msg.member.removeRole(oldrole).then(send(msg.member + " has been removed from the " + oldrole.name + " role!"));
msg.channel.send('Please specify a role. Type ' + botconfig.prefix + 'roles to see which you may add!');
}
}
} }
}; };
exports.roles = { exports.roles = {