Skip to content

Commit 8dece97

Browse files
committed
fixed issues with approach 2
1 parent 87a9552 commit 8dece97

3 files changed

Lines changed: 36 additions & 30 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class FormatCodeDispatcher {
3333
* @param target the original message the code came from, used for the channel and the
3434
* "View Original" / delete buttons
3535
*/
36-
public static void sendCode(Code code, @Nonnull CommandInteraction event, Message target){
36+
public static void sendCode(Code code, @Nonnull CommandInteraction event, Message target) {
3737
if (code.getContent().isBlank()) {
38-
Responses.errorWithTitle(event.getHook(), "404 Code not found","There is no code to format in that message.").queue();
38+
Responses.errorWithTitle(event.getHook(), "404 Code not found", "There is no code to format in that message.").queue();
3939
return;
4040
}
4141

@@ -44,20 +44,21 @@ public static void sendCode(Code code, @Nonnull CommandInteraction event, Messag
4444
MessageChannel channel = target.getChannel();
4545

4646
if (messages.size() > MAX_MESSAGES) {
47-
Responses.errorWithTitle(event.getHook(), "Output Too Large", "The formatted result is too large to send. Please provide a smaller code snippet or use a paste service instead."
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."
4848
).queue();
4949
return;
5050
}
5151

52-
event.getHook().sendMessage("Your message has been formatted. If needed, you can change the language used for syntax highlighting below.")
53-
.setEphemeral(true)
54-
.setComponents(FormatCodeInteractionHandler.buildLanguageMenu(event.getUser().getIdLong(),messages.size()))
55-
.queue(success -> sendChunksInOrder(channel, messages, 0, target,event));
52+
sendChunksInOrder(channel, messages, 0, target, event, 0L);
5653
}
5754

5855

59-
private static void sendChunksInOrder(MessageChannel channel, List<String> messages, int index, Message target, @Nonnull CommandInteraction event) {
56+
private static void sendChunksInOrder(MessageChannel channel, List<String> messages, int index, Message target, @Nonnull CommandInteraction event, long firstMessageID) {
6057
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();
6162
return;
6263
}
6364
var action = channel.sendMessage(messages.get(index))
@@ -67,7 +68,7 @@ private static void sendChunksInOrder(MessageChannel channel, List<String> messa
6768
action.setComponents(buildActionRow(target, event.getUser().getIdLong(), messages.size()));
6869
}
6970

70-
action.queue(sent -> sendChunksInOrder(channel, messages, index + 1, target, event));
71+
action.queue(sent -> sendChunksInOrder(channel, messages, index + 1, target, event, index == 0 ? sent.getIdLong() : firstMessageID));
7172
}
7273

7374
@Contract("_,_,_ -> new")

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class FormatCodeInteractionHandler implements ButtonHandler, StringSelect
3838
* @param total the number of messages making up the block
3939
* @return the delete-all button
4040
*/
41-
public static Button createDeleteAllButton(long requesterID, int total){
42-
return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID,requesterID,total),"\uD83D\uDDD1\uFE0F");
41+
public static Button createDeleteAllButton(long requesterID, int total) {
42+
return Button.secondary(ComponentIdBuilder.build(COMPONENT_ID, requesterID, total), "\uD83D\uDDD1\uFE0F");
4343
}
4444

4545
private static StringSelectMenu languageMenu(String customId) {
@@ -55,23 +55,23 @@ private static StringSelectMenu languageMenu(String customId) {
5555
/**
5656
* Builds the language-selection dropdown row for a code block.
5757
*
58-
* @param requesterId the id of the user allowed to change the language
59-
* @param total the number of messages making up the block
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+
* @param firstMessageID the id of first message to check update code block
6061
* @return an action row containing the language dropdown
6162
*/
62-
public static ActionRow buildLanguageMenu(long requesterId, int total) {
63-
return ActionRow.of(languageMenu(ComponentIdBuilder.build(COMPONENT_ID, requesterId, total)));
63+
public static ActionRow buildLanguageMenu(long requesterId, int total, long firstMessageID) {
64+
return ActionRow.of(languageMenu(ComponentIdBuilder.build(COMPONENT_ID, requesterId, total, firstMessageID)));
6465
}
6566

6667
@Override
6768
public void handleButton(ButtonInteractionEvent event, @NonNull Button button) {
6869
String[] id = ComponentIdBuilder.split(event.getComponentId());
6970
long requesterId = Long.parseLong(id[1]);
7071

71-
7272
Member member = event.getMember();
7373
if (member == null) {
74-
Responses.error(event, "This button may only be used inside a server.").queue();
74+
Responses.errorWithTitle(event, "Server Required", "This button may only be used inside a server.").queue();
7575
return;
7676
}
7777
if (!canManage(member, requesterId)) {
@@ -83,7 +83,7 @@ public void handleButton(ButtonInteractionEvent event, @NonNull Button button) {
8383

8484
LinkedMessages.resolveBefore(event.getMessage(), Integer.parseInt(id[2]),
8585
messages -> event.getChannel().purgeMessages(messages),
86-
() -> Responses.error(event.getHook(),"Could not delete the code block").queue());
86+
() -> Responses.errorWithTitle(event.getHook(), "Undefined Behavior", "Could not delete the code block").queue());
8787
}
8888

8989
@Override
@@ -93,7 +93,7 @@ public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event,
9393

9494
Member member = event.getMember();
9595
if (member == null) {
96-
Responses.error(event, "This menu may only be used inside a server.").queue();
96+
Responses.errorWithTitle(event, "Server Required", "This menu may only be used inside a server.").queue();
9797
return;
9898
}
9999
if (!canManage(member, requesterId)) {
@@ -104,16 +104,19 @@ public void handleStringSelectMenu(@NonNull StringSelectInteractionEvent event,
104104
Language language = Language.fromString(values.getFirst());
105105
event.deferEdit().queue();
106106

107-
LinkedMessages.resolveAfter(event.getMessage(), Integer.parseInt(id[2]),
108-
messages -> messages.forEach(message ->
109-
message.editMessage(withLanguage(message.getContentRaw(), language)).queue()),
110-
() -> Responses.error(event.getHook(),"Could not update the code block").queue());
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());
111114
}
112115

113116
private boolean canManage(Member member, long requesterId) {
114117
return member.getIdLong() == requesterId
115118
|| Checks.hasStaffRole(botConfig, member)
116-
|| member.isOwner() ;
119+
|| member.isOwner();
117120
}
118121

119122
/**

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
* the whole block (only the bot's own messages) so a single interaction can delete or edit it.
1414
*/
1515
public class LinkedMessages {
16-
private LinkedMessages(){}
16+
private LinkedMessages() {
17+
}
1718

1819
/**
1920
* Resolves the block ending at {@code triggerMessage} (walking back {@code total} messages) and
20-
* passes the bot's own messages to {@code onResolved}, or runs {@code onError} if it can't be
21-
* safely resolved.
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.
2223
*
2324
* @param triggerMessage the last message of the block (carries the component)
2425
* @param total the number of messages in the block
25-
* @param onResolved receives the bot's messages that make up the block
26+
* @param onResolved receives the bot's messages, ordered from newest to oldest
2627
* @param onError runs if the block can't be safely resolved
2728
*/
2829
static void resolveBefore(Message triggerMessage, int total, Consumer<List<Message>> onResolved, Runnable onError) {
@@ -39,11 +40,12 @@ static void resolveBefore(Message triggerMessage, int total, Consumer<List<Messa
3940

4041
/**
4142
* Resolves the block of {@code total} messages sent after {@code anchorMessage} and passes the
42-
* bot's own messages to {@code onResolved}, or runs {@code onError} if it can't be safely resolved.
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.
4345
*
4446
* @param anchorMessage the message just before the block (carries the component)
4547
* @param total the number of messages in the block
46-
* @param onResolved receives the bot's messages that make up the block
48+
* @param onResolved receives the bot's messages, ordered from newest to oldest
4749
* @param onError runs if the block can't be safely resolved
4850
*/
4951
static void resolveAfter(Message anchorMessage, int total, Consumer<List<Message>> onResolved, Runnable onError) {

0 commit comments

Comments
 (0)