|
| 1 | +package net.javadiscord.javabot.systems.moderation.warn; |
| 2 | + |
| 3 | +import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; |
| 4 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 5 | +import net.dv8tion.jda.api.entities.User; |
| 6 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData; |
| 10 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 11 | +import net.dv8tion.jda.api.utils.FileUpload; |
| 12 | +import net.javadiscord.javabot.data.config.BotConfig; |
| 13 | +import net.javadiscord.javabot.systems.moderation.ModerationService; |
| 14 | +import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository; |
| 15 | +import net.javadiscord.javabot.systems.moderation.warn.model.Warn; |
| 16 | +import net.javadiscord.javabot.systems.notification.NotificationService; |
| 17 | +import net.javadiscord.javabot.util.ExceptionLogger; |
| 18 | +import net.javadiscord.javabot.util.Responses; |
| 19 | + |
| 20 | +import java.io.BufferedOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.OutputStreamWriter; |
| 23 | +import java.io.PipedInputStream; |
| 24 | +import java.io.PipedOutputStream; |
| 25 | +import java.io.PrintWriter; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.ExecutorService; |
| 30 | + |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +/** |
| 34 | + * <h3>This class represents the /warn add command.</h3> |
| 35 | + * This Subcommand allows staff-members to add a single warn to any user. |
| 36 | + */ |
| 37 | +public class WarnExportSubcommand extends SlashCommand.Subcommand { |
| 38 | + private final NotificationService notificationService; |
| 39 | + private final BotConfig botConfig; |
| 40 | + private final WarnRepository warnRepository; |
| 41 | + private final ExecutorService asyncPool; |
| 42 | + |
| 43 | + /** |
| 44 | + * The constructor of this class, which sets the corresponding {@link SubcommandData}. |
| 45 | + * @param notificationService The {@link NotificationService} |
| 46 | + * @param botConfig The main configuration of the bot |
| 47 | + * @param asyncPool The main thread pool for asynchronous operations |
| 48 | + * @param warnRepository DAO for interacting with the set of {@link net.javadiscord.javabot.systems.moderation.warn.model.Warn} objects. |
| 49 | + */ |
| 50 | + public WarnExportSubcommand(NotificationService notificationService, BotConfig botConfig, ExecutorService asyncPool, WarnRepository warnRepository) { |
| 51 | + this.notificationService = notificationService; |
| 52 | + this.botConfig = botConfig; |
| 53 | + this.warnRepository = warnRepository; |
| 54 | + this.asyncPool = asyncPool; |
| 55 | + setCommandData(new SubcommandData("export", "Exports a list of all warns of a user") |
| 56 | + .addOptions( |
| 57 | + new OptionData(OptionType.USER, "user", "The user to warn.", true) |
| 58 | + ) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void execute(@NotNull SlashCommandInteractionEvent event) { |
| 64 | + OptionMapping userMapping = event.getOption("user"); |
| 65 | + if (userMapping == null) { |
| 66 | + Responses.replyMissingArguments(event).queue(); |
| 67 | + return; |
| 68 | + } |
| 69 | + if (event.getGuild() == null) { |
| 70 | + Responses.replyGuildOnly(event).queue(); |
| 71 | + return; |
| 72 | + } |
| 73 | + User target = userMapping.getAsUser(); |
| 74 | + ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool); |
| 75 | + List<Warn> warns = service.getAllWarns(target.getIdLong()); |
| 76 | + PipedInputStream pis=new PipedInputStream(); |
| 77 | + try(PrintWriter pw=new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new PipedOutputStream(pis)), StandardCharsets.UTF_8))){ |
| 78 | + event.replyEmbeds(new EmbedBuilder() |
| 79 | + .setAuthor(target.getAsTag(), null, target.getAvatarUrl()) |
| 80 | + .setDescription("Export containing all warns of "+target.getAsMention()) |
| 81 | + .addField("Total number of warns", String.valueOf(warns.stream().count()), false) |
| 82 | + .addField("Total number of non-discarded warns (includes expired warns)", String.valueOf(warns.stream().filter(w->!w.isDiscarded()).count()), false) |
| 83 | + .addField("Total severity of non-discarded warns (includes expired warns)", String.valueOf(warns.stream().filter(w->!w.isDiscarded()).mapToInt(Warn::getSeverityWeight).sum()), false) |
| 84 | + .setFooter(target.getId()) |
| 85 | + .build()) |
| 86 | + .addFiles(FileUpload.fromData(pis, "warns"+target.getId()+".txt")) |
| 87 | + .queue(); |
| 88 | + for (Iterator<Warn> it = warns.iterator(); it.hasNext();) { |
| 89 | + Warn warn = it.next(); |
| 90 | + pw.println("Reason: \"" + warn.getReason()+"\""); |
| 91 | + pw.println("Severity: " + warn.getSeverity() + "(" + warn.getSeverityWeight() + ")"); |
| 92 | + pw.println("Warn ID: " + warn.getId()); |
| 93 | + pw.println("Warned by: " + warn.getWarnedBy()); |
| 94 | + pw.println("Warned at " + warn.getCreatedAt()); |
| 95 | + if (it.hasNext()) { |
| 96 | + pw.println(); |
| 97 | + pw.println("============"); |
| 98 | + pw.println(); |
| 99 | + } |
| 100 | + } |
| 101 | + } catch (IOException e) { |
| 102 | + ExceptionLogger.capture(e, getClass().getSimpleName()); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments