From 24818bcb4494893ed5ddfc25abab03e6a0ebcbad Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 2 Dec 2021 14:36:24 +0000 Subject: [PATCH] Migrate rules command --- .env.template | 2 ++ commands/rules.js | 70 --------------------------------------- data/partner/partner.json | 14 -------- src/commands/rules.ts | 46 +++++++++++++++++++++++++ src/vylbot.ts | 3 +- 5 files changed, 50 insertions(+), 85 deletions(-) delete mode 100644 commands/rules.js delete mode 100644 data/partner/partner.json create mode 100644 src/commands/rules.ts diff --git a/.env.template b/.env.template index 4d566fd..631f40c 100644 --- a/.env.template +++ b/.env.template @@ -23,6 +23,8 @@ COMMANDS_DISABLED_MESSAGE=This command is disabled. COMMANDS_ROLE_ROLES=Notify,VotePings,ProjectUpdates +COMMANDS_RULES_FILE=data/rules/rules.json + EMBED_COLOUR=0x3050ba EMBED_COLOUR_ERROR=0xD52803 diff --git a/commands/rules.js b/commands/rules.js deleted file mode 100644 index 266e504..0000000 --- a/commands/rules.js +++ /dev/null @@ -1,70 +0,0 @@ -// 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 = "Admin"; - - // 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)) { - const rulesJson = readFileSync(context.client.config.rules.rulesfile); - const rules = JSON.parse(rulesJson); - - for (let i = 0; i < rules.length; i++) { - const rule = rules[i]; - const embed = new MessageEmbed(); - - embed.setColor(embedColor); - - 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 = ""; - - for (let j = 0; j < rule.description.length; j++) { - const line = rule.description[j]; - description += `${line}\n`; - } - - embed.setDescription(description); - } - - context.message.channel.send(embed); - } - } else { // If the rules file doesn't exist - const errorEmbed = new MessageEmbed() - .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 - const errorEmbed = new MessageEmbed() - .setColor(embedColor) - .setDescription("You do not have permission to run this command"); - - context.message.channel.send(errorEmbed); - } - } -} - -module.exports = rules; diff --git a/data/partner/partner.json b/data/partner/partner.json deleted file mode 100644 index 73c3d34..0000000 --- a/data/partner/partner.json +++ /dev/null @@ -1,14 +0,0 @@ -[ - { - "name": "Cuzethstan", - "description": "Cuzeth Server. Yes.", - "invite": "http://discord.gg/uhEFNw7", - "icon": "https://cdn.discordapp.com/icons/720177983016665251/a_e4250e57b26559c6609dfe562774ee27.gif" - }, - { - "name": "Boblin", - "description": "Official server of the... Boblin?\n- Multiple Topics\n- Lots of Very Active Members", - "invite": "https://discord.gg/Td4uzVu", - "icon": "https://cdn.discordapp.com/attachments/464708407010787328/487824441846267907/image0.png" - } -] \ No newline at end of file diff --git a/src/commands/rules.ts b/src/commands/rules.ts new file mode 100644 index 0000000..9b9af9f --- /dev/null +++ b/src/commands/rules.ts @@ -0,0 +1,46 @@ +import { existsSync, readFileSync } from "fs"; +import { Command, ICommandContext } from "vylbot-core"; +import ErrorEmbed from "../helpers/ErrorEmbed"; +import PublicEmbed from "../helpers/PublicEmbed"; + +interface IRules { + title?: string; + description?: string[]; + image?: string; + footer?: string; +} + +export default class Rules extends Command { + constructor() { + super(); + + super._category = "Admin"; + super._roles = [ + process.env.ROLES_MODERATOR! + ]; + } + + public override execute(context: ICommandContext) { + if (!existsSync(process.env.COMMANDS_RULES_FILE!)) { + const errorEmbed = new ErrorEmbed(context, "Rules file doesn't exist"); + errorEmbed.SendToCurrentChannel(); + return; + } + + const rulesFile = readFileSync(`${process.cwd()}/${process.env.COMMANDS_RULES_FILE}`).toString(); + const rules = JSON.parse(rulesFile) as IRules[]; + + const embeds: PublicEmbed[] = []; + + rules.forEach(rule => { + const embed = new PublicEmbed(context, rule.title || "", rule.description?.join("\n") || ""); + + embed.setImage(rule.image || ""); + embed.setFooter(rule.footer || ""); + + embeds.push(embed); + }); + + embeds.forEach(x => x.SendToCurrentChannel()); + } +} \ No newline at end of file diff --git a/src/vylbot.ts b/src/vylbot.ts index 301c8f0..80f7a25 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -11,7 +11,8 @@ const requiredConfigs = [ "CHANNELS_LOGS_MESSAGE", "CHANNELS_LOGS_MEMBER", "CHANNELS_LOGS_MOD", - "COMMANDS_ROLE_ROLES" + "COMMANDS_ROLE_ROLES", + "COMMANDS_RULES_FILE" ]; requiredConfigs.forEach(config => {