Skip to content

Commit 50e3fdf

Browse files
committed
refactor: address review feedback
1 parent c42529c commit 50e3fdf

3 files changed

Lines changed: 38 additions & 27 deletions

File tree

src/main/java/net/discordjug/javabot/systems/user_commands/format_code/FormatCodeInteractionHandler.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@AutoDetectableComponentHandler(FormatCodeInteractionHandler.COMPONENT_ID)
2929
@RequiredArgsConstructor
3030
public class FormatCodeInteractionHandler implements ButtonHandler, StringSelectMenuHandler {
31-
static final String COMPONENT_ID = "format-code-delete";
31+
static final String COMPONENT_ID = "format-code";
3232
private final BotConfig botConfig;
3333

3434
/**
@@ -39,7 +39,7 @@ public class FormatCodeInteractionHandler implements ButtonHandler, StringSelect
3939
* @return the delete-all button
4040
*/
4141
public static Button createDeleteAllButton(long requesterID, int total){
42-
return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID,requesterID,total),"\uD83D\uDDD1");
42+
return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID,requesterID,total),"\uD83D\uDDD1\uFE0F");
4343
}
4444

4545
private static StringSelectMenu languageMenu(String customId) {
@@ -64,7 +64,7 @@ public static ActionRow buildLanguageMenu(long requesterId, int total) {
6464
}
6565

6666
@Override
67-
public void handleButton(ButtonInteractionEvent event, Button button) {
67+
public void handleButton(ButtonInteractionEvent event, @NonNull Button button) {
6868
String[] id = ComponentIdBuilder.split(event.getComponentId());
6969
long requesterId = Long.parseLong(id[1]);
7070

@@ -80,13 +80,14 @@ public void handleButton(ButtonInteractionEvent event, Button button) {
8080
}
8181

8282
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()));
83+
84+
LinkedMessages.resolveBefore(event.getMessage(), Integer.parseInt(id[2]),
85+
messages -> event.getChannel().purgeMessages(messages),
86+
() -> Responses.error(event.getHook(),"Could not delete the code block"));
8687
}
8788

8889
@Override
89-
public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event, @NonNull List<String> values) {
90+
public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event, @NonNull List<String> values) {
9091
String[] id = ComponentIdBuilder.split(event.getComponentId());
9192
long requesterId = Long.parseLong(id[1]);
9293

@@ -103,9 +104,10 @@ public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event,
103104
Language language = Language.fromString(values.getFirst());
104105
event.deferEdit().queue();
105106

106-
LinkedMessages.resolveForward(event.getChannel(), event.getMessage(), Integer.parseInt(id[2]),
107+
LinkedMessages.resolveAfter(event.getMessage(), Integer.parseInt(id[2]),
107108
messages -> messages.forEach(message ->
108-
message.editMessage(withLanguage(message.getContentRaw(), language)).queue()));
109+
message.editMessage(withLanguage(message.getContentRaw(), language)).queue()),
110+
() -> Responses.error(event.getHook(),"Could not update the code block"));
109111
}
110112

111113
private boolean canManage(Member member, long requesterId) {
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.discordjug.javabot.systems.user_commands.format_code;
22

33
import net.dv8tion.jda.api.entities.Message;
4-
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
54

65
import java.util.ArrayList;
76
import java.util.List;
@@ -18,43 +17,53 @@ private LinkedMessages(){}
1817

1918
/**
2019
* Resolves the block ending at {@code triggerMessage} (walking back {@code total} messages) and
21-
* passes the bot's own messages among them to {@code onResolved}.
20+
* passes the bot's own messages to {@code onResolved}, or runs {@code onError} if it can't be
21+
* safely resolved.
2222
*
23-
* @param channel the channel the block is in
2423
* @param triggerMessage the last message of the block (carries the component)
2524
* @param total the number of messages in the block
26-
* @param onResolved receives the bot's messages that make up the block
25+
* @param onResolved receives the bot's messages that make up the block
26+
* @param onError runs if the block can't be safely resolved
2727
*/
28-
static void resolve(MessageChannel channel, Message triggerMessage, int total, Consumer<List<Message>> onResolved) {
28+
static void resolveBefore(Message triggerMessage, int total, Consumer<List<Message>> onResolved, Runnable onError) {
2929
if (total <= 1) {
30-
onResolved.accept(List.of(triggerMessage));
30+
verify(List.of(triggerMessage), total, onResolved, onError);
3131
return;
3232
}
33-
channel.getHistoryBefore(triggerMessage.getIdLong(), total - 1).queue(history -> {
33+
triggerMessage.getChannel().getHistoryBefore(triggerMessage.getIdLong(), total - 1).queue(history -> {
3434
List<Message> block = new ArrayList<>(history.getRetrievedHistory());
3535
block.add(triggerMessage);
36-
onResolved.accept(onlyOwn(channel, block));
36+
verify(block, total, onResolved, onError);
3737
});
3838
}
3939

4040
/**
4141
* Resolves the block of {@code total} messages sent after {@code anchorMessage} and passes the
42-
* bot's own messages among them to {@code onResolved}.
42+
* bot's own messages to {@code onResolved}, or runs {@code onError} if it can't be safely resolved.
4343
*
44-
* @param channel the channel the block is in
4544
* @param anchorMessage the message just before the block (carries the component)
4645
* @param total the number of messages in the block
47-
* @param onResolved receives the bot's messages that make up the block
46+
* @param onResolved receives the bot's messages that make up the block
47+
* @param onError runs if the block can't be safely resolved
4848
*/
49-
static void resolveForward(MessageChannel channel, Message anchorMessage, int total, Consumer<List<Message>> onResolved) {
50-
channel.getHistoryAfter(anchorMessage.getIdLong(), total).queue(history ->
51-
onResolved.accept(onlyOwn(channel, history.getRetrievedHistory())));
49+
static void resolveAfter(Message anchorMessage, int total, Consumer<List<Message>> onResolved, Runnable onError) {
50+
anchorMessage.getChannel().getHistoryAfter(anchorMessage.getIdLong(), total).queue(history ->
51+
verify(history.getRetrievedHistory(), total, onResolved, onError));
52+
}
53+
54+
private static void verify(List<Message> messages, int total, Consumer<List<Message>> onResolved, Runnable onError) {
55+
List<Message> own = onlyOwn(messages);
56+
boolean allCodeBlocks = own.stream().allMatch(message -> message.getContentRaw().startsWith("```"));
57+
if (own.size() != total || !allCodeBlocks) {
58+
onError.run();
59+
return;
60+
}
61+
onResolved.accept(own);
5262
}
5363

54-
private static List<Message> onlyOwn(MessageChannel channel, List<Message> messages) {
55-
long selfId = channel.getJDA().getSelfUser().getIdLong();
64+
private static List<Message> onlyOwn(List<Message> messages) {
5665
return messages.stream()
57-
.filter(message -> message.getAuthor().getIdLong() == selfId)
66+
.filter(message -> message.getAuthor().getIdLong() == message.getJDA().getSelfUser().getIdLong())
5867
.toList();
5968
}
6069
}

src/main/java/net/discordjug/javabot/util/InteractionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private boolean canDeleteUsingButton(Member member, String[] componentId) {
116116
}
117117

118118
public static Button createDeleteButton(long senderId) {
119-
return Button.secondary(createDeleteInteractionId(senderId), "\uD83D\uDDD1");
119+
return Button.secondary(createDeleteInteractionId(senderId), "\uD83D\uDDD1\uFE0F");
120120
}
121121

122122
public static String createDeleteInteractionId(long senderId) {

0 commit comments

Comments
 (0)