vylbot-app/commands/rules.js

71 lines
2.6 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)) {
2021-04-22 18:01:40 +01:00
const rulesJson = readFileSync(context.client.config.rules.rulesfile);
const rules = JSON.parse(rulesJson);
2020-11-04 19:58:54 +00:00
2021-04-22 18:01:40 +01:00
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
const embed = new MessageEmbed();
2020-11-04 19:58:54 +00:00
2021-04-22 18:01:40 +01:00
embed.setColor(embedColor);
2020-11-04 19:58:54 +00:00
2021-04-22 18:01:40 +01:00
if (rule.image) embed.setImage(rule.image);
if (rule.title) embed.setTitle(rule.title);
if (rule.footer) embed.setFooter(rule.footer);
if (rule.description) {
let description = "";
2020-11-04 19:58:54 +00:00
2021-04-22 18:01:40 +01:00
for (let j = 0; j < rule.description.length; j++) {
const line = rule.description[j];
description += `${line}\n`;
}
embed.setDescription(description);
2020-11-04 19:58:54 +00:00
}
2021-04-22 18:01:40 +01:00
context.message.channel.send(embed);
2020-11-04 19:58:54 +00:00
}
} 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;