|
| 1 | +package net.discordjug.javabot.systems.user_commands.format_code; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import net.discordjug.javabot.annotations.AutoDetectableComponentHandler; |
| 5 | +import net.discordjug.javabot.data.config.BotConfig; |
| 6 | +import net.discordjug.javabot.util.Checks; |
| 7 | +import net.discordjug.javabot.util.Responses; |
| 8 | +import net.dv8tion.jda.api.components.actionrow.ActionRow; |
| 9 | +import net.dv8tion.jda.api.components.buttons.Button; |
| 10 | +import net.dv8tion.jda.api.components.selections.SelectOption; |
| 11 | +import net.dv8tion.jda.api.components.selections.StringSelectMenu; |
| 12 | +import net.dv8tion.jda.api.entities.Member; |
| 13 | +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
| 14 | +import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; |
| 15 | +import org.jspecify.annotations.NonNull; |
| 16 | +import xyz.dynxsty.dih4jda.interactions.components.ButtonHandler; |
| 17 | +import xyz.dynxsty.dih4jda.interactions.components.StringSelectMenuHandler; |
| 18 | +import xyz.dynxsty.dih4jda.util.ComponentIdBuilder; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/** |
| 24 | + * Handles the interactive components on formatted code blocks: the delete-all button and the |
| 25 | + * language-selection dropdown. Both act on every message of a (possibly multi-message) block, |
| 26 | + * which is resolved via {@link LinkedMessages}. |
| 27 | + */ |
| 28 | +@AutoDetectableComponentHandler(FormatCodeInteractionHandler.COMPONENT_ID) |
| 29 | +@RequiredArgsConstructor |
| 30 | +public class FormatCodeInteractionHandler implements ButtonHandler, StringSelectMenuHandler { |
| 31 | + static final String COMPONENT_ID = "format-code-delete"; |
| 32 | + private final BotConfig botConfig; |
| 33 | + |
| 34 | + /** |
| 35 | + * Builds the delete-all button placed on the last message of a code block. |
| 36 | + * |
| 37 | + * @param requesterID the id of the user allowed to delete the block |
| 38 | + * @param total the number of messages making up the block |
| 39 | + * @return the delete-all button |
| 40 | + */ |
| 41 | + public static Button createDeleteAllButton(long requesterID, int total){ |
| 42 | + return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID,requesterID,total),"\uD83D\uDDD1️"); |
| 43 | + } |
| 44 | + |
| 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(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Builds the language-selection dropdown row for a code block. |
| 57 | + * |
| 58 | + * @param requesterId the id of the user allowed to change the language |
| 59 | + * @param total the number of messages making up the block |
| 60 | + * @return an action row containing the language dropdown |
| 61 | + */ |
| 62 | + public static ActionRow buildLanguageMenu(long requesterId, int total) { |
| 63 | + return ActionRow.of(languageMenu(ComponentIdBuilder.build(COMPONENT_ID, requesterId, total))); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void handleButton(ButtonInteractionEvent event, Button button) { |
| 68 | + String[] id = ComponentIdBuilder.split(event.getComponentId()); |
| 69 | + long requesterId = Long.parseLong(id[1]); |
| 70 | + |
| 71 | + |
| 72 | + Member member = event.getMember(); |
| 73 | + if (member == null) { |
| 74 | + Responses.error(event, "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 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + event.deferEdit().queue(); |
| 83 | + var channel = event.getChannel(); |
| 84 | + LinkedMessages.resolve(channel, event.getMessage(), Integer.parseInt(id[2]), |
| 85 | + messages -> messages.forEach(message -> message.delete().queue())); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event, @NonNull List<String> values) { |
| 90 | + String[] id = ComponentIdBuilder.split(event.getComponentId()); |
| 91 | + long requesterId = Long.parseLong(id[1]); |
| 92 | + |
| 93 | + Member member = event.getMember(); |
| 94 | + if (member == null) { |
| 95 | + Responses.error(event, "This menu may only be used inside a server.").queue(); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (!canManage(member, requesterId)) { |
| 99 | + Responses.errorWithTitle(event, "Access Denied", "You are not authorized to perform this action.").queue(); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + Language language = Language.fromString(values.getFirst()); |
| 104 | + event.deferEdit().queue(); |
| 105 | + |
| 106 | + LinkedMessages.resolveForward(event.getChannel(), event.getMessage(), Integer.parseInt(id[2]), |
| 107 | + messages -> messages.forEach(message -> |
| 108 | + message.editMessage(withLanguage(message.getContentRaw(), language)).queue())); |
| 109 | + } |
| 110 | + |
| 111 | + private boolean canManage(Member member, long requesterId) { |
| 112 | + return member.getIdLong() == requesterId |
| 113 | + || Checks.hasStaffRole(botConfig, member) |
| 114 | + || member.isOwner() ; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Re-wraps a code-block message in a different language by swapping the tag on its opening fence, |
| 119 | + * leaving the code itself untouched. |
| 120 | + * |
| 121 | + * @param content the raw message content, expected to start with a fenced code block |
| 122 | + * @param language the language to switch to |
| 123 | + * @return the message content with its opening fence set to {@code language} |
| 124 | + */ |
| 125 | + private static String withLanguage(String content, Language language) { |
| 126 | + int firstLineEnd = content.indexOf('\n'); |
| 127 | + return firstLineEnd < 0 |
| 128 | + ? content |
| 129 | + : "```" + language.getDiscordName() + content.substring(firstLineEnd); |
| 130 | + } |
| 131 | +} |
0 commit comments