-
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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
| 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 Language language; | ||
| private final String content; | ||
|
|
||
| public Code(Language language, String content) { | ||
| this.language = language; | ||
| this.content = content; | ||
| } | ||
|
|
||
| public String getContent() { | ||
|
danthe1st marked this conversation as resolved.
|
||
| return content; | ||
| } | ||
|
|
||
| public Language getLanguage() { | ||
| return language; | ||
| } | ||
|
|
||
| public void setLanguage(Language language) { | ||
| this.language = language; | ||
| } | ||
|
|
||
| /** | ||
| * Splits {@link #content} into pieces that each fit within {@link #MAX_SIZE}, | ||
| * breaking on newlines where possible so lines are not cut in half. | ||
| */ | ||
| public List<String> toDiscordChunks() { | ||
|
danthe1st marked this conversation as resolved.
Outdated
|
||
| 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; | ||
| } | ||
|
|
||
| /** Wraps each chunk in a language-tagged Discord code block. */ | ||
| 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
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
49 changes: 49 additions & 0 deletions
49
src/main/java/net/discordjug/javabot/systems/user_commands/format_code/Language.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,49 @@ | ||
| package net.discordjug.javabot.systems.user_commands.format_code; | ||
|
|
||
| public enum Language { | ||
|
danthe1st marked this conversation as resolved.
|
||
| C("c"), | ||
| CPP("cpp"), | ||
| CSHARP("csharp"), | ||
| CSS("css"), | ||
| D("d"), | ||
| GO("go"), | ||
| HTML("html"), | ||
| JAVA("java"), | ||
| JAVASCRIPT("js"), | ||
| KOTLIN("kotlin"), | ||
| PHP("php"), | ||
| PYTHON("python"), | ||
| RUBY("ruby"), | ||
| RUST("rust"), | ||
| SQL("sql"), | ||
| SWIFT("swift"), | ||
| TYPESCRIPT("typescript"), | ||
| XML("xml"), | ||
| UNKNOWN("txt"); | ||
|
|
||
| private final String discordName; | ||
|
|
||
| Language(String discordName) { | ||
| this.discordName = discordName; | ||
| } | ||
|
|
||
| public String getDiscordName() { | ||
| return discordName; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a language from a string (e.g. the value of the /format-code "format" | ||
| * option) by matching its Discord code-fence name, falling back to {@link #UNKNOWN}. | ||
| * | ||
| * @param name the code-fence name to look up (case-insensitive) | ||
| * @return the matching language, or {@link #UNKNOWN} if none matches | ||
| */ | ||
| public static Language fromString(String name) { | ||
| for (Language language : values()) { | ||
|
danthe1st marked this conversation as resolved.
Outdated
|
||
| if (language.discordName.equalsIgnoreCase(name)) { | ||
| return language; | ||
| } | ||
| } | ||
| return UNKNOWN; | ||
| } | ||
| } | ||
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.