Compare commits

..

No commits in common. "d614d99642d1cfb8dbf375865173f1d8c58ba759" and "654aca86cc6c1be827d5a321b20ea182832cd7b1" have entirely different histories.

4 changed files with 44 additions and 14 deletions

View file

@ -29,18 +29,45 @@ docker compose build
### Configure environment ### Configure environment
```shell ```shell
cp example.properties dchat.properties cp .env.example .env
``` ```
Fill the required variables. Fill the required variables.
### Start ### Start
For the fist time we want to start the containers in the following order:
First crate the DB.
```shell
docker compose up -d db
```
Crate the required tables and migrate already existing data.
```shell
docker compose up -d migration
```
Start the final apps.
```shell ```shell
docker compose up -d docker compose up -d
``` ```
The bot can now be invited with the following URL: `https://discord.com/oauth2/authorize?client_id=<application-id>&scope=applications.commands%20bot&permissions=513` ### Invite
Invite the bot through the link provided in the container logs.
```shell
docker compose logs bot
```
### Permissions
After starting the stack, navigate to [localhost:8081](http://localhost:8081)
and feel free to configure who and how much they can use the bot :)
## LICENSE ## LICENSE

View file

@ -12,6 +12,7 @@ import java.net.http.HttpClient;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executors;
public final class App { public final class App {
private static final Logger logger = LoggerFactory.getLogger(App.class); private static final Logger logger = LoggerFactory.getLogger(App.class);
@ -44,10 +45,10 @@ public final class App {
logger.error("Missing property: dchat.system-message"); logger.error("Missing property: dchat.system-message");
System.exit(1); System.exit(1);
} }
try (final var httpClient = HttpClient.newHttpClient()) { try (final var httpClient = HttpClient.newHttpClient(); final var executorService = Executors.newVirtualThreadPerTaskExecutor()) {
final var chatGPTService = new ChatGPTService(openaiApiKey, httpClient); final var chatGPTService = new ChatGPTService(openaiApiKey, httpClient);
final var discordRest = new DiscordRest(httpClient, discordApiKey); final var discordRest = new DiscordRest(httpClient, discordApiKey);
final var messageService = new MessageService(discordRest, chatGPTService, systemMessage); final var messageService = new MessageService(executorService, discordRest, chatGPTService, systemMessage);
final var eventHandler = new MessageEventHandler(allowedGuildIdList, botId, messageService); final var eventHandler = new MessageEventHandler(allowedGuildIdList, botId, messageService);
final var discordWebSocket = new DiscordWebSocket(discordApiKey, eventHandler); final var discordWebSocket = new DiscordWebSocket(discordApiKey, eventHandler);
discordWebSocket.start(); discordWebSocket.start();

View file

@ -10,14 +10,18 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
public final class MessageService { public final class MessageService {
private static final Logger logger = LoggerFactory.getLogger(MessageService.class); private static final Logger logger = LoggerFactory.getLogger(MessageService.class);
private final ExecutorService executorService;
private final DiscordRest discordRest; private final DiscordRest discordRest;
private final ChatGPTService chatGPTService; private final ChatGPTService chatGPTService;
private final String systemMessage; private final String systemMessage;
public MessageService(final DiscordRest discordRest, final ChatGPTService chatGPTService, String systemMessage) { public MessageService(final ExecutorService executorService, final DiscordRest discordRest, final ChatGPTService chatGPTService, String systemMessage) {
this.executorService = executorService;
this.discordRest = discordRest; this.discordRest = discordRest;
this.chatGPTService = chatGPTService; this.chatGPTService = chatGPTService;
this.systemMessage = systemMessage; this.systemMessage = systemMessage;
@ -42,7 +46,12 @@ public final class MessageService {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}; };
logger.debug("Handling message in new process: {}", originalMessageId);
Thread.startVirtualThread(callable).start(); // FIXME: We should find a solution to not block the main thread without loosing the started thread.
try {
executorService.submit(callable).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
} }
} }

View file

@ -2,14 +2,11 @@ package de.hhhammer.dchat.discord.ws.connection;
import de.hhhammer.dchat.discord.ws.Retryer; import de.hhhammer.dchat.discord.ws.Retryer;
import de.hhhammer.dchat.discord.ws.connection.event.CloseEvent; import de.hhhammer.dchat.discord.ws.connection.event.CloseEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration; import java.time.Duration;
public final class ConnectionManager { public final class ConnectionManager {
private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
private final String initGatewayUrl; private final String initGatewayUrl;
private final String token; private final String token;
private final Retryer retryer; private final Retryer retryer;
@ -35,10 +32,6 @@ public final class ConnectionManager {
final CloseEvent closeEvent = connector.connect(mutConnectionConfig); final CloseEvent closeEvent = connector.connect(mutConnectionConfig);
isResumable = switch (closeEvent) { isResumable = switch (closeEvent) {
case CloseEvent.ResumableCloseEvent resumableCloseEvent -> { case CloseEvent.ResumableCloseEvent resumableCloseEvent -> {
if (resumableCloseEvent.resumeGatewayUrl() == null) {
logger.warn("Unable to resume: Missing resume URL");
yield false;
}
mutConnectionConfig = new ConnectionConfig( mutConnectionConfig = new ConnectionConfig(
resumableCloseEvent.resumeGatewayUrl(), resumableCloseEvent.resumeGatewayUrl(),
new ResumeConnectionInitiator( new ResumeConnectionInitiator(