|
10 | 10 | import net.discordjug.javabot.util.WebhookUtil; |
11 | 11 | import net.dv8tion.jda.api.EmbedBuilder; |
12 | 12 | import net.dv8tion.jda.api.entities.Guild; |
| 13 | +import net.dv8tion.jda.api.entities.Message; |
13 | 14 | import net.dv8tion.jda.api.entities.MessageEmbed; |
14 | 15 | import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
15 | 16 |
|
|
27 | 28 | @RequiredArgsConstructor |
28 | 29 | @Component |
29 | 30 | public class AutoCodeFormatter { |
| 31 | + private static final String CODEBLOCK_PREFIX = " ```java"; |
| 32 | + private static final String CODEBLOCK_SUFFIX = " ```"; |
30 | 33 | private final AutoMod autoMod; |
31 | 34 | private final BotConfig botConfig; |
32 | 35 | private final UserPreferenceService preferenceService; |
@@ -128,15 +131,16 @@ private void sendFormatHint(MessageReceivedEvent event) { |
128 | 131 | ).queue(); |
129 | 132 | } |
130 | 133 |
|
| 134 | + |
131 | 135 | private void replaceUnformattedCode(String msg, int codeStartIndex, int codeEndIndex, MessageReceivedEvent event) { |
132 | 136 | // default case: a "normal", non-ping containing, non first message of a forum-thread containing "{" and "}". |
133 | 137 | // 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 |
135 | 139 | sendFormatHint(event); |
136 | 140 | return; |
137 | 141 | } |
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); |
140 | 144 | EmbedBuilder autoformatInfo = new EmbedBuilder().setDescription(botConfig.get(event.getGuild()) |
141 | 145 | .getHelpConfig() |
142 | 146 | .getAutoFormatInfoMessage()); |
|
0 commit comments