diff --git a/.gitignore b/.gitignore index 0ce9cdb2ce..563dbf8375 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ logviewer/types.d.ts logviewer/pnpm-lock.yaml logviewer/webpack.config.js logviewer/webpack.generated.js + +### Mac Patch ### +.DS_Store diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 3961b19ed8..45afe45147 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -3,6 +3,7 @@ import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.basic.DatabaseCommand; import org.togetherjava.tjbot.commands.basic.PingCommand; +import org.togetherjava.tjbot.commands.moderation.*; import org.togetherjava.tjbot.commands.mathcommands.TeXCommand; import org.togetherjava.tjbot.commands.tags.TagCommand; import org.togetherjava.tjbot.commands.tags.TagManageCommand; @@ -41,6 +42,6 @@ public enum Commands { // available. return List.of(new PingCommand(), new DatabaseCommand(database), new TeXCommand(), new TagCommand(tagSystem), new TagManageCommand(tagSystem), - new TagsCommand(tagSystem)); + new TagsCommand(tagSystem), new PurgeCommand()); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java new file mode 100644 index 0000000000..433e96be17 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.java @@ -0,0 +1,73 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; + +import java.util.Objects; + + +/** + * When triggered with {@code /prune "number_of_messages"}, the bot will check if the user has + * perms. Then it will check if itself has perms to delete messages. If it does the bot will delete + * that amount of messages. {@code I have deleted this amount of message + amount}. + * + */ +public final class PurgeCommand extends SlashCommandAdapter { + private static final Logger logger = LoggerFactory.getLogger(PurgeCommand.class); + private static final String NUMBER_OF_MESSAGES_TO_DELETE = "number_of_messages"; + private static final String USER_MESSAGES = "user"; + + public PurgeCommand() { + super("purge", "Use this command to delete a batch of messages", + SlashCommandVisibility.GUILD); + + getData() + .addOption(OptionType.INTEGER, NUMBER_OF_MESSAGES_TO_DELETE, + "The number of messages you want to delete. 1 to 100", true); + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + final Member author = event.getMember(); + final TextChannel channel = event.getTextChannel(); + + if (!Objects.requireNonNull(author).hasPermission(Permission.MESSAGE_MANAGE)) { + event.reply("You are missing MESSAGE_MANAGE permission to delete these the message") + .setEphemeral(true) + .queue(); + return; + } + + final Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); + + if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { + event.reply("I am missing MESSAGE_MANAGE permission which means I am unable to delete messages in this server.") + .setEphemeral(true) + .queue(); + + logger.error("The bot does not have MESSAGE_MANAGE permission on the server '{}' ", + event.getGuild().getId()); + return; + } + + int amount = Math.toIntExact( + Objects.requireNonNull(event.getOption(NUMBER_OF_MESSAGES_TO_DELETE)).getAsLong()); + if (amount > 100 || amount < 1) { + event.reply("You can only delete 1 to 200 messages").setEphemeral(true).queue(); + } else { + channel.getHistory().retrievePast(amount).queue(channel::purgeMessages); + event.reply("I have deleted this amount of messages " + amount) + .setEphemeral(true) + .queue(); + logger.info(" '{}' deleted this amount of messages '{}'", author, amount); + } + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java new file mode 100644 index 0000000000..7971cce2ff --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/package-info.java @@ -0,0 +1,4 @@ +/** + * This package offers all the moderation commands in the application. + */ +package org.togetherjava.tjbot.commands.moderation;