Skip to content

Commit da145c7

Browse files
committed
Added AI :>
1 parent 4c0be6b commit da145c7

5 files changed

Lines changed: 184 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ dependencies {
7979
installer('org.mangorage:installer:3.0.27')
8080
bot('org.mangorage:mangobot:11.0.18')
8181

82-
plugin('org.mangorage:mangobotplugin:11.1.6')
82+
plugin('org.mangorage:mangobotplugin:11.1.7')
8383
plugin('org.mangorage:mangobotwebsite:1.0.10')
8484

8585
compileOnly('org.mangorage:MangoBotGradle:4.0.5')
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.mangorage.mangobotgithub;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.mangorage.mangobotapi.core.commands.Arguments;
6+
import org.mangorage.mangobotapi.core.commands.CommandResult;
7+
import org.mangorage.mangobotapi.core.commands.CommandType;
8+
import org.mangorage.mangobotapi.core.commands.IBasicCommand;
9+
10+
import java.io.IOException;
11+
12+
public class AICommand implements IBasicCommand {
13+
@Override
14+
public @NotNull CommandResult execute(Message message, Arguments arguments) {
15+
var prompt = arguments.getFrom(0);
16+
if (prompt.isBlank()) {
17+
18+
} else {
19+
try {
20+
var response = ChatGPTBot.askChatGPT(prompt);
21+
if (response != null) {
22+
var choices = response.getChoices();
23+
if (!choices.isEmpty()) {
24+
var msg = choices.getFirst().getMessage();
25+
if (msg != null) {
26+
message.reply(msg.getContent()).setSuppressEmbeds(true).mentionRepliedUser(false).queue();
27+
}
28+
}
29+
}
30+
} catch (IOException ignored) {
31+
}
32+
}
33+
return CommandResult.PASS;
34+
}
35+
36+
@Override
37+
public String commandId() {
38+
return "askAI";
39+
}
40+
41+
@Override
42+
public CommandType commandType() {
43+
return CommandType.BOTH;
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.mangorage.mangobotgithub;
2+
import com.google.gson.Gson;
3+
import okhttp3.*;
4+
5+
import java.io.IOException;
6+
7+
public class ChatGPTBot {
8+
private static final String API_URL = "https://api.openai.com/v1/chat/completions";
9+
private static final Gson gson = new Gson();
10+
11+
public static ChatGPTResponse askChatGPT(String prompt) throws IOException {
12+
OkHttpClient client = new OkHttpClient();
13+
14+
String json = "{ \"model\": \"gpt-4o-mini\", \"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}] }";
15+
16+
RequestBody body = RequestBody.create(MediaType.get("application/json"), json);
17+
Request request = new Request.Builder()
18+
.url(API_URL)
19+
.post(body)
20+
.addHeader("Authorization", "Bearer " + MangoBotGithub.CHAT_AI_TOKEN.get())
21+
.build();
22+
23+
try (Response response = client.newCall(request).execute()) {
24+
if (response.isSuccessful()) {
25+
String responseBody = response.body().string();
26+
System.out.println(responseBody);
27+
return gson.fromJson(responseBody, ChatGPTResponse.class);
28+
}
29+
}
30+
return null;
31+
}
32+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.mangorage.mangobotgithub;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.List;
6+
7+
public class ChatGPTResponse {
8+
9+
private String id;
10+
private String object;
11+
private int created;
12+
private String model;
13+
14+
@SerializedName("choices")
15+
private List<Choice> choices;
16+
17+
// Define inner class for choices
18+
public static class Choice {
19+
private Message message;
20+
private int index;
21+
22+
// Getters and setters
23+
public Message getMessage() {
24+
return message;
25+
}
26+
27+
public void setMessage(Message message) {
28+
this.message = message;
29+
}
30+
31+
public int getIndex() {
32+
return index;
33+
}
34+
35+
public void setIndex(int index) {
36+
this.index = index;
37+
}
38+
}
39+
40+
// Define inner class for message
41+
public static class Message {
42+
private String role;
43+
private String content;
44+
45+
// Getters and setters
46+
public String getRole() {
47+
return role;
48+
}
49+
50+
public void setRole(String role) {
51+
this.role = role;
52+
}
53+
54+
public String getContent() {
55+
return content;
56+
}
57+
58+
public void setContent(String content) {
59+
this.content = content;
60+
}
61+
}
62+
63+
// Getters and setters for top-level fields
64+
public String getId() {
65+
return id;
66+
}
67+
68+
public void setId(String id) {
69+
this.id = id;
70+
}
71+
72+
public String getObject() {
73+
return object;
74+
}
75+
76+
public void setObject(String object) {
77+
this.object = object;
78+
}
79+
80+
public int getCreated() {
81+
return created;
82+
}
83+
84+
public void setCreated(int created) {
85+
this.created = created;
86+
}
87+
88+
public String getModel() {
89+
return model;
90+
}
91+
92+
public void setModel(String model) {
93+
this.model = model;
94+
}
95+
96+
public List<Choice> getChoices() {
97+
return choices;
98+
}
99+
100+
public void setChoices(List<Choice> choices) {
101+
this.choices = choices;
102+
}
103+
}

src/main/java/org/mangorage/mangobotgithub/MangoBotGithub.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class MangoBotGithub extends AbstractPlugin {
2727

2828
public static final ISetting<String> GITHUB_TOKEN = ConfigSetting.create(CONFIG, "PASTE_TOKEN", "empty");
2929
public static final ISetting<String> GITHUB_USERNAME = ConfigSetting.create(CONFIG, "GITHUB_USERNAME", "RealMangoRage");
30+
public static final ISetting<String> CHAT_AI_TOKEN = ConfigSetting.create(CONFIG, "AI_TOKEN", "empty");
3031

3132

3233
private final MangoBotPlugin parent;
@@ -49,9 +50,11 @@ public void onRegistration(StartupEvent event) {
4950
if (event.phase() == StartupEvent.Phase.REGISTRATION) {
5051
GuildConfig.loadServerConfigs();
5152

53+
5254
var cmdRegistry = parent.getCommandRegistry();
5355
cmdRegistry.addBasicCommand(new PRScanCommand(parent));
5456
cmdRegistry.addBasicCommand(new IssueScanCommand(parent));
57+
cmdRegistry.addBasicCommand(new AICommand());
5558

5659
new GHPRStatus(parent);
5760
new GHIssueStatus(parent);

0 commit comments

Comments
 (0)