@@ -25,6 +25,7 @@ public final class SuggestionsUpDownVoter extends MessageReceiverAdapter {
2525 private static final Logger logger = LoggerFactory .getLogger (SuggestionsUpDownVoter .class );
2626 private static final Emoji FALLBACK_UP_VOTE = Emoji .fromUnicode ("👍" );
2727 private static final Emoji FALLBACK_DOWN_VOTE = Emoji .fromUnicode ("👎" );
28+ private static final int THREAD_TITLE_MAX_LENGTH = 60 ; // Define the max length
2829
2930 private final SuggestionsConfig config ;
3031
@@ -54,10 +55,35 @@ public void onMessageReceived(MessageReceivedEvent event) {
5455 }
5556
5657 private static void createThread (Message message ) {
57- ThreadTitle threadTitle = ThreadTitle .withFallback (message .getContentRaw (),
58- message .getAuthor ().getName () + "'s suggestions" );
58+ String threadTitle = generateThreadTitle (message .getContentRaw (),
59+ message .getAuthor ().getName () + "'s suggestion" );
60+ message .createThreadChannel (threadTitle ).queue ();
61+ }
62+
63+ /**
64+ * Generates a thread title, enforcing a maximum length of 60 characters. If an initial title
65+ * exceeds this limit, it's truncated at the last word boundary before or at the 60-character
66+ * mark to prevent cutting words mid-sentence. If no space is found, it truncates at 60
67+ * characters. Uses a fallback title if the primary title is empty.
68+ *
69+ * @param primaryTitle The primary title to use.
70+ * @param fallbackTitle The fallback title to use if the primary title is empty.
71+ * @return The generated and truncated thread title.
72+ */
73+ private static String generateThreadTitle (String primaryTitle , String fallbackTitle ) {
74+ String title = primaryTitle .isEmpty () ? fallbackTitle : primaryTitle ;
75+
76+ if (title .length () < THREAD_TITLE_MAX_LENGTH ) {
77+ return title ;
78+ }
79+
80+ int lastWordEnd = title .lastIndexOf (' ' , THREAD_TITLE_MAX_LENGTH );
81+
82+ if (lastWordEnd == -1 ) {
83+ return title .substring (0 , THREAD_TITLE_MAX_LENGTH );
84+ }
5985
60- message . createThreadChannel ( threadTitle . value ()). queue ( );
86+ return title . substring ( 0 , lastWordEnd );
6187 }
6288
6389 private static void reactWith (String emojiName , Emoji fallbackEmoji , Guild guild ,
0 commit comments