Reworked outfit deletion

This commit is contained in:
Aleksei Fox 2020-07-11 01:54:56 -04:00
parent 9c8de32397
commit 712e982617
3 changed files with 80 additions and 25 deletions

View File

@ -7,17 +7,14 @@ package dev.salmonllama.fsbot.commands.staff;
import com.vdurmont.emoji.EmojiParser; import com.vdurmont.emoji.EmojiParser;
import dev.salmonllama.fsbot.config.BotConfig; import dev.salmonllama.fsbot.config.BotConfig;
import dev.salmonllama.fsbot.database.controllers.OutfitController;
import dev.salmonllama.fsbot.guthix.Command; import dev.salmonllama.fsbot.guthix.Command;
import dev.salmonllama.fsbot.guthix.CommandContext; import dev.salmonllama.fsbot.guthix.CommandContext;
import dev.salmonllama.fsbot.guthix.CommandPermission; import dev.salmonllama.fsbot.guthix.CommandPermission;
import dev.salmonllama.fsbot.guthix.PermissionType; import dev.salmonllama.fsbot.guthix.PermissionType;
import org.javacord.api.entity.channel.TextChannel; import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.user.User; import org.javacord.api.entity.message.embed.EmbedBuilder;
import dev.salmonllama.fsbot.listeners.ReactionDeleteConfirmationListener;
import dev.salmonllama.fsbot.utilities.Outfit;
import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities; 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.ArrayList;
import java.util.Arrays; 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 CommandPermission permission() { return new CommandPermission(PermissionType.ROLE, BotConfig.STAFF_ROLE); }
@Override public Collection<String> aliases() { return new ArrayList<>(Arrays.asList("removeoutfit", "remove")); } @Override public Collection<String> aliases() { return new ArrayList<>(Arrays.asList("removeoutfit", "remove")); }
private final DatabaseUtilities db;
public RemoveOutfitCommand(DatabaseUtilities db) {
this.db = db;
}
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) {
String[] args = ctx.getArgs(); String[] args = ctx.getArgs();
TextChannel channel = ctx.getChannel(); TextChannel channel = ctx.getChannel();
User author = ctx.getUser(); long authorId = ctx.getUser().getId();
if (args.length != 1) { if (args.length != 1) {
channel.sendMessage("You did that wrong, mate"); channel.sendMessage("You must supply a valid outfit ID.");
return; return;
} }
// get the outfit, confirm deletion through reaction. // get the outfit, confirm deletion through reaction.
try { String outfitId = args[0];
Outfit outfit = db.getOutfitFromId(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 -> { ctx.reply(embed).thenAcceptAsync(msg -> {
message.addReaction(EmojiParser.parseToUnicode(":white_check_mark:")); msg.addReaction(EmojiParser.parseToUnicode(":white_check_mark:"));
message.addReaction(EmojiParser.parseToUnicode(":octagonal_sign:")); msg.addReaction(EmojiParser.parseToUnicode(":octagonal_sign:"));
message.addReactionAddListener(new ReactionDeleteConfirmationListener(author, message, outfit, db));
}); msg.addReactionAddListener(event -> {
} if (event.getUser().getId() != authorId) {
catch (OutfitNotFoundException e) { return;
channel.sendMessage(new DiscordError(e.getMessage()).get()); }
}
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);
});
});
} }
} }

View File

@ -169,6 +169,26 @@ public class OutfitController {
}); });
} }
public static CompletableFuture<Void> delete(String id) {
return CompletableFuture.runAsync(() -> {
try {
deleteExec(id);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Void> 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 { private static void insertExec(Outfit outfit) throws SQLException {
if (outfit.getCreated() == null) { if (outfit.getCreated() == null) {
outfit.setCreated(new Timestamp(System.currentTimeMillis())); outfit.setCreated(new Timestamp(System.currentTimeMillis()));
@ -313,6 +333,10 @@ public class OutfitController {
return tags; return tags;
} }
private static void deleteExec(String id) throws SQLException {
FSDB.get().query("UPDATE outfits SET deleted = true WHERE id = ?", id);
}
private static Optional<Collection<Outfit>> extractMultiple(ResultSet rs) throws SQLException { private static Optional<Collection<Outfit>> extractMultiple(ResultSet rs) throws SQLException {
Collection<Outfit> outfits = new ArrayList<>(); Collection<Outfit> outfits = new ArrayList<>();

View File

@ -57,7 +57,7 @@ public class Guthix implements MessageCreateListener {
addCommand(new AddColorCommand()); addCommand(new AddColorCommand());
addCommand(new GetOutfitCommand()); addCommand(new GetOutfitCommand());
addCommand(new RetagCommand(db)); addCommand(new RetagCommand(db));
addCommand(new RemoveOutfitCommand(db)); addCommand(new RemoveOutfitCommand());
addCommand(new OutfitInfoCommand()); addCommand(new OutfitInfoCommand());
addCommand(new SetStatusCommand()); addCommand(new SetStatusCommand());
addCommand(new WelcomeMessageCommand()); addCommand(new WelcomeMessageCommand());