From cf698be97e8e117dae1d36f180ac4768d86e094f Mon Sep 17 00:00:00 2001 From: Aleksei Date: Thu, 23 Jul 2020 14:22:12 -0400 Subject: [PATCH] Updated outfit retag command --- .../fsbot/commands/staff/RetagCommand.java | 103 ++++++++++++++---- 1 file changed, 79 insertions(+), 24 deletions(-) diff --git a/src/main/java/dev/salmonllama/fsbot/commands/staff/RetagCommand.java b/src/main/java/dev/salmonllama/fsbot/commands/staff/RetagCommand.java index ee9f059..5ecc903 100644 --- a/src/main/java/dev/salmonllama/fsbot/commands/staff/RetagCommand.java +++ b/src/main/java/dev/salmonllama/fsbot/commands/staff/RetagCommand.java @@ -7,18 +7,17 @@ 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.database.models.Outfit; 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.ReactionRetagConfirmationListener; -import dev.salmonllama.fsbot.utilities.Outfit; -import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities; -import dev.salmonllama.fsbot.utilities.exceptions.DiscordError; -import dev.salmonllama.fsbot.utilities.exceptions.OutfitNotFoundException; +import org.javacord.api.entity.message.embed.EmbedBuilder; +import java.awt.*; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -31,34 +30,90 @@ public class RetagCommand extends Command { @Override public CommandPermission permission() { return new CommandPermission(PermissionType.ROLE, BotConfig.STAFF_ROLE); } @Override public Collection aliases() { return new ArrayList<>(Collections.singletonList("retag")); } - private final DatabaseUtilities db; - - public RetagCommand(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 != 2) { - channel.sendMessage("You did that wrong mate. check the help command."); + channel.sendMessage("Improper usage"); return; } - try { - Outfit outfit = this.db.getOutfitFromId(args[0]); + // Get the outfit, confirm update through reaction + String outfitId = args[0]; + String newTag = args[1]; - channel.sendMessage(outfit.generateInfo().setTitle(String.format("Update tag to %s?", args[1]))).thenAcceptAsync(message -> { - message.addReaction(EmojiParser.parseToUnicode(":white_check_mark:")); - message.addReaction(EmojiParser.parseToUnicode(":octagonal_sign:")); - message.addReactionAddListener(new ReactionRetagConfirmationListener(author, message, outfit, db, outfit.getTag(), args[1])); + OutfitController.findById(outfitId).thenAcceptAsync(possibleOutfit -> { + possibleOutfit.ifPresentOrElse(outfit -> { + // Send info, confirmation, and add reaction listener + EmbedBuilder response = new EmbedBuilder() + .setTitle("Confirm Tag Edit") + .setThumbnail(outfit.getLink()) + .setAuthor(ctx.getApi().getUserById(outfit.getSubmitter()).join()) + .setUrl(outfit.getLink()) + .addField("Current Tag:", outfit.getTag()) + .addField("New Tag:", newTag); + + ctx.reply(response).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:"))) { + // Update the outfit + Outfit newOutfit = new Outfit.OutfitBuilder(outfit) + .setTag(newTag) + .setUpdated(new Timestamp(System.currentTimeMillis())) + .build(); + + OutfitController.update(newOutfit).thenAcceptAsync((Void) -> { + EmbedBuilder embed = new EmbedBuilder() + .setTitle("Outfit retagged successfully!") + .setDescription(String.format("Outfit %s will now display as %s", newOutfit.getId(), newOutfit.getTag())); + + msg.delete(); + ctx.reply(embed); + // TODO: Log the action in FSBot-log + }); + } else if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":octagonal_sign:"))) { + // Do nothing + msg.delete(); + EmbedBuilder embed = new EmbedBuilder() + .setTitle("Update Cancelled") + .setDescription("No modifications were made"); + + ctx.reply(embed); + } + }); + }); + }, () -> { + // Err, outfit not found + EmbedBuilder response = new EmbedBuilder() + .setTitle("Error occurred") + .setDescription("That ID was not found in the database") + .setColor(Color.RED); + + ctx.reply(response); }); - } - catch (OutfitNotFoundException e) { - channel.sendMessage(new DiscordError(e.getMessage()).get()); - } + }); + +// try { +// Outfit outfit = this.db.getOutfitFromId(args[0]); +// +// channel.sendMessage(outfit.generateInfo().setTitle(String.format("Update tag to %s?", args[1]))).thenAcceptAsync(message -> { +// message.addReaction(EmojiParser.parseToUnicode(":white_check_mark:")); +// message.addReaction(EmojiParser.parseToUnicode(":octagonal_sign:")); +// message.addReactionAddListener(new ReactionRetagConfirmationListener(author, message, outfit, db, outfit.getTag(), args[1])); +// }); +// } +// catch (OutfitNotFoundException e) { +// channel.sendMessage(new DiscordError(e.getMessage()).get()); +// } } }