Skip to content

Commit 661b9da

Browse files
committed
Updated cafeBot Imports
1 parent 2fbe3c1 commit 661b9da

File tree

6 files changed

+49
-57
lines changed

6 files changed

+49
-57
lines changed

bot/build.gradle.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ tasks.clean {
8484
dependencies {
8585
implementation(project(":wrappers:cafe-api-wrapper"))
8686

87-
implementation("net.dv8tion", "JDA", "5.0.1") { exclude(module = "opus-java") }
87+
implementation("net.dv8tion", "JDA", "5.2.1") { exclude(module = "opus-java") }
8888

89-
implementation("org.apache.logging.log4j", "log4j-api", "2.23.1")
90-
implementation("org.apache.logging.log4j", "log4j-core", "2.23.1")
91-
implementation("org.apache.logging.log4j", "log4j-slf4j2-impl", "2.23.1") // JDA logging.
89+
implementation("org.apache.logging.log4j", "log4j-api", "2.24.1") // Logging - https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
90+
implementation("org.apache.logging.log4j", "log4j-core", "2.24.1")
91+
implementation("org.apache.logging.log4j", "log4j-slf4j2-impl", "2.24.1") // JDA logging.
9292

93-
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1")
93+
implementation("com.fasterxml.jackson.core", "jackson-databind", "2.18.1") // JSON - https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
9494

95-
implementation("org.apache.httpcomponents.client5:httpclient5:5.3.1") // OpenAI HTTP Requests
95+
implementation("org.apache.httpcomponents.client5", "httpclient5", "5.3.1") // HTTP Requests (For OpenAI)
9696

97-
implementation("com.github.twitch4j", "twitch4j", "1.21.0")
97+
implementation("com.github.twitch4j", "twitch4j", "1.23.0") // Twitch - https://github.com/twitch4j/twitch4j
9898

9999
compileOnly("org.projectlombok", "lombok", "1.18.32")
100100
annotationProcessor("org.projectlombok", "lombok", "1.18.32")

bot/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ public CafeBot() throws InterruptedException {
125125
.setActivity(Activity.playing("The barista is starting..."))
126126
.setStatus(OnlineStatus.IDLE)
127127
.enableIntents(
128-
GatewayIntent.GUILD_EMOJIS_AND_STICKERS,
128+
GatewayIntent.GUILD_EXPRESSIONS,
129129
GatewayIntent.GUILD_MEMBERS,
130130
GatewayIntent.DIRECT_MESSAGES,
131131
GatewayIntent.MESSAGE_CONTENT
132132
)
133-
.setMemberCachePolicy(MemberCachePolicy.ALL) // ! - Needed for mutual guilds
134-
.setChunkingFilter(ChunkingFilter.ALL) // ! - Needed for mutual guilds
133+
.setMemberCachePolicy(MemberCachePolicy.ALL) // ! - Needed for mutual guilds // TODO: REMOVE?
134+
.setChunkingFilter(ChunkingFilter.ALL) // ! - Needed for mutual guilds // TODO: REMOVE?
135135
.build();
136136

137137
logger.log(CafeBot.class, LogLevel.INFO, "Checking servers...");

bot/src/main/java/com/beanbeanjuice/cafebot/utility/api/OpenAIAPIWrapper.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.beanbeanjuice.cafebot.utility.api;
22

3-
import com.beanbeanjuice.cafeapi.wrapper.requests.Request;
43
import com.beanbeanjuice.cafebot.CafeBot;
5-
import com.beanbeanjuice.cafebot.utility.listeners.ai.AIResponseListener;
64
import com.beanbeanjuice.cafebot.utility.listeners.ai.PreviousMessage;
5+
import com.beanbeanjuice.cafebot.utility.logging.LogLevel;
76
import com.fasterxml.jackson.databind.JsonNode;
87
import com.fasterxml.jackson.databind.ObjectMapper;
98
import com.fasterxml.jackson.databind.node.ArrayNode;
10-
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
119
import com.fasterxml.jackson.databind.node.ObjectNode;
12-
import com.fasterxml.jackson.databind.util.JSONPObject;
1310
import lombok.RequiredArgsConstructor;
1411
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
1512
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
@@ -19,7 +16,6 @@
1916
import org.apache.hc.core5.http.HttpResponse;
2017
import org.apache.hc.core5.http.HttpStatus;
2118
import org.apache.hc.core5.http.Method;
22-
import org.apache.hc.core5.net.URIAuthority;
2319
import org.apache.hc.core5.net.URIBuilder;
2420

2521
import java.io.IOException;
@@ -34,6 +30,7 @@
3430
@RequiredArgsConstructor
3531
public class OpenAIAPIWrapper {
3632

33+
private final CafeBot bot;
3734
private final String authorizationKey;
3835
private final String assistantID;
3936
private final Map<String, Map<String, Queue<PreviousMessage>>> previousMessageMap;
@@ -55,8 +52,6 @@ public void setHeaders() {
5552
private CompletableFuture<String> getThread(final String guildID) throws URISyntaxException {
5653
if (aiThreads.containsKey(guildID)) return CompletableFuture.supplyAsync(() -> aiThreads.get(guildID));
5754

58-
System.out.println("Thread not found, creating.");
59-
6055
return createThread().thenApplyAsync((threadID) -> {
6156
aiThreads.put(guildID, threadID);
6257
return threadID;
@@ -84,7 +79,10 @@ private CompletableFuture<String> createThread() throws URISyntaxException {
8479
throw new RuntimeException(e);
8580
}
8681

87-
return body.get("id").asText();
82+
String newThreadID = body.get("id").asText();
83+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.DEBUG, "Created AI Thread: " + newThreadID, true, false);
84+
85+
return newThreadID;
8886
});
8987
}
9088

@@ -119,9 +117,11 @@ private CompletableFuture<String> createRun(final String guildID, final String c
119117

120118
return CompletableFuture.supplyAsync(() -> {
121119
SimpleHttpResponse httpResponse = null;
120+
122121
try {
123122
httpResponse = (SimpleHttpResponse) this.get(httpRequest);
124123
} catch (Exception e) {
124+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.WARN, "Error getting response from run.");
125125
throw new RuntimeException(e);
126126
}
127127

@@ -131,10 +131,14 @@ private CompletableFuture<String> createRun(final String guildID, final String c
131131
try {
132132
body = new ObjectMapper().readTree(bodyBytes);
133133
} catch (IOException e) {
134+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.WARN, "Error reading response from run.");
134135
throw new RuntimeException(e);
135136
}
136137

137-
return body.get("id").asText();
138+
String newRunID = body.get("id").asText();
139+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.DEBUG, "Created Run: " + newRunID);
140+
141+
return newRunID;
138142
});
139143

140144
}
@@ -156,7 +160,10 @@ private CompletableFuture<String> getLatestMessageFromRun(final String threadID,
156160
try { body = new ObjectMapper().readTree(bodyBytes); }
157161
catch (IOException e) { throw new RuntimeException(e); }
158162

159-
return body.get("data").get(0).get("content").get(0).get("text").get("value").asText();
163+
String newResponse = body.get("data").get(0).get("content").get(0).get("text").get("value").asText();
164+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.DEBUG, "Created New Response: " + newResponse);
165+
166+
return newResponse;
160167
});
161168
}
162169

