2
2
3
3
import com .github .benmanes .caffeine .cache .Cache ;
4
4
import com .github .benmanes .caffeine .cache .Caffeine ;
5
+ import net .dv8tion .jda .api .entities .MessageEmbed ;
6
+ import net .dv8tion .jda .api .entities .SelfUser ;
5
7
import net .dv8tion .jda .api .events .interaction .ModalInteractionEvent ;
6
8
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
7
9
import net .dv8tion .jda .api .interactions .components .Modal ;
10
12
11
13
import org .togetherjava .tjbot .features .CommandVisibility ;
12
14
import org .togetherjava .tjbot .features .SlashCommandAdapter ;
15
+ import org .togetherjava .tjbot .features .help .HelpSystemHelper ;
13
16
14
17
import java .time .Duration ;
15
18
import java .time .Instant ;
@@ -28,6 +31,7 @@ public final class ChatGptCommand extends SlashCommandAdapter {
28
31
private static final int MIN_MESSAGE_INPUT_LENGTH = 4 ;
29
32
private static final Duration COMMAND_COOLDOWN = Duration .of (10 , ChronoUnit .SECONDS );
30
33
private final ChatGptService chatGptService ;
34
+ private final HelpSystemHelper helper ;
31
35
32
36
private final Cache <String , Instant > userIdToAskedAtCache =
33
37
Caffeine .newBuilder ().maximumSize (1_000 ).expireAfterWrite (COMMAND_COOLDOWN ).build ();
@@ -36,11 +40,13 @@ public final class ChatGptCommand extends SlashCommandAdapter {
36
40
* Creates an instance of the chatgpt command.
37
41
*
38
42
* @param chatGptService ChatGptService - Needed to make calls to ChatGPT API
43
+ * @param helper HelpSystemHelper - Needed to generate response embed for prompt
39
44
*/
40
- public ChatGptCommand (ChatGptService chatGptService ) {
45
+ public ChatGptCommand (ChatGptService chatGptService , HelpSystemHelper helper ) {
41
46
super (COMMAND_NAME , "Ask the ChatGPT AI a question!" , CommandVisibility .GUILD );
42
47
43
48
this .chatGptService = chatGptService ;
49
+ this .helper = helper ;
44
50
}
45
51
46
52
@ Override
@@ -75,20 +81,23 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
75
81
event .deferReply ().queue ();
76
82
77
83
String context = "" ;
78
- Optional <String []> optional =
79
- chatGptService .ask (event .getValue (QUESTION_INPUT ).getAsString (), context );
84
+ String question = event .getValue (QUESTION_INPUT ).getAsString ();
85
+
86
+ Optional <String > optional = chatGptService .ask (question , context );
80
87
if (optional .isPresent ()) {
81
88
userIdToAskedAtCache .put (event .getMember ().getId (), Instant .now ());
82
89
}
83
90
84
- String [] errorResponse = { """
91
+ String errorResponse = """
85
92
An error has occurred while trying to communicate with ChatGPT.
86
93
Please try again later.
87
- """ } ;
94
+ """ ;
88
95
89
- String [] response = optional .orElse (errorResponse );
90
- for (String message : response ) {
91
- event .getHook ().sendMessage (message ).queue ();
92
- }
96
+ String response = optional .orElse (errorResponse );
97
+ SelfUser selfUser = event .getJDA ().getSelfUser ();
98
+
99
+ MessageEmbed responseEmbed = helper .generateGptResponseEmbed (response , selfUser , question );
100
+
101
+ event .getHook ().sendMessageEmbeds (responseEmbed ).queue ();
93
102
}
94
103
}
0 commit comments