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
|
### Configure environment
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
cp .env.example .env
|
cp example.properties dchat.properties
|
||||||
```
|
```
|
||||||
|
|
||||||
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
|
||||||
```
|
```
|
||||||
|
|
||||||
### Invite
|
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 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
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ 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);
|
||||||
|
@ -45,10 +44,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(); final var executorService = Executors.newVirtualThreadPerTaskExecutor()) {
|
try (final var httpClient = HttpClient.newHttpClient()) {
|
||||||
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(executorService, discordRest, chatGPTService, systemMessage);
|
final var messageService = new MessageService(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();
|
||||||
|
|
|
@ -10,18 +10,14 @@ 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 ExecutorService executorService, final DiscordRest discordRest, final ChatGPTService chatGPTService, String systemMessage) {
|
public MessageService(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;
|
||||||
|
@ -46,12 +42,7 @@ public final class MessageService {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
logger.debug("Handling message in new process: {}", originalMessageId);
|
||||||
// FIXME: We should find a solution to not block the main thread without loosing the started thread.
|
Thread.startVirtualThread(callable).start();
|
||||||
try {
|
|
||||||
executorService.submit(callable).get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,14 @@ 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;
|
||||||
|
@ -32,6 +35,10 @@ 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(
|
||||||
|
|
Loading…
Reference in a new issue