Skip to content

Commit 164957b

Browse files
authored
Merge pull request #473 from danthe1st/java-autoformat
use java codeblock when autoformatting code in help channels
2 parents 8c2a58f + ed1c7bb commit 164957b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/main/java/net/discordjug/javabot/systems/help/AutoCodeFormatter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.discordjug.javabot.util.WebhookUtil;
1111
import net.dv8tion.jda.api.EmbedBuilder;
1212
import net.dv8tion.jda.api.entities.Guild;
13+
import net.dv8tion.jda.api.entities.Message;
1314
import net.dv8tion.jda.api.entities.MessageEmbed;
1415
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
1516

@@ -27,6 +28,8 @@
2728
@RequiredArgsConstructor
2829
@Component
2930
public class AutoCodeFormatter {
31+
private static final String CODEBLOCK_PREFIX = " ```java";
32+
private static final String CODEBLOCK_SUFFIX = " ```";
3033
private final AutoMod autoMod;
3134
private final BotConfig botConfig;
3235
private final UserPreferenceService preferenceService;
@@ -128,15 +131,16 @@ private void sendFormatHint(MessageReceivedEvent event) {
128131
).queue();
129132
}
130133

134+
131135
private void replaceUnformattedCode(String msg, int codeStartIndex, int codeEndIndex, MessageReceivedEvent event) {
132136
// default case: a "normal", non-ping containing, non first message of a forum-thread containing "{" and "}".
133137
// user must also have set their preferences to allow this.
134-
if (msg.length() > 1992) { // can't exceed discord's char limit
138+
if (msg.length() > Message.MAX_CONTENT_LENGTH - CODEBLOCK_PREFIX.length() - CODEBLOCK_SUFFIX.length()) { // can't exceed discord's char limit
135139
sendFormatHint(event);
136140
return;
137141
}
138-
String messageContent = msg.substring(0, codeStartIndex) + " ```" +
139-
msg.substring(codeStartIndex, codeEndIndex) + " ```" + msg.substring(codeEndIndex);
142+
String messageContent = msg.substring(0, codeStartIndex) + CODEBLOCK_PREFIX +
143+
msg.substring(codeStartIndex, codeEndIndex) + CODEBLOCK_SUFFIX + msg.substring(codeEndIndex);
140144
EmbedBuilder autoformatInfo = new EmbedBuilder().setDescription(botConfig.get(event.getGuild())
141145
.getHelpConfig()
142146
.getAutoFormatInfoMessage());

0 commit comments

Comments
 (0)