Skip to content

Commit

Permalink
Use Dispatchers.IO for network operations in generateImage function
Browse files Browse the repository at this point in the history
  • Loading branch information
WinG4merBR committed Jan 7, 2025
1 parent 53eb263 commit a3a54ff
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,9 +17,11 @@ class GirlfriendMemeExecutor : FoxyCommandExecutor() {
context.defer()
val user = context.getOption<User>("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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,9 +16,11 @@ class LaranjoExecutor : FoxyCommandExecutor() {
context.defer()
val text = context.getOption<String>("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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,9 +16,11 @@ class NotStonksExecutor : FoxyCommandExecutor() {
context.defer()
val text = context.getOption<String>("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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,9 +16,11 @@ class StonksExecutor : FoxyCommandExecutor() {
context.defer()
val text = context.getOption<String>("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}")
Expand Down

0 comments on commit a3a54ff

Please # to comment.