@@ -176,7 +183,10 @@ private CompletableFuture<String> getRunStatus(final String threadID, final Stri
176183
try { body = new ObjectMapper().readTree(bodyBytes); }
177184
catch (IOException e) { throw new RuntimeException(e); }
178185

179-
return body.get("status").asText();
186+
String newRunStatus = body.get("status").asText();
187+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.DEBUG, "New AI Run Status: " + newRunStatus);
188+
189+
return newRunStatus;
180190
});
181191
}
182192

@@ -219,8 +229,13 @@ public CompletableFuture<String> getResponse(final String guildID, final String
219229
// Run assistant on thread
220230
.thenComposeAsync(
221231
(threadID) -> {
222-
try { return this.createRun(guildID, channelID, threadID); }
223-
catch (URISyntaxException e) { throw new RuntimeException(e); }
232+
try {
233+
return this.createRun(guildID, channelID, threadID);
234+
}
235+
catch (URISyntaxException e) {
236+
bot.getLogger().log(OpenAIAPIWrapper.class, LogLevel.WARN, "Error running AI on thread.");
237+
throw new RuntimeException(e);
238+
}
224239
}
225240
)
226241

bot/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/ai/AIResponseListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AIResponseListener(final CafeBot cafeBot, final String openAIAPIKey, fina
2828
this.messageMap = new HashMap<>();
2929

3030
previousMessageMap = new HashMap<>();
31-
this.openAI = new OpenAIAPIWrapper(openAIAPIKey, openAIAssistantID, previousMessageMap);
31+
this.openAI = new OpenAIAPIWrapper(cafeBot, openAIAPIKey, openAIAssistantID, previousMessageMap);
3232
this.openAI.setHeaders();
3333
refreshMaps();
3434
}
@@ -77,7 +77,7 @@ private void handleOpenAIResponse(final MessageReceivedEvent event) {
7777
try {
7878
event.getChannel().sendTyping().queue();
7979

80-
cafeBot.getLogger().log(AIResponseListener.class, LogLevel.INFO, String.format("Running AI For User (%s) on Guild (%s): %s", event.getAuthor().getId(), event.getGuild().getId(), event.getMessage().getContentRaw()), false, false);
80+
cafeBot.getLogger().log(AIResponseListener.class, LogLevel.INFO, String.format("Running AI For User (%s) on Guild (%s): %s", event.getAuthor().getId(), event.getGuild().getId(), event.getMessage().getContentRaw()), true, false);
8181
openAI.getResponse(event.getGuild().getId(), event.getChannel().getId())
8282
.thenAcceptAsync((response) -> event.getMessage().reply(response).queue());
8383
} catch (URISyntaxException e) {

bot/src/main/java/com/beanbeanjuice/cafebot/utility/logging/LogLevel.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.beanbeanjuice.cafebot.utility.logging;
22

3-
import org.jetbrains.annotations.NotNull;
3+
import lombok.Getter;
44

55
import java.awt.*;
66

@@ -9,6 +9,7 @@
99
*
1010
* @author beanbeanjuice
1111
*/
12+
@Getter
1213
public enum LogLevel {
1314

1415
DEBUG(Color.blue, "TEST", "TEST", "https://cdn.beanbeanjuice.com/images/cafeBot/log_level/debug.png"),
@@ -36,41 +37,10 @@ public enum LogLevel {
3637
this.imageURL = imageURL;
3738
}
3839

39-
/**
40-
* @return The color to be displayed in the {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbed}.
41-
*/
42-
public Color getColor() {
43-
return color;
44-
}
45-
46-
/**
47-
* @return The code for the {@link LogLevel}.
48-
*/
49-
public String getMessage() {
50-
return message;
51-
}
52-
53-
/**
54-
* @return The image URL for the {@link LogLevel}.
55-
*/
56-
public String getImageURL() {
57-
return imageURL;
58-
}
59-
60-
/**
61-
* Gets the default code for the {@link LogLevel} chosen.
62-
* @return The code in its unedited, default state.
63-
*/
64-
@NotNull
65-
public String getCode() {
66-
return code;
67-
}
68-
6940
/**
7041
* Gets the formatted code for the {@link LogLevel} chosen.
7142
* @return The code in its formatted state.
7243
*/
73-
@NotNull
7444
public String formatCode() {
7545
return code + (".").repeat(Math.max(0, 10 - code.length()));
7646
}

bot/src/main/java/com/beanbeanjuice/cafebot/utility/logging/LogManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.beanbeanjuice.cafeapi.wrapper.utility.Time;
88
import net.dv8tion.jda.api.EmbedBuilder;
99
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
10+
import org.apache.logging.log4j.Level;
1011
import org.apache.logging.log4j.Logger;
12+
import org.apache.logging.log4j.core.config.Configurator;
1113
import org.jetbrains.annotations.NotNull;
1214
import org.jetbrains.annotations.Nullable;
1315

@@ -43,6 +45,11 @@ public class LogManager {
4345
public LogManager(CafeBot cafeBot, String name, String guildID, String logChannelID, String filePath) {
4446
this.cafeBot = cafeBot;
4547

48+
// Set log level for everything.
49+
Optional<String> logLevelString = Optional.ofNullable(System.getenv("CAFEBOT_LOG_LEVEL"));
50+
Level level = logLevelString.map(Level::valueOf).orElse(Level.INFO);
51+
Configurator.setAllLevels(org.apache.logging.log4j.LogManager.getRootLogger().getName(), level);
52+
4653
time = new Time();
4754

4855
this.name = name;

0 commit comments

Comments
 (0)