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 all 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;

private final SuggestionsConfig config;

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

private static void createThread(Message message) {
String title = message.getContentRaw();
String threadTitle = generateThreadTitle(message);
message.createThreadChannel(threadTitle).queue();
}

if (title.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = title.lastIndexOf(' ', TITLE_MAX_LENGTH);
/**
* Generates a title for the given message. The maximum length of the title is
* {@value #THREAD_TITLE_MAX_LENGTH}.
*
* @param message The message for which to generate the title.
* @return The generated and truncated thread title.
*/
private static String generateThreadTitle(Message message) {
String primaryTitle = message.getContentStripped();
String fallbackTitle = message.getAuthor().getEffectiveName() + "'s suggestion";
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