diff --git a/src/main/java/dev/salmonllama/fsbot/commands/staff/RemoveOutfitCommand.java b/src/main/java/dev/salmonllama/fsbot/commands/staff/RemoveOutfitCommand.java index deb3424..f8ea40e 100644 --- a/src/main/java/dev/salmonllama/fsbot/commands/staff/RemoveOutfitCommand.java +++ b/src/main/java/dev/salmonllama/fsbot/commands/staff/RemoveOutfitCommand.java @@ -7,17 +7,14 @@ package dev.salmonllama.fsbot.commands.staff; import com.vdurmont.emoji.EmojiParser; import dev.salmonllama.fsbot.config.BotConfig; +import dev.salmonllama.fsbot.database.controllers.OutfitController; import dev.salmonllama.fsbot.guthix.Command; import dev.salmonllama.fsbot.guthix.CommandContext; import dev.salmonllama.fsbot.guthix.CommandPermission; import dev.salmonllama.fsbot.guthix.PermissionType; import org.javacord.api.entity.channel.TextChannel; -import org.javacord.api.entity.user.User; -import dev.salmonllama.fsbot.listeners.ReactionDeleteConfirmationListener; -import dev.salmonllama.fsbot.utilities.Outfit; +import org.javacord.api.entity.message.embed.EmbedBuilder; import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities; -import dev.salmonllama.fsbot.utilities.exceptions.DiscordError; -import dev.salmonllama.fsbot.utilities.exceptions.OutfitNotFoundException; import java.util.ArrayList; import java.util.Arrays; @@ -31,35 +28,69 @@ public class RemoveOutfitCommand extends Command { @Override public CommandPermission permission() { return new CommandPermission(PermissionType.ROLE, BotConfig.STAFF_ROLE); } @Override public Collection aliases() { return new ArrayList<>(Arrays.asList("removeoutfit", "remove")); } - private final DatabaseUtilities db; - - public RemoveOutfitCommand(DatabaseUtilities db) { - this.db = db; - } - @Override public void onCommand(CommandContext ctx) { String[] args = ctx.getArgs(); TextChannel channel = ctx.getChannel(); - User author = ctx.getUser(); + long authorId = ctx.getUser().getId(); if (args.length != 1) { - channel.sendMessage("You did that wrong, mate"); + channel.sendMessage("You must supply a valid outfit ID."); return; } // get the outfit, confirm deletion through reaction. - try { - Outfit outfit = db.getOutfitFromId(args[0]); + String outfitId = args[0]; + OutfitController.findById(outfitId).thenAcceptAsync(possibleOutfit -> { + possibleOutfit.ifPresentOrElse(outfit -> { + // Send outfit info, react with selectors, add a listener to the message + EmbedBuilder embed = new EmbedBuilder() + .setTitle("Confirm Outfit Deletion") + .setThumbnail(outfit.getLink()) + .setAuthor(ctx.getApi().getYourself()) + .setUrl(outfit.getLink()) + .setFooter(String.format("Tag: %s", outfit.getTag())) + .addField("Added", outfit.getCreated().toString(), true) + .addField("Updated", outfit.getUpdated().toString(), true) + .addField("Submitted by:", ctx.getApi().getUserById(outfit.getSubmitter()).join().getDiscriminatedName()) + .addField("Deleted", outfit.isDeleted() ? "True" : "False", true) + .addField("Featured", outfit.isFeatured() ? "True" : "False", true); - channel.sendMessage(outfit.generateInfo().setTitle("Confirm Outfit Deletion")).thenAccept(message -> { - message.addReaction(EmojiParser.parseToUnicode(":white_check_mark:")); - message.addReaction(EmojiParser.parseToUnicode(":octagonal_sign:")); - message.addReactionAddListener(new ReactionDeleteConfirmationListener(author, message, outfit, db)); - }); - } - catch (OutfitNotFoundException e) { - channel.sendMessage(new DiscordError(e.getMessage()).get()); - } + ctx.reply(embed).thenAcceptAsync(msg -> { + msg.addReaction(EmojiParser.parseToUnicode(":white_check_mark:")); + msg.addReaction(EmojiParser.parseToUnicode(":octagonal_sign:")); + + msg.addReactionAddListener(event -> { + if (event.getUser().getId() != authorId) { + return; + } + + if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":white_check_mark:"))) { + // Delete the outfit + OutfitController.delete(outfit.getId()); + + EmbedBuilder response = new EmbedBuilder() + .setTitle("Deletion Successful!") + .setDescription(String.format("Outfit %s marked as deleted", outfit.getId())); + ctx.reply(response); + + } else if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":octagonal_sign:"))) { + // Do nothing + EmbedBuilder response = new EmbedBuilder() + .setTitle("Deletion Aborted") + .setDescription(String.format("No modifications were made to %s", outfit.getId())); + + ctx.reply(response); + } + }); + }); + }, () -> { + EmbedBuilder response = new EmbedBuilder() + .setTitle("Outfit not Found") + .setDescription(String.format("ID %s does not exist", outfitId)); + + ctx.reply(response); + }); + }); } } diff --git a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java index cbd7f13..15bb82f 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java @@ -169,6 +169,26 @@ public class OutfitController { }); } + public static CompletableFuture delete(String id) { + return CompletableFuture.runAsync(() -> { + try { + deleteExec(id); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture delete(Outfit outfit) { + return CompletableFuture.runAsync(() -> { + try { + deleteExec(outfit.getId()); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + private static void insertExec(Outfit outfit) throws SQLException { if (outfit.getCreated() == null) { outfit.setCreated(new Timestamp(System.currentTimeMillis())); @@ -313,6 +333,10 @@ public class OutfitController { return tags; } + private static void deleteExec(String id) throws SQLException { + FSDB.get().query("UPDATE outfits SET deleted = true WHERE id = ?", id); + } + private static Optional> extractMultiple(ResultSet rs) throws SQLException { Collection outfits = new ArrayList<>(); diff --git a/src/main/java/dev/salmonllama/fsbot/guthix/Guthix.java b/src/main/java/dev/salmonllama/fsbot/guthix/Guthix.java index bf76765..802e429 100644 --- a/src/main/java/dev/salmonllama/fsbot/guthix/Guthix.java +++ b/src/main/java/dev/salmonllama/fsbot/guthix/Guthix.java @@ -57,7 +57,7 @@ public class Guthix implements MessageCreateListener { addCommand(new AddColorCommand()); addCommand(new GetOutfitCommand()); addCommand(new RetagCommand(db)); - addCommand(new RemoveOutfitCommand(db)); + addCommand(new RemoveOutfitCommand()); addCommand(new OutfitInfoCommand()); addCommand(new SetStatusCommand()); addCommand(new WelcomeMessageCommand());