11package org .togetherjava .tjbot .features .help ;
22
3+ import net .dv8tion .jda .api .EmbedBuilder ;
34import net .dv8tion .jda .api .entities .*;
45import net .dv8tion .jda .api .entities .channel .attribute .IThreadContainer ;
56import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
910import net .dv8tion .jda .api .entities .channel .middleman .GuildChannel ;
1011import net .dv8tion .jda .api .interactions .components .buttons .Button ;
1112import net .dv8tion .jda .api .requests .RestAction ;
12- import net .dv8tion .jda .api .requests .restaction .MessageCreateAction ;
1313import net .dv8tion .jda .internal .requests .CompletedRestAction ;
1414import org .slf4j .Logger ;
1515import org .slf4j .LoggerFactory ;
3333import java .util .Map ;
3434import java .util .Optional ;
3535import java .util .Set ;
36- import java .util .concurrent .CopyOnWriteArrayList ;
3736import java .util .function .Consumer ;
3837import java .util .function .Function ;
3938import java .util .function .Predicate ;
@@ -117,7 +116,7 @@ public HelpSystemHelper(Config config, Database database, ChatGptService chatGpt
117116 RestAction <Message > constructChatGptAttempt (ThreadChannel threadChannel ,
118117 String originalQuestion , ComponentIdInteractor componentIdInteractor ) {
119118 Optional <String > questionOptional = prepareChatGptQuestion (threadChannel , originalQuestion );
120- Optional <String [] > chatGPTAnswer ;
119+ Optional <String > chatGPTAnswer ;
121120
122121 if (questionOptional .isEmpty ()) {
123122 return useChatGptFallbackMessage (threadChannel );
@@ -130,11 +129,12 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
130129
131130 String context = matchingTag .getName ();
132131 chatGPTAnswer = chatGptService .ask (question , context );
132+
133133 if (chatGPTAnswer .isEmpty ()) {
134134 return useChatGptFallbackMessage (threadChannel );
135135 }
136136
137- List < String > ids = new CopyOnWriteArrayList <> ();
137+ StringBuilder idForDismissButton = new StringBuilder ();
138138 RestAction <Message > message =
139139 mentionGuildSlashCommand (threadChannel .getGuild (), ChatGptCommand .COMMAND_NAME )
140140 .map ("""
@@ -143,27 +143,43 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
143143 %s.
144144 """ ::formatted )
145145 .flatMap (threadChannel ::sendMessage )
146- .onSuccess (m -> ids .add (m .getId ()));
147- String [] answers = chatGPTAnswer .orElseThrow ();
148-
149- for (int i = 0 ; i < answers .length ; i ++) {
150- MessageCreateAction answer = threadChannel .sendMessage (answers [i ]);
146+ .onSuccess (m -> idForDismissButton .append (m .getId ()));
151147
152- if (i == answers .length - 1 ) {
153- message = message .flatMap (any -> answer
154- .addActionRow (generateDismissButton (componentIdInteractor , ids )));
155- continue ;
156- }
148+ String answer = chatGPTAnswer .orElseThrow ();
149+ SelfUser selfUser = threadChannel .getJDA ().getSelfUser ();
157150
158- message = message .flatMap (ignored -> answer .onSuccess (m -> ids .add (m .getId ())));
151+ int responseCharLimit = MessageEmbed .DESCRIPTION_MAX_LENGTH ;
152+ if (answer .length () > responseCharLimit ) {
153+ answer = answer .substring (0 , responseCharLimit );
159154 }
160155
161- return message ;
156+ MessageEmbed responseEmbed = generateGptResponseEmbed (answer , selfUser , originalQuestion );
157+ return message .flatMap (any -> threadChannel .sendMessageEmbeds (responseEmbed )
158+ .addActionRow (
159+ generateDismissButton (componentIdInteractor , idForDismissButton .toString ())));
160+ }
161+
162+ public MessageEmbed generateGptResponseEmbed (String answer , SelfUser selfUser , String title ) {
163+ String responseByGptFooter = "- AI generated response" ;
164+
165+ int embedTitleLimit = MessageEmbed .TITLE_MAX_LENGTH ;
166+ String capitalizedTitle = Character .toUpperCase (title .charAt (0 )) + title .substring (1 );
167+
168+ String titleForEmbed = capitalizedTitle .length () > embedTitleLimit
169+ ? capitalizedTitle .substring (0 , embedTitleLimit )
170+ : capitalizedTitle ;
171+
172+ return new EmbedBuilder ()
173+ .setAuthor (selfUser .getName (), null , selfUser .getEffectiveAvatarUrl ())
174+ .setTitle (titleForEmbed )
175+ .setDescription (answer )
176+ .setColor (Color .pink )
177+ .setFooter (responseByGptFooter )
178+ .build ();
162179 }
163180
164- private Button generateDismissButton (ComponentIdInteractor componentIdInteractor ,
165- List <String > ids ) {
166- String buttonId = componentIdInteractor .generateComponentId (ids .toArray (String []::new ));
181+ private Button generateDismissButton (ComponentIdInteractor componentIdInteractor , String id ) {
182+ String buttonId = componentIdInteractor .generateComponentId (id );
167183 return Button .danger (buttonId , "Dismiss" );
168184 }
169185
0 commit comments