Add comments to role, rules commands, and the events

This commit is contained in:
vylpes 2020-11-07 17:01:03 +00:00
parent 2e13b4d861
commit 6a7e52b0ca
7 changed files with 82 additions and 10 deletions

View file

@ -1,32 +1,46 @@
// Required components
const { command } = require('vylbot-core');
const { MessageEmbed } = require('discord.js');
// Command variables
const embedColor = "0x3050ba";
// Command class
class role extends command {
constructor() {
// Set the command's run method, description, category, and example usage
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
super.requiredConfigs = "assignable";
}
// Run method
role(context) {
// Get the array containing the assignable roles
let roles = context.client.config.role.assignable;
let requestedRole = "";
// If the arguments specifys a specific role
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
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
if (requestedRole != "") {
// Get the role object from the server with the role name
let role = context.message.guild.roles.cache.find(r => r.name == requestedRole);
// If the user already has the role, remove the role from them and send an embed
// Otherwise, add the role and send an embed
if (context.message.member.roles.cache.find(r => r.name == requestedRole)) {
context.message.member.roles.remove(role).then(() => {
let embed = new MessageEmbed()
@ -43,7 +57,7 @@ class role extends command {
context.message.channel.send(errorEmbed);
});
} else {
} else { // If the user doesn't have the role
context.message.member.roles.add(role).then(() => {
let embed = new MessageEmbed()
.setColor(embedColor)
@ -60,28 +74,32 @@ class role extends command {
context.message.channel.send(errorEmbed);
});
}
} else {
} else { // If the role can't be found, send an error embed
let embed = new MessageEmbed()
.setColor(embedColor)
.setDescription("This role does not exist, see assignable roles with the role command (no arguments)");
context.message.channel.send(embed);
}
} else {
} else { // If no role was specified, Send a list of the roles you can assign
// The start of the embed text
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
for (let i = 0; i < roles.length; i++) {
rolesString += `${roles[i]}\n`;
}
// Create an embed containing the text
let embed = new MessageEmbed()
.setTitle("Roles")
.setColor(embedColor)
.setDescription(rolesString);
// Send the embed
context.message.channel.send(embed);
}
}
}
module.exports = role;
module.exports = role;

View file

@ -1,53 +1,68 @@
// Required components
const { command } = require('vylbot-core');
const { MessageEmbed } = require('discord.js');
const { existsSync, readFileSync } = require('fs');
// Command variables
const embedColor = "0x3050ba";
// Command class
class rules extends command {
constructor() {
// Set the command's run method, description, and category
super("rules");
super.description = "Generates the rules embeds from the rules.txt file";
super.category = "Administration";
// Require in the config the name of the admin role and the rules file name
super.requiredConfigs = "adminrole";
super.requiredConfigs = "rulesfile";
}
// Run method
rules(context) {
// If the user is an Admin (has the admin role)
if (context.message.member.roles.cache.find(role => role.name == context.client.config.rules.adminrole)) {
// If the rulesfile exists
if (existsSync(context.client.config.rules.rulesfile)) {
// Get the contents of the rules file, and split it by "> "
// Each embed in the rules is set by the "> " syntax
let rulesText = readFileSync(context.client.config.rules.rulesfile).toString();
rulesText = rulesText.split("> ");
// Loop through each embed to be sent
for (let i = 0; i < rulesText.length; i++) {
// If the first line after "> " has a "#", create and embed with an image of the url specified after
if (rulesText[i].charAt(0) == '#') {
let embed = new MessageEmbed()
.setColor(embedColor)
.setImage(rulesText[i].substring(1));
context.message.channel.send(embed);
} else {
} else { // If the file doesn't have a "#" at the start
// Split the embed into different lines, set the first line as the title, and the rest as the description
let rulesLines = rulesText[i].split("\n");
let rulesTitle = rulesLines[0];
let rulesDescription = rulesLines.slice(1).join("\n");
// Create the embed with the specified information above
let embed = new MessageEmbed()
.setTitle(rulesTitle)
.setColor(embedColor)
.setDescription(rulesDescription);
// Send the embed
context.message.channel.send(embed);
}
}
} else {
} else { // If the rules file doesn't exist
let errorEmbed = new MessageEmbed()
.setColor(embedColor)
.setDescription(`${context.client.config.rules.rulesfile} doesn't exist`);
context.message.channel.send(errorEmbed);
}
} else {
} else { // If the user doesn't have the Admin role
let errorEmbed = new MessageEmbed()
.setColor(embedColor)
.setDescription("You do not have permission to run this command");
@ -57,4 +72,4 @@ class rules extends command {
}
}
module.exports = rules;
module.exports = rules;