|
3 | 3 | import org.bukkit.command.Command;
|
4 | 4 | import org.bukkit.command.CommandExecutor;
|
5 | 5 | import org.bukkit.command.CommandSender;
|
6 |
| -import org.apache.http.client.methods.CloseableHttpResponse; |
7 |
| -import org.apache.http.client.methods.HttpPost; |
8 |
| -import org.apache.http.entity.StringEntity; |
9 |
| -import org.apache.http.impl.client.CloseableHttpClient; |
10 |
| -import org.apache.http.impl.client.HttpClients; |
11 |
| -import org.apache.http.util.EntityUtils; |
12 | 6 | import org.json.JSONArray;
|
13 | 7 | import org.json.JSONObject;
|
14 | 8 |
|
15 | 9 | import java.util.logging.Level;
|
16 | 10 | import java.util.logging.Logger;
|
17 | 11 | import java.util.List;
|
18 | 12 |
|
| 13 | +import jodd.http.HttpRequest; |
| 14 | +import jodd.http.HttpResponse; |
19 | 15 | public class CommandHandler implements CommandExecutor {
|
20 | 16 | private final Main plugin;
|
21 | 17 | private final ConfigManager configManager;
|
@@ -97,26 +93,21 @@ private void askChatGPT(CommandSender sender, String question) {
|
97 | 93 | messages.put(message);
|
98 | 94 | json.put("messages", messages);
|
99 | 95 |
|
100 |
| - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
101 |
| - HttpPost request = new HttpPost(configManager.getBaseUrl() + "/chat/completions"); |
102 |
| - request.setHeader("Content-Type", "application/json"); |
103 |
| - request.setHeader("Authorization", "Bearer " + configManager.getApiKey()); |
104 |
| - request.setEntity(new StringEntity(json.toString(), "UTF-8")); |
| 96 | + HttpRequest request = HttpRequest.post(configManager.getBaseUrl() + "/chat/completions") |
| 97 | + .header("Content-Type", "application/json") |
| 98 | + .header("Authorization", "Bearer " + configManager.getApiKey()) |
| 99 | + .body(json.toString()); |
105 | 100 |
|
106 |
| - try (CloseableHttpResponse response = httpClient.execute(request)) { |
107 |
| - if (response.getStatusLine().getStatusCode() == 200) { |
108 |
| - String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); |
109 |
| - JSONObject jsonResponse = new JSONObject(responseBody); |
110 |
| - String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"); |
111 |
| - sender.sendMessage(configManager.getChatGPTResponseMessage().replace("%s", answer)); |
112 |
| - } else { |
113 |
| - String errorBody = EntityUtils.toString(response.getEntity(), "UTF-8"); |
114 |
| - logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody); |
115 |
| - sender.sendMessage(configManager.getChatGPTErrorMessage()); |
116 |
| - } |
117 |
| - } |
118 |
| - } catch (Exception e) { |
119 |
| - logger.log(Level.SEVERE, "Failed to contact ChatGPT: " + e.getMessage(), e); |
| 101 | + HttpResponse response = request.send(); |
| 102 | + |
| 103 | + if (response.statusCode() == 200) { |
| 104 | + String responseBody = response.bodyText(); |
| 105 | + JSONObject jsonResponse = new JSONObject(responseBody); |
| 106 | + String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"); |
| 107 | + sender.sendMessage(configManager.getChatGPTResponseMessage().replace("%s", answer)); |
| 108 | + } else { |
| 109 | + String errorBody = response.bodyText(); |
| 110 | + logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody); |
120 | 111 | sender.sendMessage(configManager.getChatGPTErrorMessage());
|
121 | 112 | }
|
122 | 113 | }
|
|
0 commit comments