From a3a54ff5a2716de6cadc4448e3234332be423200 Mon Sep 17 00:00:00 2001 From: WinG4merBR <68250074+WinG4merBR@users.noreply.github.com> Date: Tue, 7 Jan 2025 00:05:00 -0300 Subject: [PATCH] Use Dispatchers.IO for network operations in generateImage function --- .../media/AntesQueVireModaExecutor.kt | 10 +++++++--- .../vanilla/entertainment/media/EminemExecutor.kt | 14 +++++++++----- .../vanilla/entertainment/media/ErrorExecutor.kt | 10 +++++++--- .../entertainment/media/GirlfriendMemeExecutor.kt | 10 +++++++--- .../entertainment/media/GostosIguaisExecutor.kt | 14 +++++++++----- .../vanilla/entertainment/media/LaranjoExecutor.kt | 10 +++++++--- .../entertainment/media/NotStonksExecutor.kt | 10 +++++++--- .../vanilla/entertainment/media/StonksExecutor.kt | 10 +++++++--- 8 files changed, 60 insertions(+), 28 deletions(-) diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/AntesQueVireModaExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/AntesQueVireModaExecutor.kt index d07b8268..88347ece 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/AntesQueVireModaExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/AntesQueVireModaExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -59,9 +61,11 @@ class AntesQueVireModaExecutor: FoxyCommandExecutor() { return } - val response = context.foxy.artistryClient.generateImage("memes/moda", buildJsonObject { - put("asset", attachment.url) - }) + val response = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/moda", buildJsonObject { + put("asset", attachment.url) + }) + } if (response.status.value in 400..499) { context.reply { diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/EminemExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/EminemExecutor.kt index df8cc96d..c2b96e3a 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/EminemExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/EminemExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -56,11 +58,13 @@ class EminemExecutor: FoxyCommandExecutor() { } - val response = context.foxy.artistryClient.generateImage("memes/8mile", buildJsonObject { - put("url", attachment.url) - put("contentType", attachment.contentType) - put("size", attachment.size) - }) + val response = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/8mile", buildJsonObject { + put("url", attachment.url) + put("contentType", attachment.contentType) + put("size", attachment.size) + }) + } if (response.status.value in 400..499) { context.reply { diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/ErrorExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/ErrorExecutor.kt index 8111d625..d8759d8a 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/ErrorExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/ErrorExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -26,9 +28,11 @@ class ErrorExecutor : FoxyCommandExecutor() { } } - val errorImage = context.foxy.artistryClient.generateImage("memes/windowserror", buildJsonObject { - put("text", text) - }) + val errorImage = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/windowserror", buildJsonObject { + put("text", text) + }) + } if (errorImage.status.value !in 200..299) { throw IllegalArgumentException("Error while generating image! Received ${errorImage.status}") diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GirlfriendMemeExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GirlfriendMemeExecutor.kt index 4f5ddc06..2436d61f 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GirlfriendMemeExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GirlfriendMemeExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -15,9 +17,11 @@ class GirlfriendMemeExecutor : FoxyCommandExecutor() { context.defer() val user = context.getOption("user") - val girlfriendImageBuffer = context.foxy.artistryClient.generateImage("memes/girlfriend", buildJsonObject { - put("avatar", user?.avatarUrl) - }) + val girlfriendImageBuffer = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/girlfriend", buildJsonObject { + put("avatar", user?.avatarUrl) + }) + } if (girlfriendImageBuffer.status.value !in 200..299) { throw IllegalArgumentException("Error while generating image! Received ${girlfriendImageBuffer.status}") diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GostosIguaisExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GostosIguaisExecutor.kt index 47b17e0b..3d2a3a27 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GostosIguaisExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/GostosIguaisExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -30,11 +32,13 @@ class GostosIguaisExecutor : FoxyCommandExecutor() { return replyFileTooBig(context) } - val image = context.foxy.artistryClient.generateImage("memes/gosto", buildJsonObject { - put("asset1", attachment1.url) - put("asset2", attachment2.url) - put("text", text) - }) + val image = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/gosto", buildJsonObject { + put("asset1", attachment1.url) + put("asset2", attachment2.url) + put("text", text) + }) + } if (image.status.value !in 200..299) { throw IllegalArgumentException("Error while generating image! Received ${image.status}") diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/LaranjoExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/LaranjoExecutor.kt index 16ca4427..3fca01a2 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/LaranjoExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/LaranjoExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -14,9 +16,11 @@ class LaranjoExecutor : FoxyCommandExecutor() { context.defer() val text = context.getOption("text")!! - val laranjoImage = context.foxy.artistryClient.generateImage("memes/laranjo", buildJsonObject { - put("text", text) - }) + val laranjoImage = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/laranjo", buildJsonObject { + put("text", text) + }) + } if (laranjoImage.status.value !in 200..299) { throw IllegalArgumentException("Error while generating image! Received ${laranjoImage.status}") diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/NotStonksExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/NotStonksExecutor.kt index aab31c98..6e5a8450 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/NotStonksExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/NotStonksExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -14,9 +16,11 @@ class NotStonksExecutor : FoxyCommandExecutor() { context.defer() val text = context.getOption("text")!! - val notStonksImage = context.foxy.artistryClient.generateImage("memes/notstonks", buildJsonObject { - put("text", text) - }) + val notStonksImage = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/notstonks", buildJsonObject { + put("text", text) + }) + } if (notStonksImage.status.value !in 200..299) { throw IllegalArgumentException("Error while generating image! Received ${notStonksImage.status}") diff --git a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/StonksExecutor.kt b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/StonksExecutor.kt index 01a3c40f..6060a86e 100644 --- a/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/StonksExecutor.kt +++ b/foxy/src/main/kotlin/net/cakeyfox/foxy/command/vanilla/entertainment/media/StonksExecutor.kt @@ -2,6 +2,8 @@ package net.cakeyfox.foxy.command.vanilla.entertainment.media import io.ktor.client.call.* import io.ktor.utils.io.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import kotlinx.io.readByteArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put @@ -14,9 +16,11 @@ class StonksExecutor : FoxyCommandExecutor() { context.defer() val text = context.getOption("text")!! - val stonksImage = context.foxy.artistryClient.generateImage("memes/stonks", buildJsonObject { - put("text", text) - }) + val stonksImage = withContext(Dispatchers.IO) { + context.foxy.artistryClient.generateImage("memes/stonks", buildJsonObject { + put("text", text) + }) + } if (stonksImage.status.value !in 200..299) { throw IllegalArgumentException("Error while generating image! Received ${stonksImage.status}")