prune command, fixes #339 #340

This commit is contained in:
Axel Olausson Holtenäs 2022-06-12 01:10:08 +02:00
parent 24e44f7235
commit b3f3fb1727
No known key found for this signature in database
GPG key ID: 7BF6826B76382CBA
3 changed files with 116 additions and 0 deletions

View file

@ -0,0 +1,22 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { CommandInteraction } from "discord.js";
import modules from "./modules";
export const moduleData = modules;
export const builder = new SlashCommandBuilder()
.setName("moderation")
.setDescription("Moderation.")
.addSubcommand(modules.prune.builder);
export const execute = async (interaction: CommandInteraction) => {
switch (interaction.options.getSubcommand()) {
case "prune":
return modules.prune.execute(interaction);
default:
throw new Error(
`Unknown subcommand: ${interaction.options.getSubcommand()}`
);
}
};

View file

@ -0,0 +1,3 @@
import prune from "./prune";
export default { prune };

View file

@ -0,0 +1,91 @@
// Dependencies
import {
CommandInteraction,
Permissions,
} from "discord.js";
// Configurations
import getEmbedConfig from "../../../../../helpers/getEmbedConfig";
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
// Function
export default {
metadata: {
guildOnly: true,
ephemeral: false,
permissions: [Permissions.FLAGS.MANAGE_MESSAGES],
},
builder: (command: SlashCommandSubcommandBuilder) => {
return command
.setName("prune")
.setDescription("Prune messages!")
.addIntegerOption((option) =>
option
.setName("count")
.setDescription("How many messages you want to prune.")
.setRequired(true)
)
.addBooleanOption((option) =>
option.setName("bots").setDescription("Include bots.")
);
},
execute: async (interaction: CommandInteraction) => {
const { successColor, footerText, footerIcon } = await getEmbedConfig(
interaction.guild
);
const count = interaction.options.getInteger("count");
if (count == null) return;
const bots = interaction.options.getBoolean("bots");
if (count < 1 || count > 100) {
const interactionEmbed = {
title: "[:police_car:] Prune",
description: `You can only prune between 1 and 100 messages.`,
color: successColor,
timestamp: new Date(),
footer: {
iconURL: footerIcon,
text: footerText,
},
};
await interaction.editReply({
embeds: [interactionEmbed],
});
return;
}
if (interaction?.channel?.type !== "GUILD_TEXT") return;
await interaction.channel.messages.fetch().then(async (messages) => {
const messagesToDelete = (
bots
? messages.filter((m) => m?.interaction?.id !== interaction.id)
: messages.filter(
(m) =>
m?.interaction?.id !== interaction.id && m?.author?.bot !== true
)
).first(count);
if (interaction?.channel?.type !== "GUILD_TEXT") return;
await interaction.channel
.bulkDelete(messagesToDelete, true)
.then(async () => {
const interactionEmbed = {
title: "[:police_car:] Prune",
description: `Successfully pruned \`${count}\` messages.`,
color: successColor,
timestamp: new Date(),
footer: {
iconURL: footerIcon,
text: footerText,
},
};
await interaction.editReply({
embeds: [interactionEmbed],
});
});
});
},
};