Skip to content

Commit 01c0c16

Browse files
Implemented message-splitting for messages for >2000 chars in length
1 parent e1f7aac commit 01c0c16

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.javadiscord.javabot.listener;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import net.dv8tion.jda.api.entities.channel.ChannelType;
5+
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
6+
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
7+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
8+
import net.dv8tion.jda.api.interactions.components.buttons.Button;
9+
import net.javadiscord.javabot.data.config.BotConfig;
10+
import net.javadiscord.javabot.data.config.guild.QOTWConfig;
11+
import net.javadiscord.javabot.util.InteractionUtils;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
/**
15+
* Listens for new messages inside QOTW submissions and warns the author about the webhook limitations.
16+
*/
17+
@RequiredArgsConstructor
18+
public class QOTWSubmissionListener extends ListenerAdapter {
19+
private final BotConfig config;
20+
21+
@Override
22+
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
23+
if (!event.isFromGuild() || !event.isFromThread() || event.getChannelType() != ChannelType.GUILD_PRIVATE_THREAD) {
24+
return;
25+
}
26+
QOTWConfig qotwConfig = config.get(event.getGuild()).getQotwConfig();
27+
ThreadChannel thread = event.getChannel().asThreadChannel();
28+
if (thread.getParentChannel().getIdLong() != qotwConfig.getSubmissionChannelId()) {
29+
return;
30+
}
31+
if (event.getMessage().getContentRaw().length() > 2000) {
32+
event.getChannel().sendMessageFormat("""
33+
Hey %s!
34+
Please keep in mind that messages **over 2000 characters** get split in half due to webhook limitations.
35+
If you want to make sure that your submission is properly formatted, split your message into smaller chunks instead.""",
36+
event.getAuthor().getAsMention())
37+
.setActionRow(Button.secondary(InteractionUtils.DELETE_ORIGINAL_TEMPLATE, "\uD83D\uDDD1️"))
38+
.queue();
39+
}
40+
}
41+
}

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,14 @@ public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread
178178
getMessagesByUser(thread, author).thenAccept(messages -> {
179179
for (Message message : messages) {
180180
if (message.getAuthor().isBot() || message.getType() != MessageType.DEFAULT) continue;
181-
WebhookUtil.ensureWebhookExists(newestPost.getParentChannel().asForumChannel(), wh ->
182-
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong()));
181+
WebhookUtil.ensureWebhookExists(newestPost.getParentChannel().asForumChannel(), wh -> {
182+
if (message.getContentRaw().length() > 2000) {
183+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(0, 2000), newestPost.getIdLong());
184+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(2001), newestPost.getIdLong());
185+
} else {
186+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong());
187+
}
188+
});
183189
}
184190
newestPost.sendMessageEmbeds(buildAuthorEmbed(author, bestAnswer)).queue();
185191
});

0 commit comments

Comments
 (0)