Compare commits

..

3 commits

Author SHA1 Message Date
d614d99642 doc: Update README to new version 2024-11-28 20:46:04 +01:00
f2e5b04c1c discord-ws: Fix resume if no resume URL is given
This can happen if something goes wrong befor auth.
For example with an init with a wrong payload leading to a
4002 error code from the gateway. In general before the ready event.
2024-11-28 20:13:57 +01:00
4f1292377e bot: Fix execution in new thread 2024-11-28 20:09:30 +01:00
4 changed files with 14 additions and 44 deletions

View file

@ -29,45 +29,18 @@ docker compose build
### Configure environment
```shell
cp .env.example .env
cp example.properties dchat.properties
```
Fill the required variables.
### 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
docker compose up -d
```
### 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 :)
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`
## LICENSE

View file

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

View file

@ -10,18 +10,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
public final class MessageService {
private static final Logger logger = LoggerFactory.getLogger(MessageService.class);
private final ExecutorService executorService;
private final DiscordRest discordRest;
private final ChatGPTService chatGPTService;
private final String systemMessage;
public MessageService(final ExecutorService executorService, final DiscordRest discordRest, final ChatGPTService chatGPTService, String systemMessage) {
this.executorService = executorService;
public MessageService(final DiscordRest discordRest, final ChatGPTService chatGPTService, String systemMessage) {
this.discordRest = discordRest;
this.chatGPTService = chatGPTService;
this.systemMessage = systemMessage;
@ -46,12 +42,7 @@ public final class MessageService {
throw new RuntimeException(e);
}
};
// 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);
}
logger.debug("Handling message in new process: {}", originalMessageId);
Thread.startVirtualThread(callable).start();
}
}

View file

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