Compare commits
3 commits
654aca86cc
...
d614d99642
Author | SHA1 | Date | |
---|---|---|---|
d614d99642 | |||
f2e5b04c1c | |||
4f1292377e |
4 changed files with 14 additions and 44 deletions
31
README.md
31
README.md
|
@ -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
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue