1515import org .togetherjava .tjbot .features .chatgpt .ChatGptService ;
1616
1717import java .util .Arrays ;
18- import java .util .Objects ;
1918import java .util .Optional ;
2019
2120/**
22- * The implemented command is {@code /rewrite-msg }, which allows users to have their message
23- * rewritten in a clearer, more professional, or better structured form using AI.
21+ * The implemented command is {@code /rewrite}, which allows users to have their message rewritten
22+ * in a clearer, more professional, or better structured form using AI.
2423 * <p>
2524 * The rewritten message is shown as an ephemeral message visible only to the user who triggered the
2625 * command.
@@ -35,39 +34,33 @@ public final class RewriteCommand extends SlashCommandAdapter {
3534
3635 private static final int MAX_MESSAGE_LENGTH = Message .MAX_CONTENT_LENGTH ;
3736 private static final int MIN_MESSAGE_LENGTH = 3 ;
38- private static final ChatGptModel CHAT_GPT_MODEL = ChatGptModel .FASTEST ;
3937
4038 private final ChatGptService chatGptService ;
4139
42- private static String createAiPrompt (String userMessage , MessageTone tone ) {
43- return """
44- Rewrite the following message to make it clearer, more professional, \
45- and better structured. Maintain the original meaning while improving the quality \
46- of the writing. Do NOT use em-dashes (—). %s
47-
48- IMPORTANT: The rewritten text MUST be no more than 2000 characters. \
49- If needed, compress wording while preserving key details and intent.
50-
51- If the message is already well-written, provide minor improvements.
52-
53- Original message:
54- %s""" .stripIndent ().formatted (tone .description , userMessage );
40+ private static ChatGptModel selectAiModel (MessageTone tone ) {
41+ return switch (tone ) {
42+ case CLEAR , PROFESSIONAL -> ChatGptModel .FASTEST ;
43+ case DETAILED , TECHNICAL -> ChatGptModel .HIGH_QUALITY ;
44+ };
5545 }
5646
57- private static String buildOriginalMsgResponse (String userMessage , MessageTone tone ) {
47+ private static String createAiPrompt (String userMessage , MessageTone tone ) {
5848 return """
59- **Original message (%s)**
49+ You are rewriting a Discord text chat message for clarity and professionalism.
50+ Keep it conversational and casual—NOT email or formal document format.
6051
61- %s
62- """ .stripIndent ().formatted (tone .displayName , userMessage );
63- }
52+ Tone: %s
6453
65- private static String buildRewrittenMsgResponse (String aiMessage , MessageTone tone ) {
66- return """
67- **Rewritten message (%s)**
54+ Rewrite the message to:
55+ - Improve clarity and structure
56+ - Maintain the original meaning
57+ - Avoid em-dashes (—)
58+ - Stay under %d characters (strict limit)
59+
60+ If the message is already well-written, make only minor improvements.
6861
69- %s
70- """ .stripIndent ().formatted (tone .displayName , aiMessage );
62+ Message to rewrite:
63+ %s """ .stripIndent ().formatted (tone .description , MAX_MESSAGE_LENGTH , userMessage );
7164 }
7265
7366 /**
@@ -100,8 +93,13 @@ public RewriteCommand(ChatGptService chatGptService) {
10093 @ Override
10194 public void onSlashCommand (SlashCommandInteractionEvent event ) {
10295
103- final String userMessage =
104- Objects .requireNonNull (event .getOption (MESSAGE_OPTION )).getAsString ();
96+ final OptionMapping messageOption = event .getOption (MESSAGE_OPTION );
97+
98+ if (messageOption == null ) {
99+ throw new IllegalStateException ("Required option '" + MESSAGE_OPTION + "' is missing" );
100+ }
101+
102+ final String userMessage = messageOption .getAsString ();
105103 final MessageTone tone = parseTone (event .getOption (TONE_OPTION ));
106104
107105 event .deferReply (true ).queue ();
@@ -124,15 +122,7 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
124122
125123 logger .debug ("Rewrite successful; rewritten message length: {}" , rewrittenText .length ());
126124
127- event .getHook ()
128- .sendMessage (buildOriginalMsgResponse (userMessage , tone ))
129- .setEphemeral (true )
130- .queue ();
131-
132- event .getHook ()
133- .sendMessage (buildRewrittenMsgResponse (rewrittenText , tone ))
134- .setEphemeral (true )
135- .queue ();
125+ event .getHook ().sendMessage (rewrittenText ).setEphemeral (true ).queue ();
136126 }
137127
138128 private MessageTone parseTone (@ Nullable OptionMapping toneOption )
@@ -150,10 +140,11 @@ private MessageTone parseTone(@Nullable OptionMapping toneOption)
150140
151141 private Optional <String > rewrite (String userMessage , MessageTone tone ) {
152142
143+ final ChatGptModel aiModel = selectAiModel (tone );
144+
153145 final String rewritePrompt = createAiPrompt (userMessage , tone );
154146
155- Optional <String > attempt =
156- chatGptService .ask (rewritePrompt , tone .displayName , CHAT_GPT_MODEL );
147+ Optional <String > attempt = chatGptService .askRaw (rewritePrompt , aiModel );
157148
158149 if (attempt .isEmpty ()) {
159150 return attempt ;
@@ -166,20 +157,19 @@ private Optional<String> rewrite(String userMessage, MessageTone tone) {
166157 }
167158
168159 logger .debug ("Rewritten message exceeded {} characters; retrying with stricter constraint" ,
169- Message . MAX_CONTENT_LENGTH );
160+ MAX_MESSAGE_LENGTH );
170161
171162 final String shortenPrompt = rewritePrompt
172- + "\n \n Constraint reminder: Your previous rewrite exceeded "
173- + Message .MAX_CONTENT_LENGTH
174- + " characters. Provide a revised rewrite strictly under "
175- + Message .MAX_CONTENT_LENGTH + " characters while preserving meaning and tone." ;
163+ + "\n \n Constraint reminder: Your previous rewrite exceeded " + MAX_MESSAGE_LENGTH
164+ + " characters. Provide a revised rewrite strictly under " + MAX_MESSAGE_LENGTH
165+ + " characters while preserving meaning and tone." ;
176166
177- return chatGptService .ask (shortenPrompt , tone . displayName , CHAT_GPT_MODEL );
167+ return chatGptService .askRaw (shortenPrompt , aiModel );
178168 }
179169
180170 private enum MessageTone {
181171 CLEAR ("Clear" , "Make it clear and easy to understand." ),
182- PRO ( "Pro " , "Use a professional and polished tone." ),
172+ PROFESSIONAL ( "Professional " , "Use a professional and polished tone." ),
183173 DETAILED ("Detailed" , "Expand with more detail and explanation." ),
184174 TECHNICAL ("Technical" , "Use technical and specialized language where appropriate." );
185175
0 commit comments