vylbot-app/commands/role.js

106 lines
4.3 KiB
JavaScript
Raw Normal View History

// Required components
2020-11-04 19:58:54 +00:00
const { command } = require('vylbot-core');
const { MessageEmbed } = require('discord.js');
// Command variables
2020-11-04 19:58:54 +00:00
const embedColor = "0x3050ba";
// Command class
2020-11-04 19:58:54 +00:00
class role extends command {
constructor() {
// Set the command's run method, description, category, and example usage
2020-11-04 19:58:54 +00:00
super("role");
super.description = "Toggles a role for the user to gain/remove";
super.category = "General";
super.usage = "[name]";
// Require in the config the 'assignable roles' array
2020-11-04 19:58:54 +00:00
super.requiredConfigs = "assignable";
}
// Run method
2020-11-04 19:58:54 +00:00
role(context) {
// Get the array containing the assignable roles
2021-02-17 18:12:45 +00:00
const roles = context.client.config.role.assignable;
2020-11-04 19:58:54 +00:00
let requestedRole = "";
// If the arguments specifys a specific role
2020-11-04 19:58:54 +00:00
if (context.arguments.length > 0) {
// Loop through all the assignable roles and check against the first parameter
// Save the role name if they match, i.e. the role can be assignable
2020-11-04 19:58:54 +00:00
for (let i = 0; i < roles.length; i++) {
if (roles[i].toLowerCase() == context.arguments[0].toLowerCase()) {
requestedRole = roles[i];
}
}
// If a matching assignable role was found
2020-11-04 19:58:54 +00:00
if (requestedRole != "") {
// Get the role object from the server with the role name
2021-02-17 18:12:45 +00:00
const role = context.message.guild.roles.cache.find(r => r.name == requestedRole);
2020-11-04 19:58:54 +00:00
// If the user already has the role, remove the role from them and send an embed
// Otherwise, add the role and send an embed
2020-11-04 19:58:54 +00:00
if (context.message.member.roles.cache.find(r => r.name == requestedRole)) {
context.message.member.roles.remove(role).then(() => {
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription(`Removed role: ${requestedRole}`);
context.message.channel.send(embed);
}).catch(err => {
console.error(err);
2021-02-17 18:12:45 +00:00
const errorEmbed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription("An error occured. Please check logs");
context.message.channel.send(errorEmbed);
});
} else { // If the user doesn't have the role
2020-11-04 19:58:54 +00:00
context.message.member.roles.add(role).then(() => {
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription(`Gave role: ${requestedRole}`);
context.message.channel.send(embed);
}).catch(err => {
console.error(err);
2021-02-17 18:12:45 +00:00
const errorEmbed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription("An error occured. Please check logs");
context.message.channel.send(errorEmbed);
});
}
} else { // If the role can't be found, send an error embed
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription("This role does not exist, see assignable roles with the role command (no arguments)");
context.message.channel.send(embed);
}
} else { // If no role was specified, Send a list of the roles you can assign
// The start of the embed text
2020-11-04 19:58:54 +00:00
let rolesString = `Do ${context.client.config.prefix}role <role> to get the role!\n`;
// Loop through all the roles, and add them to the embed text
2020-11-04 19:58:54 +00:00
for (let i = 0; i < roles.length; i++) {
rolesString += `${roles[i]}\n`;
}
// Create an embed containing the text
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setTitle("Roles")
.setColor(embedColor)
.setDescription(rolesString);
// Send the embed
2020-11-04 19:58:54 +00:00
context.message.channel.send(embed);
}
}
}
module.exports = role;