Skip to content

Commit 6556600

Browse files
committed
fix: minor fixes
1 parent 6e47314 commit 6556600

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,20 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
6868
if (target != null) {
6969
sendFormattedCode(event, target, language, indentation);
7070
} else {
71-
Responses.error(event, "Could not find message; please specify a message id.").queue();
71+
Responses.errorWithTitle(event, "Message Not Found", "No recent user message could be found. Please specify a message ID.")
72+
.queue();
7273
}
7374
});
7475
} else {
7576
if (Checks.isInvalidLongInput(idOption)) {
76-
Responses.error(event, "Please provide a valid message id!").queue();
77+
Responses.errorWithTitle(event, "Invalid Message ID", "Please provide a valid Discord message ID.")
78+
.queue();
7779
return;
7880
}
7981
long messageId = idOption.getAsLong();
8082
event.getChannel().retrieveMessageById(messageId).queue(
8183
target -> sendFormattedCode(event, target, language, indentation),
82-
e -> Responses.error(event, "Could not retrieve message with id: " + messageId).queue());
84+
error -> Responses.errorWithTitle(event, "Message Not Found", "Could not retrieve the message with ID `" + messageId + "`. Make sure the message exists and is accessible.").queue());
8385
}
8486
}
8587

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*/
2222
class FormatCodeDispatcher {
2323

24+
/**
25+
* The maximum number of code-block messages to post inline; longer code is sent only as a file.
26+
*/
27+
private static final int MAX_MESSAGES = 5;
28+
2429
/**
2530
* Acknowledges the interaction by replying with the full code as a file, then posts the code as
2631
* ordered code-block messages. Replies with an error instead if there is nothing to format.
@@ -32,7 +37,7 @@ class FormatCodeDispatcher {
3237
*/
3338
public static void sendCode(Code code, @Nonnull CommandInteraction event, Message target){
3439
if (code.getContent().isBlank()) {
35-
Responses.error(event, "There is no code to format in that message.").queue();
40+
Responses.errorWithTitle(event, "404 Code not found","There is no code to format in that message.").queue();
3641
return;
3742
}
3843

@@ -58,6 +63,11 @@ private static void sendChunksInOrder(MessageChannel channel, List<String> messa
5863
if (index >= messages.size()) {
5964
return;
6065
}
66+
if (messages.size() > MAX_MESSAGES) {
67+
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."
68+
).queue();
69+
return;
70+
}
6171
var action = channel.sendMessage(messages.get(index))
6272
.setAllowedMentions(List.of());
6373

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public enum Language {
7878
* Extensible Markup Language.
7979
*/
8080
XML("XML", "xml"),
81+
/**
82+
* A structured data format.
83+
*/
84+
JSON("JSON", "json"),
8185
/**
8286
* Fallback used when the language is unrecognised; renders as plain text.
8387
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ private Responses() {
6262
return reply(event, title, String.format(message, args), Type.ERROR.getColor(), true);
6363
}
6464

65+
@CheckReturnValue
66+
public static @NotNull WebhookMessageCreateAction<Message> errorWithTitle(InteractionHook hook, String title, String message, Object... args) {
67+
return reply(hook, title, String.format(message, args), Type.ERROR.getColor(), true);
68+
}
69+
6570
@CheckReturnValue
6671
public static @NotNull WebhookMessageCreateAction<Message> error(InteractionHook hook, String message, Object... args) {
6772
return reply(hook, "An Error Occurred", String.format(message, args), Type.ERROR.getColor(), true);

0 commit comments

Comments
 (0)