K
Kord3y ago
Marc

Sending a message with an image attachment

I am trying to send a message with an image attachment as a command reply. This is the relevant code:
val image = interaction.command.attachments["image"]!!

val uploadedImageChannel = httpClient.get(image.url).bodyAsChannel()

val response = interaction.channel.createMessage {
files += NamedFile(image.filename, ChannelProvider(size = image.size.toLong()) { uploadedImageChannel })
}
val image = interaction.command.attachments["image"]!!

val uploadedImageChannel = httpClient.get(image.url).bodyAsChannel()

val response = interaction.channel.createMessage {
files += NamedFile(image.filename, ChannelProvider(size = image.size.toLong()) { uploadedImageChannel })
}
This used to work fine, but now I get this error:
2023-04-09T00:41:53.861+0000 [DefaultDispatcher-worker-3] ERROR dev.kord.core.Kord - catching(dev.kord.rest.request.KtorRequestException: REST request returned an error: 400 Bad Request Cannot send an empty message null)
dev.kord.rest.request.KtorRequestException: REST request returned an error: 400 Bad Request Cannot send an empty message null
at dev.kord.rest.request.KtorRequestHandler.handle(KtorRequestHandler.kt:61)
at dev.kord.rest.request.KtorRequestHandler$handle$1.invokeSuspend(KtorRequestHandler.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
2023-04-09T00:41:53.861+0000 [DefaultDispatcher-worker-3] ERROR dev.kord.core.Kord - catching(dev.kord.rest.request.KtorRequestException: REST request returned an error: 400 Bad Request Cannot send an empty message null)
dev.kord.rest.request.KtorRequestException: REST request returned an error: 400 Bad Request Cannot send an empty message null
at dev.kord.rest.request.KtorRequestHandler.handle(KtorRequestHandler.kt:61)
at dev.kord.rest.request.KtorRequestHandler$handle$1.invokeSuspend(KtorRequestHandler.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
I tried looking into that spot and it seems like the Discord API just directly returns the error about the empty message with an error code of 50006. The image I am appending is definitely valid - if I output it to my PC instead of attaching it, it shows no signs of corruption.
5 Replies
LustigerLurch
LustigerLurch3y ago
could you enable trace logging for more info? also what kord version are you using? but if this suddenly stopped to work without any change in your code/kord version, i have an idea where it could come from
LustigerLurch
LustigerLurch3y ago
https://github.com/kordlib/kord/blob/0.9.x/rest/src/commonMain/kotlin/request/Request.kt#L88 - here we don't seem to properly match discord's docs: https://discord.com/developers/docs/reference#uploading-files "file$index" might have to be replaced with "file[$index]" and "filename=$fileName" with "filename=\"$fileName\""
GitHub
kord/Request.kt at 0.9.x · kordlib/kord
Idiomatic Kotlin Wrapper for The Discord API. Contribute to kordlib/kord development by creating an account on GitHub.
Discord Developer Portal
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.
LustigerLurch
LustigerLurch3y ago
so maybe some lenient internal thing at discord's site was made more strict so this now fails however just speculations and thoughts at this point, need to try to repro and test my theory, but don't have time rn hm, i can't reproduce this, the code i used:
suspend fun main() {
val kord = Kord(token)

kord.createGlobalApplicationCommands {
input("attachment-test", "test attachment") {
attachment("image", "description") {
required = true
}
}
}

kord.on<ChatInputCommandInteractionCreateEvent> {
interaction.respondEphemeral { content = "received image" }

val image = interaction.command.attachments["image"]!!
val imageChannel = kord.resources.httpClient.get(image.url).bodyAsChannel()

interaction.channel.createMessage {
addFile(image.filename, ChannelProvider { imageChannel })
}
}

kord.login()
}
suspend fun main() {
val kord = Kord(token)

kord.createGlobalApplicationCommands {
input("attachment-test", "test attachment") {
attachment("image", "description") {
required = true
}
}
}

kord.on<ChatInputCommandInteractionCreateEvent> {
interaction.respondEphemeral { content = "received image" }

val image = interaction.command.attachments["image"]!!
val imageChannel = kord.resources.httpClient.get(image.url).bodyAsChannel()

interaction.channel.createMessage {
addFile(image.filename, ChannelProvider { imageChannel })
}
}

kord.login()
}
does this code work for you @Marc?
Marc
MarcOP3y ago
Sorry for the late reply, but it does work. It seems like the issue was the size parameter in the ChannelProvider. If I just remove it, it works again. How weird... Thanks anyways.
LustigerLurch
LustigerLurch3y ago
hm, alright good to know that it works now :)

Did you find this page helpful?