Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Go to [`https://duncte.bot/invite`][inviteLink] and follow the steps on the webs
While self-hosting is very much possible with Docker, please note that no support will be provided with self-hosting.
The code of this bot was made public for people who want to see how the bot works or want to contribute to it.

# Used external libraries
Skybot relies on [this lib](https://github.com/sot-tech/LottieConverter/releases/tag/r0.2) for lottie conversion.

# Support
[![Support Server](https://discord.com/api/guilds/191245668617158656/embed.png?style=banner2)](https://duncte.bot/server)
Expand Down
1 change: 1 addition & 0 deletions assets
2 changes: 2 additions & 0 deletions bot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ FROM azul/zulu-openjdk-alpine:19-jre
# also add some fonts
RUN apk add --no-cache libstdc++ fontconfig font-noto

COPY ./assets/lottieconverter_musl /skybot/assets/lottieconverter

WORKDIR /skybot
COPY --from=builder /skybot/bot/build/libs/bot*-prod.jar ./skybot.jar

Expand Down
Binary file added bot/assets/lottieconverter
Binary file not shown.
Binary file added bot/assets/lottieconverter_musl
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import net.dv8tion.jda.api.utils.FileUpload;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg;
Expand All @@ -59,12 +62,22 @@ public void execute(@NotNull CommandContext ctx) {
final StickerItem sticker = stickers.get(0);
final StickerFormat formatType = sticker.getFormatType();

if (formatType == StickerFormat.UNKNOWN || formatType == StickerFormat.LOTTIE) {
sendMsg(ctx, "The sticker supplied could not be rendered");
return;
switch (formatType) {
case GIF:
this.uploadFile(sticker.getIconUrl(), ctx);
break;
case PNG:
case APNG:
this.uploadFile(sticker.getIconUrl().replace("apng", "png"), ctx);
break;
case LOTTIE:
this.sendLottieGif(sticker.getIconUrl(), sticker.getName(), ctx);
break;
default:
sendMsg(ctx, "The sticker supplied could not be rendered");
break;
}

this.uploadFile(sticker.getIconUrl().replace("apng", "png"), ctx);
return;
}

Expand Down Expand Up @@ -114,6 +127,54 @@ public void execute(@NotNull CommandContext ctx) {
this.uploadFile(emojiUrl, ctx);
}

// TODO: use virtual threads
private void sendLottieGif(final String lottieUrl, final String stickerName, final CommandContext ctx) {
WebUtils.ins.getText(lottieUrl, (it) -> it.setRateLimiter(RateLimiter.directLimiter())).async(
(lottieJson) -> {
try {
final var bytes = this.renderLottie(lottieJson);

ctx.getChannel()
.sendFiles(
FileUpload.fromData(bytes, "%s.gif".formatted(stickerName))
)
.setMessageReference(ctx.getMessage())
.queue();
} catch (final IOException e) {
LOGGER.error("Failed to render lottie", e);
sendMsg(ctx, "Failed to render lottie: " + e.getMessage());
}
},
(error) -> {
Sentry.captureException(error);
sendMsg(ctx, "Failed to render lottie: " + error.getMessage());
}
);
}

private byte[] renderLottie(final String lottieJson) throws IOException {
final var tmpFile = File.createTempFile("lottie-json", ".json");

try (var writer = Files.newBufferedWriter(tmpFile.toPath(), StandardCharsets.UTF_8)) {
writer.write(lottieJson);
}

final var programFile = new File("assets/lottieconverter");
final var process = new ProcessBuilder()
.command(
programFile.getAbsolutePath(),
"-", "-", "gif", "512x512", "60"
)
.redirectInput(ProcessBuilder.Redirect.from(tmpFile))
.start();

try (var stream = process.getInputStream()) {
return stream.readAllBytes();
} finally {
tmpFile.delete();
}
}

private void uploadFile(final String url, final CommandContext ctx) {
WebUtils.ins.getByteStream(url, (it) -> it.setRateLimiter(RateLimiter.directLimiter())).async(
(bytes) -> {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ allprojects {
}

tasks.withType<Wrapper> {
gradleVersion = "8.2.1"
gradleVersion = "8.5"
distributionType = Wrapper.DistributionType.BIN
}
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:

# Options are: psql, web
- USE_DATABASE=psql
- JDBC_URI=jdbc:postgresql://localhost:5432/skybot?user=root&password=root
- JDBC_URI=jdbc:postgresql://localhost:5433/skybot?user=root&password=postgres
# - REDIS_HOST=localhost
- REDIS_HOST=disabled
dashboard:
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
16 changes: 10 additions & 6 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class PostgreDatabase(jdbcURI: String, ohShitFn: (Int, Int) -> Unit = { _, _ ->
}

init {
// Needed for docker it seems
Class.forName("org.postgresql.ds.PGSimpleDataSource")

val config = HikariConfig()

// IT IS postgresql:// NOT psql://
Expand Down