-
Notifications
You must be signed in to change notification settings - Fork 25
Fix formatting of code longer than Discord's message limit #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danthe1st
merged 12 commits into
Java-Discord:main
from
Neil-Tomar:fix/format-code-long-messages
Jun 25, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7679c4b
fix formatting of code longer than Discord's message limit
Neil-Tomar d2f25e6
add javadocs, and fixed requested changes.
Neil-Tomar 1cdeef7
added javadocs, and fixed requested changes, fixed build issue.
Neil-Tomar 50e0594
Merge remote-tracking branch 'origin/fix/format-code-long-messages' i…
Neil-Tomar f5c3037
refactor: address format-code review feedback
Neil-Tomar 6e47314
fix: normalize line endings in IndentationHelperTest
Neil-Tomar 6556600
fix: minor fixes
Neil-Tomar fd2be73
fix: more minor fixes
Neil-Tomar f1a6a9f
fix: typo fix.
Neil-Tomar ba03c61
fix: typo fix.
Neil-Tomar 3467109
Merge remote-tracking branch 'origin/fix/format-code-long-messages' i…
Neil-Tomar c6e158e
acknowledge interaction immediately for format-code, remove unused code
danthe1st File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/net/discordjug/javabot/systems/user_commands/format_code/Code.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package net.discordjug.javabot.systems.user_commands.format_code; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Holds a piece of code and its {@link Language}, and turns it into | ||
| * Discord-friendly representations that respect Discord's 2000-character limit. | ||
| */ | ||
| public class Code { | ||
|
|
||
| /** | ||
| * Maximum characters per chunk. Discord's hard limit per message is 2000; | ||
| * the remaining headroom covers the surrounding ```language fences. | ||
| */ | ||
| private static final int MAX_SIZE = 1980; | ||
|
|
||
| private final Language language; | ||
| private final String content; | ||
|
|
||
| /** | ||
| * Creates a code block for the given language and content. | ||
| * | ||
| * @param language the language the code is written in, used for syntax highlighting | ||
| * @param content the raw, already-sanitized code to format | ||
| */ | ||
| public Code(Language language, String content) { | ||
| this.language = language; | ||
| this.content = content; | ||
| } | ||
|
|
||
| public String getContent() { | ||
| return content; | ||
| } | ||
|
|
||
| /** | ||
| * Splits {@link #content} into pieces that each fit within {@link #MAX_SIZE}, | ||
| * breaking on newlines where possible so lines are not cut in half. | ||
| * | ||
| * @return the content split into chunks that each fit within the limit | ||
| */ | ||
| private List<String> toDiscordChunks() { | ||
| List<String> chunks = new ArrayList<>(); | ||
| String remaining = content; | ||
|
|
||
| while (remaining.length() > MAX_SIZE) { | ||
| int split = remaining.lastIndexOf('\n', MAX_SIZE); | ||
| if (split <= 0) { | ||
| // No newline in range (or only at the very start) -> hard cut, | ||
| // guaranteeing progress so this can never infinite-loop. | ||
| chunks.add(remaining.substring(0, MAX_SIZE)); | ||
| remaining = remaining.substring(MAX_SIZE); | ||
| } else { | ||
| chunks.add(remaining.substring(0, split)); | ||
| remaining = remaining.substring(split + 1); // +1 consumes the '\n' | ||
| } | ||
| } | ||
| chunks.add(remaining); | ||
| return chunks; | ||
| } | ||
|
|
||
| /** | ||
| * Splits the content into chunks that each fit within Discord's character limit and wraps | ||
| * every chunk in a language-tagged code block. | ||
| * | ||
| * @return the formatted code-block messages, one per Discord message | ||
| */ | ||
| public List<String> toDiscordMessages() { | ||
| return toDiscordChunks() | ||
| .stream() | ||
| .map(chunk -> String.format("```%s\n%s\n```", language.getDiscordName(), chunk)) | ||
| .toList(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...n/java/net/discordjug/javabot/systems/user_commands/format_code/FormatCodeDispatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package net.discordjug.javabot.systems.user_commands.format_code; | ||
|
|
||
| import net.discordjug.javabot.util.*; | ||
| import net.dv8tion.jda.api.components.actionrow.ActionRow; | ||
| import net.dv8tion.jda.api.components.buttons.Button; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | ||
| import net.dv8tion.jda.api.interactions.commands.CommandInteraction; | ||
| import org.jetbrains.annotations.Contract; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Shared sending logic for the code-formatting commands. Replies with the full code as a | ||
| * downloadable file, then posts it as one or more ordered code-block messages that each respect | ||
| * Discord's 2000-character limit. | ||
| */ | ||
| class FormatCodeDispatcher { | ||
|
|
||
| /** | ||
| * The maximum number of code-block messages to post inline; longer code results in an error. | ||
| */ | ||
| private static final int MAX_MESSAGES = 5; | ||
|
|
||
| /** | ||
| * Acknowledges the interaction by replying with the full code as a file, then posts the code as | ||
| * ordered code-block messages. Replies with an error instead if there is nothing to format. | ||
| * | ||
| * @param code the code to send | ||
| * @param event the interaction to reply to | ||
| * @param target the original message the code came from, used for the channel and the | ||
| * "View Original" / delete buttons | ||
| */ | ||
| public static void sendCode(Code code, @Nonnull CommandInteraction event, Message target){ | ||
| if (code.getContent().isBlank()) { | ||
| Responses.errorWithTitle(event.getHook(), "404 Code not found","There is no code to format in that message.").queue(); | ||
| return; | ||
| } | ||
|
|
||
| List<String> messages = code.toDiscordMessages(); | ||
|
danthe1st marked this conversation as resolved.
|
||
|
|
||
| MessageChannel channel = target.getChannel(); | ||
|
|
||
| if (messages.size() > MAX_MESSAGES) { | ||
| 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." | ||
| ).queue(); | ||
| return; | ||
| } | ||
|
|
||
| Responses.success(event.getHook(), "Success", "The formatted message is being sent to this channel.") | ||
| .queue(success -> sendChunksInOrder(channel, messages, 0, target,event)); | ||
| } | ||
|
|
||
|
|
||
| private static void sendChunksInOrder(MessageChannel channel, List<String> messages, int index, Message target, @Nonnull CommandInteraction event) { | ||
| if (index >= messages.size()) { | ||
| return; | ||
| } | ||
| var action = channel.sendMessage(messages.get(index)) | ||
|
danthe1st marked this conversation as resolved.
|
||
| .setAllowedMentions(List.of()); | ||
|
|
||
| if (index == messages.size() - 1) { | ||
| if(index == 0){ | ||
| action.setComponents(buildActionRow(target, event.getUser().getIdLong())); | ||
| } else { | ||
| action.setComponents(buildActionRow(target)); | ||
| } | ||
| } | ||
|
|
||
| action.queue(success -> | ||
| sendChunksInOrder(channel, messages, index + 1, target, event)); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the action row placed on the last code-block message. | ||
| * | ||
| * @param target the original message linked by the "View Original" button | ||
| * @return an action row containing the "View Original" link button | ||
| */ | ||
| @Contract("_ -> new") | ||
| static @NotNull ActionRow buildActionRow(@NotNull Message target) { | ||
| return ActionRow.of(Button.link(target.getJumpUrl(), "View Original")); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the action row placed on the file-upload message: a delete button and a "View Original" link. | ||
| * | ||
| * @param target the original message linked by the "View Original" button | ||
| * @param requesterId the id of the user permitted to delete the message | ||
| * @return an action row containing the delete and "View Original" buttons | ||
| */ | ||
| @Contract("_,_ -> new") | ||
| static @NotNull ActionRow buildActionRow(@NotNull Message target, long requesterId) { | ||
| return ActionRow.of(InteractionUtils.createDeleteButton(requesterId), | ||
| Button.link(target.getJumpUrl(), "View Original")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.