-
Notifications
You must be signed in to change notification settings - Fork 25
feat: delete-all button and language dropdown for multi-message code blocks #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package net.discordjug.javabot.systems.user_commands.format_code; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import net.discordjug.javabot.annotations.AutoDetectableComponentHandler; | ||
| import net.discordjug.javabot.data.config.BotConfig; | ||
| import net.discordjug.javabot.util.Checks; | ||
| import net.discordjug.javabot.util.Responses; | ||
| import net.dv8tion.jda.api.components.actionrow.ActionRow; | ||
| import net.dv8tion.jda.api.components.buttons.Button; | ||
| import net.dv8tion.jda.api.components.selections.SelectOption; | ||
| import net.dv8tion.jda.api.components.selections.StringSelectMenu; | ||
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; | ||
| import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; | ||
| import org.jspecify.annotations.NonNull; | ||
| import xyz.dynxsty.dih4jda.interactions.components.ButtonHandler; | ||
| import xyz.dynxsty.dih4jda.interactions.components.StringSelectMenuHandler; | ||
| import xyz.dynxsty.dih4jda.util.ComponentIdBuilder; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Handles the interactive components on formatted code blocks: the delete-all button and the | ||
| * language-selection dropdown. Both act on every message of a (possibly multi-message) block, | ||
| * which is resolved via {@link LinkedMessages}. | ||
| */ | ||
| @AutoDetectableComponentHandler(FormatCodeInteractionHandler.COMPONENT_ID) | ||
| @RequiredArgsConstructor | ||
| public class FormatCodeInteractionHandler implements ButtonHandler, StringSelectMenuHandler { | ||
| static final String COMPONENT_ID = "format-code-delete"; | ||
| private final BotConfig botConfig; | ||
|
|
||
| /** | ||
| * Builds the delete-all button placed on the last message of a code block. | ||
| * | ||
| * @param requesterID the id of the user allowed to delete the block | ||
| * @param total the number of messages making up the block | ||
| * @return the delete-all button | ||
| */ | ||
| public static Button createDeleteAllButton(long requesterID, int total){ | ||
| return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID,requesterID,total),"\uD83D\uDDD1️"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure why such a special |
||
| } | ||
|
|
||
| private static StringSelectMenu languageMenu(String customId) { | ||
| return StringSelectMenu.create(customId) | ||
| .setPlaceholder("Change language") | ||
| .addOptions(Arrays.stream(Language.values()) | ||
| .filter(language -> language != Language.UNKNOWN) | ||
| .map(language -> SelectOption.of(language.getDisplayName(), language.name())) | ||
| .toList()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the language-selection dropdown row for a code block. | ||
| * | ||
| * @param requesterId the id of the user allowed to change the language | ||
| * @param total the number of messages making up the block | ||
| * @return an action row containing the language dropdown | ||
| */ | ||
| public static ActionRow buildLanguageMenu(long requesterId, int total) { | ||
| return ActionRow.of(languageMenu(ComponentIdBuilder.build(COMPONENT_ID, requesterId, total))); | ||
| } | ||
|
|
||
| @Override | ||
| public void handleButton(ButtonInteractionEvent event, Button button) { | ||
| String[] id = ComponentIdBuilder.split(event.getComponentId()); | ||
| long requesterId = Long.parseLong(id[1]); | ||
|
|
||
|
|
||
| Member member = event.getMember(); | ||
| if (member == null) { | ||
| Responses.error(event, "This button may only be used inside a server.").queue(); | ||
| return; | ||
| } | ||
| if (!canManage(member, requesterId)) { | ||
| Responses.errorWithTitle(event, "Access Denied", "You are not authorized to perform this action.").queue(); | ||
| return; | ||
| } | ||
|
|
||
| event.deferEdit().queue(); | ||
| var channel = event.getChannel(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please use the proper type here? I assume it's a |
||
| LinkedMessages.resolve(channel, event.getMessage(), Integer.parseInt(id[2]), | ||
| messages -> messages.forEach(message -> message.delete().queue())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please use the |
||
| } | ||
|
|
||
| @Override | ||
| public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event, @NonNull List<String> values) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation is messed up here. |
||
| String[] id = ComponentIdBuilder.split(event.getComponentId()); | ||
| long requesterId = Long.parseLong(id[1]); | ||
|
|
||
| Member member = event.getMember(); | ||
| if (member == null) { | ||
| Responses.error(event, "This menu may only be used inside a server.").queue(); | ||
| return; | ||
| } | ||
| if (!canManage(member, requesterId)) { | ||
| Responses.errorWithTitle(event, "Access Denied", "You are not authorized to perform this action.").queue(); | ||
| return; | ||
| } | ||
|
|
||
| Language language = Language.fromString(values.getFirst()); | ||
| event.deferEdit().queue(); | ||
|
|
||
| LinkedMessages.resolveForward(event.getChannel(), event.getMessage(), Integer.parseInt(id[2]), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the channel argument really needed here? Can't you derive it from the message? |
||
| messages -> messages.forEach(message -> | ||
| message.editMessage(withLanguage(message.getContentRaw(), language)).queue())); | ||
| } | ||
|
|
||
| private boolean canManage(Member member, long requesterId) { | ||
| return member.getIdLong() == requesterId | ||
| || Checks.hasStaffRole(botConfig, member) | ||
| || member.isOwner() ; | ||
| } | ||
|
|
||
| /** | ||
| * Re-wraps a code-block message in a different language by swapping the tag on its opening fence, | ||
| * leaving the code itself untouched. | ||
| * | ||
| * @param content the raw message content, expected to start with a fenced code block | ||
| * @param language the language to switch to | ||
| * @return the message content with its opening fence set to {@code language} | ||
| */ | ||
| private static String withLanguage(String content, Language language) { | ||
| int firstLineEnd = content.indexOf('\n'); | ||
| return firstLineEnd < 0 | ||
| ? content | ||
| : "```" + language.getDiscordName() + content.substring(firstLineEnd); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package net.discordjug.javabot.systems.user_commands.format_code; | ||
|
|
||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Consumer; | ||
|
|
||
|
|
||
| /** | ||
| * Helper for acting on a block of related messages sent as a group — such as a piece of code split | ||
| * across several Discord messages. Given one message of the block and the total count, it resolves | ||
| * the whole block (only the bot's own messages) so a single interaction can delete or edit it. | ||
| */ | ||
| public class LinkedMessages { | ||
| private LinkedMessages(){} | ||
|
|
||
| /** | ||
| * Resolves the block ending at {@code triggerMessage} (walking back {@code total} messages) and | ||
| * passes the bot's own messages among them to {@code onResolved}. | ||
| * | ||
| * @param channel the channel the block is in | ||
| * @param triggerMessage the last message of the block (carries the component) | ||
| * @param total the number of messages in the block | ||
| * @param onResolved receives the bot's messages that make up the block | ||
| */ | ||
| static void resolve(MessageChannel channel, Message triggerMessage, int total, Consumer<List<Message>> onResolved) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you don't need the |
||
| if (total <= 1) { | ||
| onResolved.accept(List.of(triggerMessage)); | ||
| return; | ||
| } | ||
| channel.getHistoryBefore(triggerMessage.getIdLong(), total - 1).queue(history -> { | ||
| List<Message> block = new ArrayList<>(history.getRetrievedHistory()); | ||
| block.add(triggerMessage); | ||
| onResolved.accept(onlyOwn(channel, block)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fail (i.e. return an ephemeral error message) without deleting any messages in the following situations:
You could do that by just adding an "error" |
||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the block of {@code total} messages sent after {@code anchorMessage} and passes the | ||
| * bot's own messages among them to {@code onResolved}. | ||
| * | ||
| * @param channel the channel the block is in | ||
| * @param anchorMessage the message just before the block (carries the component) | ||
| * @param total the number of messages in the block | ||
| * @param onResolved receives the bot's messages that make up the block | ||
| */ | ||
| static void resolveForward(MessageChannel channel, Message anchorMessage, int total, Consumer<List<Message>> onResolved) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you don't need the |
||
| channel.getHistoryAfter(anchorMessage.getIdLong(), total).queue(history -> | ||
| onResolved.accept(onlyOwn(channel, history.getRetrievedHistory()))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also perform the same message check/error handling approach here as well. |
||
| } | ||
|
|
||
| private static List<Message> onlyOwn(MessageChannel channel, List<Message> messages) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the |
||
| long selfId = channel.getJDA().getSelfUser().getIdLong(); | ||
| return messages.stream() | ||
| .filter(message -> message.getAuthor().getIdLong() == selfId) | ||
| .toList(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The select menu isn't about deleting hence the id
format-code-deleteisn't appropriate.