vylbot-app/src/commands/rules.ts
Vylpes ed8f5927c8
Feature/81 slash command support (#192)
* Update discord.js

* Migrate to slash commands

* Clean up imports

* Update permissions

* Fix guild-specific commands not showing up

* Fix changes requested
2022-09-18 11:57:22 +01:00

61 lines
1.8 KiB
TypeScript

import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { existsSync, readFileSync } from "fs";
import EmbedColours from "../constants/EmbedColours";
import { Command } from "../type/command";
interface IRules {
title?: string;
description?: string[];
image?: string;
footer?: string;
}
export default class Rules extends Command {
constructor() {
super();
super.CommandBuilder = new SlashCommandBuilder()
.setName("rules")
.setDescription("Send the rules embeds for this server")
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator);
}
public override async execute(interaction: CommandInteraction) {
if (!interaction.guildId) return;
if (!existsSync(`${process.cwd()}/data/rules/${interaction.guildId}.json`)) {
await interaction.reply('Rules file doesn\'t exist.');
return;
}
const rulesFile = readFileSync(`${process.cwd()}/data/rules/${interaction.guildId}.json`).toString();
const rules = JSON.parse(rulesFile) as IRules[];
const embeds: EmbedBuilder[] = [];
rules.forEach(rule => {
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle(rule.title || "Rules")
.setDescription(rule.description ? rule.description.join("\n") : "*none*");
if (rule.image) {
embed.setImage(rule.image);
}
if (rule.footer) {
embed.setFooter({ text: rule.footer });
}
embeds.push(embed);
});
const channel = interaction.channel;
if (!channel) {
return;
}
await channel.send({ embeds: embeds });
}
}