/blocklist
This commit is contained in:
194
src/commands/blocklist.ts
Normal file
194
src/commands/blocklist.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
|
||||
import { database, Badword } from "../data";
|
||||
import { IsNull } from "typeorm";
|
||||
import { getGuildSetting, isPremiumActive } from "../tools/data";
|
||||
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
|
||||
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 isPremium = isPremiumActive(settings.isPremiumUntil);
|
||||
|
||||
const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
|
||||
|
||||
switch (interaction.options.getSubcommand(true)) {
|
||||
case "get": {
|
||||
const guildBadWords = await database.getRepository(Badword).find({
|
||||
select: {
|
||||
value: true
|
||||
},
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
const globalBadWords = await database.getRepository(Badword).find({
|
||||
where: {
|
||||
guildID: IsNull()
|
||||
}
|
||||
});
|
||||
|
||||
interaction.reply({
|
||||
content: `\`\`\`Global bad word list\`\`\`\n||${globalBadWords.map((word) => word.value).reduce((prev, next) => prev + ", " + next, "").slice(2)} ||\n\`\`\`Local server bad word list (${guildBadWords.length}/${isPremium ? 100 : 10})\`\`\`\n||${guildBadWords.map((word) => word.value).reduce((prev, next) => prev + ", " + next, "").slice(2)} ||`,
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
break;
|
||||
}
|
||||
|
||||
case "add": {
|
||||
const count = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
|
||||
const limit = isPremium ? 100 : 10;
|
||||
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("Word added");
|
||||
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("Word removed");
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user