1
1
package org .togetherjava .tjbot .features .help ;
2
2
3
+ import net .dv8tion .jda .api .EmbedBuilder ;
3
4
import net .dv8tion .jda .api .entities .*;
4
5
import net .dv8tion .jda .api .entities .channel .attribute .IThreadContainer ;
5
6
import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
9
10
import net .dv8tion .jda .api .entities .channel .middleman .GuildChannel ;
10
11
import net .dv8tion .jda .api .interactions .components .buttons .Button ;
11
12
import net .dv8tion .jda .api .requests .RestAction ;
12
- import net .dv8tion .jda .api .requests .restaction .MessageCreateAction ;
13
13
import net .dv8tion .jda .internal .requests .CompletedRestAction ;
14
14
import org .slf4j .Logger ;
15
15
import org .slf4j .LoggerFactory ;
33
33
import java .util .Map ;
34
34
import java .util .Optional ;
35
35
import java .util .Set ;
36
- import java .util .concurrent .CopyOnWriteArrayList ;
37
36
import java .util .function .Consumer ;
38
37
import java .util .function .Function ;
39
38
import java .util .function .Predicate ;
@@ -117,7 +116,7 @@ public HelpSystemHelper(Config config, Database database, ChatGptService chatGpt
117
116
RestAction <Message > constructChatGptAttempt (ThreadChannel threadChannel ,
118
117
String originalQuestion , ComponentIdInteractor componentIdInteractor ) {
119
118
Optional <String > questionOptional = prepareChatGptQuestion (threadChannel , originalQuestion );
120
- Optional <String [] > chatGPTAnswer ;
119
+ Optional <String > chatGPTAnswer ;
121
120
122
121
if (questionOptional .isEmpty ()) {
123
122
return useChatGptFallbackMessage (threadChannel );
@@ -130,11 +129,12 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
130
129
131
130
String context = matchingTag .getName ();
132
131
chatGPTAnswer = chatGptService .ask (question , context );
132
+
133
133
if (chatGPTAnswer .isEmpty ()) {
134
134
return useChatGptFallbackMessage (threadChannel );
135
135
}
136
136
137
- List < String > ids = new CopyOnWriteArrayList <> ();
137
+ StringBuilder idForDismissButton = new StringBuilder ();
138
138
RestAction <Message > message =
139
139
mentionGuildSlashCommand (threadChannel .getGuild (), ChatGptCommand .COMMAND_NAME )
140
140
.map ("""
@@ -143,27 +143,43 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
143
143
%s.
144
144
""" ::formatted )
145
145
.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 ()));
151
147
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 ();
157
150
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 );
159
154
}
160
155
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 ();
162
179
}
163
180
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 );
167
183
return Button .danger (buttonId , "Dismiss" );
168
184
}
169
185
0 commit comments