| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8dece97 commit 3bc9739
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ | |||
| 6 | 6 | import net.dv8tion.jda.api.entities.Message; | |
| 7 | 7 | import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | |
| 8 | 8 | import net.dv8tion.jda.api.interactions.commands.CommandInteraction; | |
| 9 | + import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | ||
| 9 | 10 | import org.jetbrains.annotations.Contract; | |
| 10 | 11 | import org.jetbrains.annotations.NotNull; | |
| 11 | 12 | ||
@@ -44,7 +45,7 @@ public static void sendCode(Code code, @Nonnull CommandInteraction event, Messag | |||
| 44 | 45 | MessageChannel channel = target.getChannel(); | |
| 45 | 46 | ||
| 46 | 47 | if (messages.size() > MAX_MESSAGES) { | |
| 47 | - Responses.errorWithTitle(event.getHook(), "Code out of Bound", "The formatted result is too large to send. Please provide a smaller code snippet or use a paste service instead." | ||
| 48 | + Responses.errorWithTitle(event.getHook(), "Code out of Bounds", "The formatted result is too large to send. Please provide a smaller code snippet or use a paste service instead." | ||
| 48 | 49 | ).queue(); | |
| 49 | 50 | return; | |
| 50 | 51 | } | |
@@ -55,26 +56,39 @@ public static void sendCode(Code code, @Nonnull CommandInteraction event, Messag | |||
| 55 | 56 | ||
| 56 | 57 | private static void sendChunksInOrder(MessageChannel channel, List<String> messages, int index, Message target, @Nonnull CommandInteraction event, long firstMessageID) { | |
| 57 | 58 | if (index >= messages.size()) { | |
| 58 | - event.getHook().sendMessage("Your message has been set. If needed, you can change the language used for syntax highlighting below.") | ||
| 59 | - .setEphemeral(true) | ||
| 60 | - .setComponents(FormatCodeInteractionHandler.buildLanguageMenu(event.getUser().getIdLong(), messages.size(), firstMessageID)) | ||
| 61 | - .queue(); | ||
| 59 | + event.getHook().deleteOriginal().queue(_ -> | ||
| 60 | + event.getHook().sendMessage("Your message has been sent. If needed, you can change the language used for syntax highlighting below.") | ||
| 61 | + .setEphemeral(true) | ||
| 62 | + .setComponents(FormatCodeInteractionHandler.buildLanguageMenu(event.getUser().getIdLong(), messages.size(), firstMessageID)) | ||
| 63 | + .queue() | ||
| 64 | + ); | ||
| 62 | 65 | return; | |
| 63 | 66 | } | |
| 64 | - var action = channel.sendMessage(messages.get(index)) | ||
| 67 | + MessageCreateAction action = channel.sendMessage(messages.get(index)) | ||
| 65 | 68 | .setAllowedMentions(List.of()); | |
| 66 | 69 | ||
| 67 | 70 | if (index == messages.size() - 1) { | |
| 68 | - action.setComponents(buildActionRow(target, event.getUser().getIdLong(), messages.size())); | ||
| 71 | + if(messages.size() >1) { | ||
| 72 | + action.setComponents(buildMultiMessageActionRow(target, event.getUser().getIdLong(), messages.size(), firstMessageID)); | ||
| 73 | + } else { | ||
| 74 | + action.setComponents(buildActionRow(target,event.getUser().getIdLong())); | ||
| 75 | + } | ||
| 69 | 76 | } | |
| 70 | 77 | ||
| 71 | 78 | action.queue(sent -> sendChunksInOrder(channel, messages, index + 1, target, event, index == 0 ? sent.getIdLong() : firstMessageID)); | |
| 72 | 79 | } | |
| 73 | 80 | ||
| 74 | - @Contract("_,_,_ -> new") | ||
| 75 | - static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId, int total) { | ||
| 81 | + @Contract("_,_,_,_ -> new") | ||
| 82 | + static @NotNull ActionRow buildMultiMessageActionRow(@NotNull Message target, long requesterId, int total, long firstMessageID) { | ||
| 76 | 83 | return ActionRow.of( | |
| 77 | - FormatCodeInteractionHandler.createDeleteAllButton(requesterId, total), | ||
| 84 | + FormatCodeInteractionHandler.createDeleteAllButton(requesterId, total, firstMessageID ), | ||
| 85 | + Button.link(target.getJumpUrl(), "View Original")); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + @Contract("_,_-> new") | ||
| 89 | + static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId) { | ||
| 90 | + return ActionRow.of( | ||
| 91 | + InteractionUtils.createDeleteButton(requesterId), | ||
| 78 | 92 | Button.link(target.getJumpUrl(), "View Original")); | |
| 79 | 93 | } | |
| 80 | 94 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ | |||
| 12 | 12 | import net.dv8tion.jda.api.entities.Member; | |
| 13 | 13 | import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; | |
| 14 | 14 | import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; | |
| 15 | + import net.dv8tion.jda.api.interactions.components.ComponentInteraction; | ||
| 15 | 16 | import org.jspecify.annotations.NonNull; | |
| 16 | 17 | import xyz.dynxsty.dih4jda.interactions.components.ButtonHandler; | |
| 17 | 18 | import xyz.dynxsty.dih4jda.interactions.components.StringSelectMenuHandler; | |
@@ -34,22 +35,32 @@ public class FormatCodeInteractionHandler implements ButtonHandler, StringSelect | |||
| 34 | 35 | /** | |
| 35 | 36 | * Builds the delete-all button placed on the last message of a code block. | |
| 36 | 37 | * | |
| 37 | - * @param requesterID the id of the user allowed to delete the block | ||
| 38 | - * @param total the number of messages making up the block | ||
| 38 | + * @param requesterID the id of the user allowed to delete the block | ||
| 39 | + * @param total the number of messages making up the block | ||
| 40 | + * @param firstMessageID the id of first message to check update code block | ||
| 39 | 41 | * @return the delete-all button | |
| 40 | 42 | */ | |
| 41 | - public static Button createDeleteAllButton(long requesterID, int total) { | ||
| 42 | - return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID, requesterID, total), "\uD83D\uDDD1\uFE0F"); | ||
| 43 | + public static Button createDeleteAllButton(long requesterID, int total, long firstMessageID) { | ||
| 44 | + return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID, requesterID, total,firstMessageID), "\uD83D\uDDD1\uFE0F"); | ||
| 43 | 45 | } | |
| 44 | 46 | ||
| 45 | - private static StringSelectMenu languageMenu(String customId) { | ||
| 46 | - return StringSelectMenu.create(customId) | ||
| 47 | - .setPlaceholder("Change language") | ||
| 48 | - .addOptions(Arrays.stream(Language.values()) | ||
| 49 | - .filter(language -> language != Language.UNKNOWN) | ||
| 50 | - .map(language -> SelectOption.of(language.getDisplayName(), language.name())) | ||
| 51 | - .toList()) | ||
| 52 | - .build(); | ||
| 47 | + @Override | ||
| 48 | + public void handleButton(ButtonInteractionEvent event, @NonNull Button button) { | ||
| 49 | + if (!isValid(event)) { | ||
| 50 | + return; | ||
| 51 | + } | ||
| 52 | + String[] id = ComponentIdBuilder.split(event.getComponentId()); | ||
| 53 | + | ||
| 54 | + event.deferEdit().queue(); | ||
| 55 | + | ||
| 56 | + LinkedMessages.resolveBefore(event.getMessage(), Integer.parseInt(id[2]), true, | ||
| 57 | + messages -> { | ||
| 58 | + if (messages.getLast().getIdLong() == Long.parseLong(id[3])) { | ||
| 59 | + event.getChannel().purgeMessages(messages); | ||
| 60 | + } else { | ||
| 61 | + Responses.error(event.getHook(), "The code block could not be deleted. The messages may have been deleted.").queue(); | ||
| 62 | + } | ||
| 63 | + }, () -> Responses.error(event.getHook(), "Could not delete the code block").queue()); | ||
| 53 | 64 | } | |
| 54 | 65 | ||
| 55 | 66 | /** | |
@@ -65,58 +76,51 @@ public static ActionRow buildLanguageMenu(long requesterId, int total, long firs | |||
| 65 | 76 | } | |
| 66 | 77 | ||
| 67 | 78 | @Override | |
| 68 | - public void handleButton(ButtonInteractionEvent event, @NonNull Button button) { | ||
| 69 | - String[] id = ComponentIdBuilder.split(event.getComponentId()); | ||
| 70 | - long requesterId = Long.parseLong(id[1]); | ||
| 71 | - | ||
| 72 | - Member member = event.getMember(); | ||
| 73 | - if (member == null) { | ||
| 74 | - Responses.errorWithTitle(event, "Server Required", "This button may only be used inside a server.").queue(); | ||
| 75 | - return; | ||
| 76 | - } | ||
| 77 | - if (!canManage(member, requesterId)) { | ||
| 78 | - Responses.errorWithTitle(event, "Access Denied", "You are not authorized to perform this action.").queue(); | ||
| 79 | + public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event, @NonNull List<String> values) { | ||
| 80 | + if (!isValid(event)) { | ||
| 79 | 81 | return; | |
| 80 | 82 | } | |
| 81 | 83 | ||
| 84 | + String[] id = ComponentIdBuilder.split(event.getComponentId()); | ||
| 85 | + Language language = Language.fromString(values.getFirst()); | ||
| 86 | + | ||
| 82 | 87 | event.deferEdit().queue(); | |
| 83 | 88 | ||
| 84 | - LinkedMessages.resolveBefore(event.getMessage(), Integer.parseInt(id[2]), | ||
| 85 | - messages -> event.getChannel().purgeMessages(messages), | ||
| 86 | - () -> Responses.errorWithTitle(event.getHook(), "Undefined Behavior", "Could not delete the code block").queue()); | ||
| 89 | + LinkedMessages.resolveBefore(event.getMessage(), Integer.parseInt(id[2]), false, | ||
| 90 | + messages -> { | ||
| 91 | + if (messages.getLast().getIdLong() == Long.parseLong(id[3])) { | ||
| 92 | + messages.forEach(message -> message.editMessage(withLanguage(message.getContentRaw(), language)).queue()); | ||
| 93 | + } else { | ||
| 94 | + Responses.error(event.getHook(), "The code block could not be updated. The messages may have been deleted.").queue(); | ||
| 95 | + } | ||
| 96 | + }, () -> Responses.error(event.getHook(), "Could not update the code block").queue()); | ||
| 87 | 97 | } | |
| 88 | 98 | ||
| 89 | - @Override | ||
| 90 | - public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event, @NonNull List<String> values) { | ||
| 99 | + private static StringSelectMenu languageMenu(String customId) { | ||
| 100 | + return StringSelectMenu.create(customId) | ||
| 101 | + .setPlaceholder("Change language") | ||
| 102 | + .addOptions(Arrays.stream(Language.values()) | ||
| 103 | + .filter(language -> language != Language.UNKNOWN) | ||
| 104 | + .map(language -> SelectOption.of(language.getDisplayName(), language.name())) | ||
| 105 | + .toList()) | ||
| 106 | + .build(); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + private boolean isValid(ComponentInteraction event) { | ||
| 91 | 110 | String[] id = ComponentIdBuilder.split(event.getComponentId()); | |
| 92 | 111 | long requesterId = Long.parseLong(id[1]); | |
| 93 | 112 | ||
| 94 | 113 | Member member = event.getMember(); | |
| 95 | 114 | if (member == null) { | |
| 96 | - Responses.errorWithTitle(event, "Server Required", "This menu may only be used inside a server.").queue(); | ||
| 97 | - return; | ||
| 115 | + Responses.errorWithTitle(event, "Server Required", "This button may only be used inside a server.").queue(); | ||
| 116 | + return false; | ||
| 98 | 117 | } | |
| 99 | - if (!canManage(member, requesterId)) { | ||
| 118 | + if (!(member.getIdLong() == requesterId || Checks.hasStaffRole(botConfig, member))) { | ||
| 100 | 119 | Responses.errorWithTitle(event, "Access Denied", "You are not authorized to perform this action.").queue(); | |
| 101 | - return; | ||
| 120 | + return false; | ||
| 102 | 121 | } | |
| 103 | 122 | ||
| 104 | - Language language = Language.fromString(values.getFirst()); | ||
| 105 | - event.deferEdit().queue(); | ||
| 106 | - | ||
| 107 | - LinkedMessages.resolveAfter(event.getMessage(), Integer.parseInt(id[2]), messages -> { | ||
| 108 | - if (messages.getLast().getIdLong() == Long.parseLong(id[3])) { | ||
| 109 | - messages.forEach(message -> message.editMessage(withLanguage(message.getContentRaw(), language)).queue()); | ||
| 110 | - } else { | ||
| 111 | - Responses.errorWithTitle(event.getHook(), "Merge Conflict", "The code block could not be updated. The messages may have been deleted.").queue(); | ||
| 112 | - } | ||
| 113 | - }, () -> Responses.errorWithTitle(event.getHook(), "Undefined Behavior", "Could not update the code block").queue()); | ||
| 114 | - } | ||
| 115 | - | ||
| 116 | - private boolean canManage(Member member, long requesterId) { | ||
| 117 | - return member.getIdLong() == requesterId | ||
| 118 | - || Checks.hasStaffRole(botConfig, member) | ||
| 119 | - || member.isOwner(); | ||
| 123 | + return true; | ||
| 120 | 124 | } | |
| 121 | 125 | ||
| 122 | 126 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,42 +17,32 @@ private LinkedMessages() { | |||
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | 19 | /** | |
| 20 | - * Resolves the block ending at {@code triggerMessage} (walking back {@code total} messages) and | ||
| 21 | - * passes the bot's own messages to {@code onResolved}, ordered from newest to oldest. | ||
| 22 | - * Runs {@code onError} if the block can't be safely resolved. | ||
| 20 | + * Resolves a block of messages ending at {@code triggerMessage} and passes the | ||
| 21 | + * bot's own messages to {@code onResolved}, ordered from newest to oldest. | ||
| 22 | + * If {@code inclusive} is {@code true}, {@code triggerMessage} is included in | ||
| 23 | + * the resolved block; otherwise, only the preceding {@code total} messages are | ||
| 24 | + * considered. Runs {@code onError} if the block can't be safely resolved. | ||
| 23 | 25 | * | |
| 24 | - * @param triggerMessage the last message of the block (carries the component) | ||
| 25 | - * @param total the number of messages in the block | ||
| 26 | + * @param triggerMessage the message marking the end of the block | ||
| 27 | + * @param total the number of messages to resolve | ||
| 28 | + * @param inclusive whether {@code triggerMessage} should be included in the resolved block | ||
| 26 | 29 | * @param onResolved receives the bot's messages, ordered from newest to oldest | |
| 27 | 30 | * @param onError runs if the block can't be safely resolved | |
| 28 | 31 | */ | |
| 29 | - static void resolveBefore(Message triggerMessage, int total, Consumer<List<Message>> onResolved, Runnable onError) { | ||
| 30 | - if (total <= 1) { | ||
| 32 | + static void resolveBefore(Message triggerMessage, int total, boolean inclusive,Consumer<List<Message>> onResolved, Runnable onError) { | ||
| 33 | + if (total <= 1 && inclusive) { | ||
| 31 | 34 | verify(List.of(triggerMessage), total, onResolved, onError); | |
| 32 | 35 | return; | |
| 33 | 36 | } | |
| 34 | - triggerMessage.getChannel().getHistoryBefore(triggerMessage.getIdLong(), total - 1).queue(history -> { | ||
| 37 | + triggerMessage.getChannel().getHistoryBefore(triggerMessage.getIdLong(), inclusive?total-1 :total).queue(history -> { | ||
| 35 | 38 | List<Message> block = new ArrayList<>(history.getRetrievedHistory()); | |
| 36 | - block.add(triggerMessage); | ||
| 39 | + if (inclusive){ | ||
| 40 | + block.addFirst(triggerMessage); | ||
| 41 | + } | ||
| 37 | 42 | verify(block, total, onResolved, onError); | |
| 38 | 43 | }); | |
| 39 | 44 | } | |
| 40 | 45 | ||
| 41 | - /** | ||
| 42 | - * Resolves the block of {@code total} messages sent after {@code anchorMessage} and passes the | ||
| 43 | - * bot's own messages to {@code onResolved}, ordered from newest to oldest. | ||
| 44 | - * Runs {@code onError} if the block can't be safely resolved. | ||
| 45 | - * | ||
| 46 | - * @param anchorMessage the message just before the block (carries the component) | ||
| 47 | - * @param total the number of messages in the block | ||
| 48 | - * @param onResolved receives the bot's messages, ordered from newest to oldest | ||
| 49 | - * @param onError runs if the block can't be safely resolved | ||
| 50 | - */ | ||
| 51 | - static void resolveAfter(Message anchorMessage, int total, Consumer<List<Message>> onResolved, Runnable onError) { | ||
| 52 | - anchorMessage.getChannel().getHistoryAfter(anchorMessage.getIdLong(), total).queue(history -> | ||
| 53 | - verify(history.getRetrievedHistory(), total, onResolved, onError)); | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | 46 | private static void verify(List<Message> messages, int total, Consumer<List<Message>> onResolved, Runnable onError) { | |
| 57 | 47 | List<Message> own = onlyOwn(messages); | |
| 58 | 48 | boolean allCodeBlocks = own.stream().allMatch(message -> message.getContentRaw().startsWith("```")); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments