vylbot-app/commands/rules.js

76 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-02-17 18:12:45 +00:00
// Required Components
2020-11-04 19:58:54 +00:00
const { command } = require('vylbot-core');
const { MessageEmbed } = require('discord.js');
const { existsSync, readFileSync } = require('fs');
// Command variables
2020-11-04 19:58:54 +00:00
const embedColor = "0x3050ba";
// Command class
2020-11-04 19:58:54 +00:00
class rules extends command {
constructor() {
// Set the command's run method, description, and category
2020-11-04 19:58:54 +00:00
super("rules");
super.description = "Generates the rules embeds from the rules.txt file";
2021-02-17 18:12:45 +00:00
super.category = "Admin";
2020-11-04 19:58:54 +00:00
// Require in the config the name of the admin role and the rules file name
2020-11-04 19:58:54 +00:00
super.requiredConfigs = "adminrole";
super.requiredConfigs = "rulesfile";
}
// Run method
2020-11-04 19:58:54 +00:00
rules(context) {
// If the user is an Admin (has the admin role)
2020-11-04 19:58:54 +00:00
if (context.message.member.roles.cache.find(role => role.name == context.client.config.rules.adminrole)) {
// If the rulesfile exists
2020-11-04 19:58:54 +00:00
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
2020-11-04 19:58:54 +00:00
let rulesText = readFileSync(context.client.config.rules.rulesfile).toString();
rulesText = rulesText.split("> ");
// Loop through each embed to be sent
2020-11-04 19:58:54 +00:00
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
2020-11-04 19:58:54 +00:00
if (rulesText[i].charAt(0) == '#') {
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setImage(rulesText[i].substring(1));
context.message.channel.send(embed);
} 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
2021-02-17 18:12:45 +00:00
const rulesLines = rulesText[i].split("\n");
const rulesTitle = rulesLines[0];
const rulesDescription = rulesLines.slice(1).join("\n");
2020-11-04 19:58:54 +00:00
// Create the embed with the specified information above
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setTitle(rulesTitle)
.setColor(embedColor)
.setDescription(rulesDescription);
// Send the embed
2020-11-04 19:58:54 +00:00
context.message.channel.send(embed);
}
}
} else { // If the rules file doesn't exist
2021-02-17 18:12:45 +00:00
const errorEmbed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription(`${context.client.config.rules.rulesfile} doesn't exist`);
context.message.channel.send(errorEmbed);
}
} else { // If the user doesn't have the Admin role
2021-02-17 18:12:45 +00:00
const errorEmbed = new MessageEmbed()
2020-11-04 19:58:54 +00:00
.setColor(embedColor)
.setDescription("You do not have permission to run this command");
context.message.channel.send(errorEmbed);
}
}
}
module.exports = rules;