Skip to content

fix: Handle empty message content for suggestion threads #1265

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
public final class SuggestionsUpDownVoter extends MessageReceiverAdapter {
private static final Logger logger = LoggerFactory.getLogger(SuggestionsUpDownVoter.class);
private static final int TITLE_MAX_LENGTH = 60;
private static final Emoji FALLBACK_UP_VOTE = Emoji.fromUnicode("👍");
private static final Emoji FALLBACK_DOWN_VOTE = Emoji.fromUnicode("👎");
private static final int THREAD_TITLE_MAX_LENGTH = 60; // Define the max length

private final SuggestionsConfig config;

Expand Down Expand Up @@ -55,19 +55,35 @@ public void onMessageReceived(MessageReceivedEvent event) {
}

private static void createThread(Message message) {
String title = message.getContentRaw();
String threadTitle = generateThreadTitle(message.getContentRaw(),
message.getAuthor().getName() + "'s suggestion");
message.createThreadChannel(threadTitle).queue();
}

if (title.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = title.lastIndexOf(' ', TITLE_MAX_LENGTH);
/**
* Generates a thread title, enforcing a maximum length of 60 characters. If an initial title
* exceeds this limit, it's truncated at the last word boundary before or at the 60-character
* mark to prevent cutting words mid-sentence. If no space is found, it truncates at 60
* characters. Uses a fallback title if the primary title is empty.
*
* @param primaryTitle The primary title to use.
* @param fallbackTitle The fallback title to use if the primary title is empty.
* @return The generated and truncated thread title.
*/
private static String generateThreadTitle(String primaryTitle, String fallbackTitle) {
String title = primaryTitle.isEmpty() ? fallbackTitle : primaryTitle;

if (lastWordEnd == -1) {
lastWordEnd = TITLE_MAX_LENGTH;
}
if (title.length() < THREAD_TITLE_MAX_LENGTH) {
return title;
}

int lastWordEnd = title.lastIndexOf(' ', THREAD_TITLE_MAX_LENGTH);

title = title.substring(0, lastWordEnd);
if (lastWordEnd == -1) {
return title.substring(0, THREAD_TITLE_MAX_LENGTH);
}

message.createThreadChannel(title).queue();
return title.substring(0, lastWordEnd);
}

private static void reactWith(String emojiName, Emoji fallbackEmoji, Guild guild,
Expand Down
Loading