Allow sender to delete their own urban command results

This commit is contained in:
Bea 2022-12-20 00:59:26 +01:00
parent 8ca70dac78
commit 60ee5f2ae2
2 changed files with 20 additions and 2 deletions

View File

@ -49,6 +49,8 @@ public class UrbanDictionaryCommand implements MessageCommand
.withEmoji(Emoji.fromFormatted("⬅️")); .withEmoji(Emoji.fromFormatted("⬅️"));
static final Button nextPageButton = Button.primary("urban_nextpage", "Next") static final Button nextPageButton = Button.primary("urban_nextpage", "Next")
.withEmoji(Emoji.fromFormatted("➡️")); .withEmoji(Emoji.fromFormatted("➡️"));
static final Button deleteButton = Button.danger("urban_delete", "Delete")
.withEmoji(Emoji.fromFormatted("\uD83D\uDDD1"));
private static MessageEmbed buildEmbed(String term, private static MessageEmbed buildEmbed(String term,
String url, String url,
@ -196,7 +198,8 @@ public class UrbanDictionaryCommand implements MessageCommand
event.getChannel() event.getChannel()
.sendMessageEmbeds(embed) .sendMessageEmbeds(embed)
.addActionRow(previousPageButton.asDisabled(), //disabled by default because we're on page 0 .addActionRow(previousPageButton.asDisabled(), //disabled by default because we're on page 0
nextPageBtnLocal) nextPageBtnLocal,
deleteButton)
.queue(message -> .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) public static void changePage(ButtonInteractionEvent event, boolean increase)
{ {
@ -298,6 +314,8 @@ public class UrbanDictionaryCommand implements MessageCommand
components.add(nextPageButton.asEnabled()); components.add(nextPageButton.asEnabled());
} }
components.add(deleteButton);
ActionRow currentRow = ActionRow.of(components); ActionRow currentRow = ActionRow.of(components);
List<ActionRow> actionRows = new ArrayList<>(Collections.singletonList(currentRow)); List<ActionRow> actionRows = new ArrayList<>(Collections.singletonList(currentRow));
@ -305,6 +323,5 @@ public class UrbanDictionaryCommand implements MessageCommand
event.editComponents(actionRows).complete(); event.editComponents(actionRows).complete();
database.setUrbanPage(messageId, page); database.setUrbanPage(messageId, page);
database.resetExpiryTimestamp(messageId); database.resetExpiryTimestamp(messageId);
} }
} }

View File

@ -24,6 +24,7 @@ public class ButtonInteractionListener extends ListenerAdapter
// urban dictionary navigation // urban dictionary navigation
case "urban_nextpage" -> UrbanDictionaryCommand.changePage(event, true); case "urban_nextpage" -> UrbanDictionaryCommand.changePage(event, true);
case "urban_previouspage" -> UrbanDictionaryCommand.changePage(event, false); case "urban_previouspage" -> UrbanDictionaryCommand.changePage(event, false);
case "urban_delete" -> UrbanDictionaryCommand.delete(event);
} }