180 lines
6.3 KiB
TypeScript

import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { database, Badword } from "../data";
import { IsNull } from "typeorm";
import { getGuildSetting } from "../tools/data";
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
import { getGuildChannel } from "../tools/discord";
import { Color, Emoji } from "../tools/design";
import { execute as showBlocklistRunner } from "./showblocklist";
const builder = new SlashCommandBuilder();
builder.setName("blocklist");
builder.setDescription("Configures the servers blocklist");
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
builder.setDMPermission(false);
builder.addSubcommand((builder) => {
builder.setName("add");
builder.setDescription("Adds a word to the servers blocklist");
builder.addStringOption((option) => {
option.setName("word");
option.setDescription("The word to add");
option.setRequired(true);
option.setMaxLength(50);
return option;
});
return builder;
});
builder.addSubcommand((builder) => {
builder.setName("remove");
builder.setDescription("Removes a word from the servers blocklist");
builder.addStringOption((option) => {
option.setName("word");
option.setDescription("The word to remove");
option.setRequired(true);
option.setMaxLength(50);
return option;
});
return builder;
});
builder.addSubcommand((builder) => {
builder.setName("get");
builder.setDescription("Returns all words from the servers blocklist");
return builder;
});
async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
if (!interaction.guildId) throw new Error("Command was executed in DM context");
const settings = await getGuildSetting(interaction.guildId);
const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
switch (interaction.options.getSubcommand(true)) {
case "get": {
await showBlocklistRunner(interaction);
break;
}
case "add": {
const count = await database.getRepository(Badword).count({
where: {
guildID: interaction.guildId
}
});
const limit = 100;
if (count >= limit) {
const embed = getFailedEmbed();
embed.setDescription(`You reached the word limit for your guild. Please delete a word before adding a new one`);
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch(() => {});
return;
}
const word = interaction.options.getString("word", true).toLowerCase();
const exists = await database.getRepository(Badword).count({
where: [
{guildID: interaction.guildId, value: word},
{guildID: IsNull(), value: word}
]
}) > 0;
if (exists) {
const embed = getFailedEmbed();
embed.setDescription(`"${word}" already exists in the blocklist`);
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch(() => {});
return;
}
const entry = new Badword();
entry.guildID = interaction.guildId;
entry.value = word;
await database.getRepository(Badword).save(entry);
const embed = getSuccessEmbed();
embed.setDescription(`"${word}" has been added to the blocklist`);
interaction.reply({
embeds: [embed],
ephemeral: true
});
const logMessage = getDefaultEmbed();
logMessage.setTitle(`${Emoji.SETTINGS} Word added`);
logMessage.setColor(Color.INFORMING_BLUE);
logMessage.setDescription(`"||${word}||" has been added to the blocklist`);
logMessage.addFields({
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
if (logChannel && logChannel.isTextBased()) {
logChannel.send({
embeds: [logMessage]
}).catch(() => {});
}
break;
}
case "remove": {
const word = interaction.options.getString("word", true).toLowerCase();
const entry = await database.getRepository(Badword).findOne({
where: {
guildID: interaction.guildId,
value: word
}
});
if (!entry) {
const embed = getFailedEmbed();
embed.setDescription(`"${word}" was not found in the blocklist`);
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch(() => {});
return;
}
await database.getRepository(Badword).delete({
id: entry.id
});
const embed = getSuccessEmbed();
embed.setDescription(`"${word}" has been removed from the blocklist`);
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch(() => {});
const logMessage = getDefaultEmbed();
logMessage.setTitle(`${Emoji.SETTINGS} Word removed`);
logMessage.setColor(Color.INFORMING_BLUE);
logMessage.setDescription(`"||${word}||" has been removed from the blocklist`);
logMessage.addFields({
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
if (logChannel && logChannel.isTextBased()) {
logChannel.send({
embeds: [logMessage]
}).catch(() => {});
}
break;
}
default: {
throw new Error(`"${interaction.options.getSubcommand(true)}" cannot be executed`);
}
}
}
export {
builder,
execute
}