Skip to content

Commit

Permalink
Merge pull request #473 from danthe1st/java-autoformat
Browse files Browse the repository at this point in the history
use java codeblock when autoformatting code in help channels
  • Loading branch information
MoonTM-GIT authored Jan 26, 2024
2 parents 8c2a58f + ed1c7bb commit 164957b
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.discordjug.javabot.util.WebhookUtil;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

Expand All @@ -27,6 +28,8 @@
@RequiredArgsConstructor
@Component
public class AutoCodeFormatter {
private static final String CODEBLOCK_PREFIX = " ```java";
private static final String CODEBLOCK_SUFFIX = " ```";
private final AutoMod autoMod;
private final BotConfig botConfig;
private final UserPreferenceService preferenceService;
Expand Down Expand Up @@ -128,15 +131,16 @@ private void sendFormatHint(MessageReceivedEvent event) {
).queue();
}


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

0 comments on commit 164957b

Please sign in to comment.