From 60ee5f2ae25d35c85527802a0b6e912b620efc37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 20 Dec 2022 00:59:26 +0100 Subject: [PATCH] Allow sender to delete their own urban command results --- .../message/UrbanDictionaryCommand.java | 21 +++++++++++++++++-- .../listeners/ButtonInteractionListener.java | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java index d784016..000b159 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java @@ -49,6 +49,8 @@ public class UrbanDictionaryCommand implements MessageCommand .withEmoji(Emoji.fromFormatted("⬅️")); static final Button nextPageButton = Button.primary("urban_nextpage", "Next") .withEmoji(Emoji.fromFormatted("➡️")); + static final Button deleteButton = Button.danger("urban_delete", "Delete") + .withEmoji(Emoji.fromFormatted("\uD83D\uDDD1️")); private static MessageEmbed buildEmbed(String term, String url, @@ -196,7 +198,8 @@ public class UrbanDictionaryCommand implements MessageCommand event.getChannel() .sendMessageEmbeds(embed) .addActionRow(previousPageButton.asDisabled(), //disabled by default because we're on page 0 - nextPageBtnLocal) + nextPageBtnLocal, + deleteButton) .queue(message -> { @@ -238,6 +241,19 @@ public class UrbanDictionaryCommand implements MessageCommand } } + public static void delete(ButtonInteractionEvent event) + { + String messageId = event.getMessageId(); + DatabaseSource database = Cache.getDatabaseSource(); + + // check if the user interacting is the same one who ran the command + if (!(database.isUserTrackedFor(event.getUser().getId(), messageId))) { + event.reply("❌ You did not run this command!").setEphemeral(true).queue(); + return; + } + + event.getInteraction().getMessage().delete().queue(); + } public static void changePage(ButtonInteractionEvent event, boolean increase) { @@ -298,6 +314,8 @@ public class UrbanDictionaryCommand implements MessageCommand components.add(nextPageButton.asEnabled()); } + components.add(deleteButton); + ActionRow currentRow = ActionRow.of(components); List actionRows = new ArrayList<>(Collections.singletonList(currentRow)); @@ -305,6 +323,5 @@ public class UrbanDictionaryCommand implements MessageCommand event.editComponents(actionRows).complete(); database.setUrbanPage(messageId, page); database.resetExpiryTimestamp(messageId); - } } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java index 8fb267c..c115276 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java @@ -24,6 +24,7 @@ public class ButtonInteractionListener extends ListenerAdapter // urban dictionary navigation case "urban_nextpage" -> UrbanDictionaryCommand.changePage(event, true); case "urban_previouspage" -> UrbanDictionaryCommand.changePage(event, false); + case "urban_delete" -> UrbanDictionaryCommand.delete(event); }