2022-09-18 11:57:22 +01:00
|
|
|
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
2022-04-24 14:46:37 +01:00
|
|
|
import { existsSync, readFileSync } from "fs";
|
2022-09-18 11:57:22 +01:00
|
|
|
import EmbedColours from "../constants/EmbedColours";
|
2022-04-24 14:46:37 +01:00
|
|
|
import { Command } from "../type/command";
|
|
|
|
|
|
|
|
interface IRules {
|
|
|
|
title?: string;
|
|
|
|
description?: string[];
|
|
|
|
image?: string;
|
|
|
|
footer?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Rules extends Command {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
super.CommandBuilder = new SlashCommandBuilder()
|
|
|
|
.setName("rules")
|
|
|
|
.setDescription("Send the rules embeds for this server")
|
|
|
|
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator);
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
public override async execute(interaction: CommandInteraction) {
|
|
|
|
if (!interaction.guildId) return;
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
if (!existsSync(`${process.cwd()}/data/rules/${interaction.guildId}.json`)) {
|
|
|
|
await interaction.reply('Rules file doesn\'t exist.');
|
2022-06-05 14:11:01 +01:00
|
|
|
return;
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const rulesFile = readFileSync(`${process.cwd()}/data/rules/${interaction.guildId}.json`).toString();
|
2022-04-24 14:46:37 +01:00
|
|
|
const rules = JSON.parse(rulesFile) as IRules[];
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const embeds: EmbedBuilder[] = [];
|
2022-12-27 15:18:55 +00:00
|
|
|
|
|
|
|
if (rules.length == 0) {
|
|
|
|
await interaction.reply({ content: "No rules have been supplied within code base for this server.", ephemeral: true });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-24 14:46:37 +01:00
|
|
|
rules.forEach(rule => {
|
2022-09-18 11:57:22 +01:00
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor(EmbedColours.Ok)
|
|
|
|
.setTitle(rule.title || "Rules")
|
|
|
|
.setDescription(rule.description ? rule.description.join("\n") : "*none*");
|
2022-12-27 15:18:55 +00:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
if (rule.image) {
|
|
|
|
embed.setImage(rule.image);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rule.footer) {
|
|
|
|
embed.setFooter({ text: rule.footer });
|
|
|
|
}
|
2022-04-24 14:46:37 +01:00
|
|
|
|
|
|
|
embeds.push(embed);
|
|
|
|
});
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const channel = interaction.channel;
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
if (!channel) {
|
2022-12-27 15:18:55 +00:00
|
|
|
await interaction.reply({ content: "Channel not found.", ephemeral: true });
|
2022-09-18 11:57:22 +01:00
|
|
|
return;
|
2022-06-05 14:11:01 +01:00
|
|
|
}
|
2022-12-27 15:18:55 +00:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
await channel.send({ embeds: embeds });
|
2022-12-27 15:18:55 +00:00
|
|
|
|
|
|
|
const successEmbed = new EmbedBuilder()
|
|
|
|
.setColor(EmbedColours.Ok)
|
|
|
|
.setTitle("Success")
|
|
|
|
.setDescription("The rules have sent to this channel successfully");
|
|
|
|
|
|
|
|
await interaction.reply({ embeds: [ successEmbed ], ephemeral: true });
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
|
|
|
}
|