Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}

codeMessageHandler.addAndHandleCodeMessage(originalMessage);
codeMessageHandler.addAndHandleCodeMessage(originalMessage, true);
}

private boolean isHelpThread(MessageReceivedEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@
import javax.annotation.Nullable;

import java.awt.Color;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Handles code in registered messages and offers code actions to the user, such as formatting their
* code.
* <p>
* Messages can be registered by using {@link #addAndHandleCodeMessage(Message)}.
* Messages can be registered by using {@link #addAndHandleCodeMessage(Message, boolean)}.
* <p>
* Code actions are automatically updated whenever the code in the original message is edited or
* deleted.
Expand Down Expand Up @@ -111,33 +107,37 @@ public void acceptComponentIdGenerator(ComponentIdGenerator generator) {
* corresponding code-reply to the author.
*
* @param originalMessage the code message to add to this handler
* @param showDeleteButton whether the code-actions should initially have a delete button or not
*/
public void addAndHandleCodeMessage(Message originalMessage) {
public void addAndHandleCodeMessage(Message originalMessage, boolean showDeleteButton) {
// Suggest code actions and remember the message <-> reply
MessageCreateData codeReply = createCodeReplyMessage(originalMessage.getIdLong());
MessageCreateData codeReply =
createCodeReplyMessage(originalMessage.getIdLong(), showDeleteButton);

originalMessage.reply(codeReply)
.onSuccess(replyMessage -> originalMessageToCodeReply.put(originalMessage.getIdLong(),
replyMessage.getIdLong()))
.queue();
}

private MessageCreateData createCodeReplyMessage(long originalMessageId) {
private MessageCreateData createCodeReplyMessage(long originalMessageId,
boolean showDeleteButton) {
List<Button> codeActionButtons = new ArrayList<>(createButtons(originalMessageId, null));
if (showDeleteButton) {
codeActionButtons.add(createDeleteButton(originalMessageId));
}

return new MessageCreateBuilder().setContent("Detected code, here are some useful tools:")
.setActionRow(createButtons(originalMessageId, null))
.setActionRow(codeActionButtons)
.build();
}

private List<Button> createButtons(long originalMessageId,
@Nullable CodeAction currentlyActiveAction) {
Stream<Button> codeActionButtons = labelToCodeAction.values().stream().map(action -> {
return labelToCodeAction.values().stream().map(action -> {
Button button = createButtonForAction(action, originalMessageId);
return action == currentlyActiveAction ? button.asDisabled() : button;
});

Stream<Button> otherButtons = Stream.of(createDeleteButton(originalMessageId));

return Stream.concat(codeActionButtons, otherButtons).toList();
}).toList();
}

private Button createDeleteButton(long originalMessageId) {
Expand Down Expand Up @@ -192,6 +192,9 @@ public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
}

private void deleteCodeReply(ButtonInteractionEvent event, long originalMessageId) {
logger.debug("User {} deleted the code-reply from original message {} in channel {}",
event.getUser().getId(), originalMessageId, event.getChannel().getName());

originalMessageToCodeReply.invalidate(originalMessageId);
event.getMessage().delete().queue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public void onMessageContext(MessageContextInteractionEvent event) {
.setEphemeral(true)
.queue();

codeMessageHandler.addAndHandleCodeMessage(event.getTarget());
codeMessageHandler.addAndHandleCodeMessage(event.getTarget(), false);
}
}