Skip to content

Commit fd2be73

Browse files
committed
fix: more minor fixes
1 parent 6556600 commit fd2be73

3 files changed

Lines changed: 15 additions & 24 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
import org.jetbrains.annotations.NotNull;
1414

15-
import java.util.Collections;
16-
1715
/**
1816
* <h3>This class represents the /format-code command.</h3>
1917
*/
@@ -61,7 +59,6 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
6159
event.getChannel().getHistory()
6260
.retrievePast(10)
6361
.queue(messages -> {
64-
Collections.reverse(messages);
6562
Message target = messages.stream()
6663
.filter(m -> !m.getAuthor().isBot()).findFirst()
6764
.orElse(null);

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import net.dv8tion.jda.api.entities.Message;
77
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
88
import net.dv8tion.jda.api.interactions.commands.CommandInteraction;
9-
import net.dv8tion.jda.api.utils.FileUpload;
109
import org.jetbrains.annotations.Contract;
1110
import org.jetbrains.annotations.NotNull;
1211

1312
import javax.annotation.Nonnull;
14-
import java.nio.charset.StandardCharsets;
1513
import java.util.List;
1614

1715
/**
@@ -22,7 +20,7 @@
2220
class FormatCodeDispatcher {
2321

2422
/**
25-
* The maximum number of code-block messages to post inline; longer code is sent only as a file.
23+
* The maximum number of code-block messages to post inline; longer code results as an ERROR.
2624
*/
2725
private static final int MAX_MESSAGES = 5;
2826

@@ -43,18 +41,15 @@ public static void sendCode(Code code, @Nonnull CommandInteraction event, Messag
4341

4442
List<String> messages = code.toDiscordMessages();
4543

46-
// The reply both acknowledges the interaction and hands users the full,
47-
// un-split code as a downloadable file (so chunking never loses anything).
48-
FileUpload file = FileUpload.fromData(
49-
code.getContent().getBytes(StandardCharsets.UTF_8),
50-
"code." + code.getLanguage().getDiscordName()
51-
);
52-
5344
MessageChannel channel = target.getChannel();
5445

55-
event.replyFiles(file)
56-
.setAllowedMentions(List.of())
57-
.setComponents(buildActionRow(target, event.getUser().getIdLong()))
46+
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."
48+
).queue();
49+
return;
50+
}
51+
52+
Responses.success(event, "Success", "The formatted message is being sent to this channel.")
5853
.queue(success -> sendChunksInOrder(channel, messages, 0, target,event));
5954
}
6055

@@ -63,11 +58,6 @@ private static void sendChunksInOrder(MessageChannel channel, List<String> messa
6358
if (index >= messages.size()) {
6459
return;
6560
}
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-
}
7161
var action = channel.sendMessage(messages.get(index))
7262
.setAllowedMentions(List.of());
7363

@@ -101,7 +91,7 @@ private static void sendChunksInOrder(MessageChannel channel, List<String> messa
10191
* @param requesterId the id of the user permitted to delete the message
10292
* @return an action row containing the delete and "View Original" buttons
10393
*/
104-
@Contract("_ -> new")
94+
@Contract("_,_ -> new")
10595
static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId) {
10696
return ActionRow.of(InteractionUtils.createDeleteButton(requesterId),
10797
Button.link(target.getJumpUrl(), "View Original"));

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ public enum Language {
8383
*/
8484
JSON("JSON", "json"),
8585
/**
86-
* Fallback used when the language is unrecognised; renders as plain text.
86+
* Simple plain text.
8787
*/
88-
UNKNOWN("Unknown", "txt");
88+
TXT("TXT", "txt"),
89+
/**
90+
* Fallback used when the language is unrecognised.
91+
*/
92+
UNKNOWN("Unknown", "");
8993

9094
private final String displayName;
9195
private final String discordName;

0 commit comments

Comments
 (0)