Skip to content

Commit f5c3037

Browse files
committed
refactor: address format-code review feedback
1 parent 50e0594 commit f5c3037

3 files changed

Lines changed: 28 additions & 27 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Code {
1515
*/
1616
private static final int MAX_SIZE = 1980;
1717

18-
private Language language;
18+
private final Language language;
1919
private final String content;
2020

2121
/**

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
44
import net.discordjug.javabot.util.*;
5-
import net.dv8tion.jda.api.components.actionrow.ActionRow;
6-
import net.dv8tion.jda.api.components.buttons.Button;
75
import net.dv8tion.jda.api.entities.Message;
86
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
97
import net.dv8tion.jda.api.interactions.InteractionContextType;
108
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
119
import net.dv8tion.jda.api.interactions.commands.OptionType;
1210
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1311
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
14-
import org.jetbrains.annotations.Contract;
12+
1513
import org.jetbrains.annotations.NotNull;
1614

1715
import java.util.Collections;
@@ -53,19 +51,6 @@ private static OptionData formatOption() {
5351
return option;
5452
}
5553

56-
/**
57-
* Builds the action row placed on the file-upload message: a delete button and a "View Original" link.
58-
*
59-
* @param target the original message linked by the "View Original" button
60-
* @param requesterId the id of the user permitted to delete the message
61-
* @return an action row containing the delete and "View Original" buttons
62-
*/
63-
@Contract("_ -> new")
64-
static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId) {
65-
return ActionRow.of(InteractionUtils.createDeleteButton(requesterId),
66-
Button.link(target.getJumpUrl(), "View Original"));
67-
}
68-
6954
@Override
7055
public void execute(@NotNull SlashCommandInteractionEvent event) {
7156
OptionMapping idOption = event.getOption("message-id");
@@ -83,18 +68,18 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
8368
if (target != null) {
8469
sendFormattedCode(event, target, language, indentation);
8570
} else {
86-
Responses.error(event.getHook(), "Could not find message; please specify a message id.").queue();
71+
Responses.error(event, "Could not find message; please specify a message id.").queue();
8772
}
8873
});
8974
} else {
9075
if (Checks.isInvalidLongInput(idOption)) {
91-
Responses.error(event.getHook(), "Please provide a valid message id!").queue();
76+
Responses.error(event, "Please provide a valid message id!").queue();
9277
return;
9378
}
9479
long messageId = idOption.getAsLong();
9580
event.getChannel().retrieveMessageById(messageId).queue(
9681
target -> sendFormattedCode(event, target, language, indentation),
97-
e -> Responses.error(event.getHook(), "Could not retrieve message with id: " + messageId).queue());
82+
e -> Responses.error(event, "Could not retrieve message with id: " + messageId).queue());
9883
}
9984
}
10085

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* downloadable file, then posts it as one or more ordered code-block messages that each respect
2020
* Discord's 2000-character limit.
2121
*/
22-
public class FormatCodeDispatcher {
22+
class FormatCodeDispatcher {
2323

2424
/**
2525
* Acknowledges the interaction by replying with the full code as a file, then posts the code as
@@ -32,10 +32,10 @@ public class FormatCodeDispatcher {
3232
*/
3333
public static void sendCode(Code code, @Nonnull CommandInteraction event, Message target){
3434
if (code.getContent().isBlank()) {
35-
Responses.error(event.getHook(), "There is no code to format in that message.").queue();
35+
Responses.error(event, "There is no code to format in that message.").queue();
3636
return;
3737
}
38-
// Currently we always format as Java. A language dropdown will be added in the future.
38+
3939
List<String> messages = code.toDiscordMessages();
4040

4141
// The reply both acknowledges the interaction and hands users the full,
@@ -49,7 +49,7 @@ public static void sendCode(Code code, @Nonnull CommandInteraction event, Messag
4949

5050
event.replyFiles(file)
5151
.setAllowedMentions(List.of())
52-
.setComponents(FormatCodeCommand.buildActionRow(target, event.getUser().getIdLong()))
52+
.setComponents(buildActionRow(target, event.getUser().getIdLong()))
5353
.queue(success -> sendChunksInOrder(channel, messages, 0, target,event));
5454
}
5555

@@ -62,7 +62,11 @@ private static void sendChunksInOrder(MessageChannel channel, List<String> messa
6262
.setAllowedMentions(List.of());
6363

6464
if (index == messages.size() - 1) {
65-
action.setComponents(buildActionRow(target, event.getUser().getIdLong()));
65+
if(index == 0){
66+
action.setComponents(buildActionRow(target, event.getUser().getIdLong()));
67+
} else {
68+
action.setComponents(buildActionRow(target));
69+
}
6670
}
6771

6872
action.queue(success ->
@@ -73,11 +77,23 @@ private static void sendChunksInOrder(MessageChannel channel, List<String> messa
7377
* Builds the action row placed on the last code-block message.
7478
*
7579
* @param target the original message linked by the "View Original" button
76-
* @param requesterId the id of the requesting user
7780
* @return an action row containing the "View Original" link button
7881
*/
7982
@Contract("_ -> new")
80-
static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId) {
83+
static @NotNull ActionRow buildActionRow(@NotNull Message target) {
8184
return ActionRow.of(Button.link(target.getJumpUrl(), "View Original"));
8285
}
86+
87+
/**
88+
* Builds the action row placed on the file-upload message: a delete button and a "View Original" link.
89+
*
90+
* @param target the original message linked by the "View Original" button
91+
* @param requesterId the id of the user permitted to delete the message
92+
* @return an action row containing the delete and "View Original" buttons
93+
*/
94+
@Contract("_ -> new")
95+
static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId) {
96+
return ActionRow.of(InteractionUtils.createDeleteButton(requesterId),
97+
Button.link(target.getJumpUrl(), "View Original"));
98+
}
8399
}

0 commit comments

Comments
 (0)