Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple files support #247

Merged
merged 6 commits into from
Apr 18, 2021
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
33 changes: 22 additions & 11 deletions rest/src/main/kotlin/builder/interaction/FollowupMessageBuilders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import dev.kord.common.entity.AllowedMentions
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.mapList
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.message.AllowedMentionsBuilder
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.json.request.EmbedRequest
import dev.kord.rest.json.request.FollowupMessageCreateRequest
import dev.kord.rest.json.request.FollowupMessageModifyRequest
import dev.kord.rest.json.request.MultipartFollowupMessageCreateRequest
import dev.kord.rest.json.request.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStream
Expand All @@ -27,13 +23,16 @@ import kotlin.contracts.contract
@KordPreview
@KordDsl
class PublicFollowupMessageModifyBuilder :
RequestBuilder<FollowupMessageModifyRequest> {
RequestBuilder<MultipartFollowupMessageModifyRequest> {
private var _content: Optional<String> = Optional.Missing()
var content: String? by ::_content.delegate()

private var _embeds: Optional<MutableList<EmbedBuilder>> = Optional.Missing()
var embeds: MutableList<EmbedBuilder>? by ::_embeds.delegate()

val files: MutableList<Pair<String, InputStream>> = mutableListOf()


private var _allowedMentions: Optional<AllowedMentions> = Optional.Missing()
var allowedMentions: AllowedMentions? by ::_allowedMentions.delegate()

Expand All @@ -44,18 +43,30 @@ class PublicFollowupMessageModifyBuilder :
embeds!! += EmbedBuilder().apply(builder)
}


fun addFile(name: String, content: InputStream) {
files += name to content
}

suspend fun addFile(path: Path) = withContext(Dispatchers.IO) {
addFile(path.fileName.toString(), Files.newInputStream(path))
}

@OptIn(ExperimentalContracts::class)
inline fun allowedMentions(builder: AllowedMentionsBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
allowedMentions = AllowedMentionsBuilder().apply(builder).build()
}


override fun toRequest(): FollowupMessageModifyRequest {
return FollowupMessageModifyRequest(
_content,
_embeds.mapList { it.toRequest() },
_allowedMentions
override fun toRequest(): MultipartFollowupMessageModifyRequest {
return MultipartFollowupMessageModifyRequest(
FollowupMessageModifyRequest(
_content,
_embeds.mapList { it.toRequest() },
_allowedMentions
),
files
)
}
}
Expand Down
26 changes: 23 additions & 3 deletions rest/src/main/kotlin/builder/webhook/EditWebhookMessageBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.json.request.MultipartWebhookEditMessageRequest
import dev.kord.rest.json.request.WebhookEditMessageRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

class EditWebhookMessageBuilder : RequestBuilder<WebhookEditMessageRequest> {
class EditWebhookMessageBuilder : RequestBuilder<MultipartWebhookEditMessageRequest> {

private var _content: Optional<String> = Optional.Missing()
var content: String? by ::_content.delegate()

var embeds: MutableList<EmbedBuilder> = mutableListOf()

val files: MutableList<Pair<String, InputStream>> = mutableListOf()

private var _allowedMentions: Optional<AllowedMentions> = Optional.Missing()
var allowedMentions: AllowedMentions? by ::_allowedMentions.delegate()

Expand All @@ -28,7 +36,19 @@ class EditWebhookMessageBuilder : RequestBuilder<WebhookEditMessageRequest> {
embeds.add(EmbedBuilder().apply(builder))
}

override fun toRequest(): WebhookEditMessageRequest = WebhookEditMessageRequest(
_content, Optional.missingOnEmpty(embeds.map(EmbedBuilder::toRequest)), _allowedMentions
fun addFile(name: String, content: InputStream) {
files += name to content
}

suspend fun addFile(path: Path) = withContext(Dispatchers.IO) {
addFile(path.fileName.toString(), Files.newInputStream(path))
}


override fun toRequest(): MultipartWebhookEditMessageRequest = MultipartWebhookEditMessageRequest(
WebhookEditMessageRequest(
_content, Optional.missingOnEmpty(embeds.map(EmbedBuilder::toRequest)), _allowedMentions
),
files
)
}
18 changes: 10 additions & 8 deletions rest/src/main/kotlin/builder/webhook/ExecuteWebhookBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import dev.kord.rest.json.request.MultiPartWebhookExecuteRequest
import dev.kord.rest.json.request.WebhookExecuteRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@KordDsl
class ExecuteWebhookBuilder : RequestBuilder<MultiPartWebhookExecuteRequest> {
class ExecuteWebhookBuilder: RequestBuilder<MultiPartWebhookExecuteRequest> {

private var _content: Optional<String> = Optional.Missing()
var content: String? by ::_content.delegate()
Expand All @@ -32,15 +33,16 @@ class ExecuteWebhookBuilder : RequestBuilder<MultiPartWebhookExecuteRequest> {
private var _tts: OptionalBoolean = OptionalBoolean.Missing
var tts: Boolean? by ::_tts.delegate()

private var file: Pair<String, java.io.InputStream>? = null
val files: MutableList<Pair<String, InputStream>> = mutableListOf()

var embeds: MutableList<EmbedRequest> = mutableListOf()

fun setFile(name: String, content: java.io.InputStream) {
file = name to content
fun addFile(name: String, content: InputStream) {
files += name to content
}

suspend fun setFile(path: Path) = withContext(Dispatchers.IO) {
setFile(path.fileName.toString(), Files.newInputStream(path))
suspend fun addFile(path: Path) = withContext(Dispatchers.IO) {
addFile(path.fileName.toString(), Files.newInputStream(path))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}


@OptIn(ExperimentalContracts::class)
Expand All @@ -51,8 +53,8 @@ class ExecuteWebhookBuilder : RequestBuilder<MultiPartWebhookExecuteRequest> {
embeds.add(EmbedBuilder().apply(builder).toRequest())
}

override fun toRequest(): MultiPartWebhookExecuteRequest = MultiPartWebhookExecuteRequest(
WebhookExecuteRequest(_content, _username, _avatarUrl, _tts, Optional.missingOnEmpty(embeds)), file
override fun toRequest() : MultiPartWebhookExecuteRequest = MultiPartWebhookExecuteRequest(
WebhookExecuteRequest(_content, _username, _avatarUrl, _tts, Optional.missingOnEmpty(embeds)), files
)

}
6 changes: 6 additions & 0 deletions rest/src/main/kotlin/json/request/InteractionsRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ data class FollowupMessageModifyRequest(
val embeds: Optional<List<EmbedRequest>> = Optional.Missing(),
@SerialName("allowed_mentions")
val allowedMentions: Optional<AllowedMentions> = Optional.Missing(),
)

@KordPreview
data class MultipartFollowupMessageModifyRequest(
val request: FollowupMessageModifyRequest,
val files: List<Pair<String, java.io.InputStream>> = emptyList(),
)
9 changes: 7 additions & 2 deletions rest/src/main/kotlin/json/request/WebhookRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ data class WebhookExecuteRequest(
)

data class MultiPartWebhookExecuteRequest(
val request: WebhookExecuteRequest,
val file: Pair<String, java.io.InputStream>?
val request: WebhookExecuteRequest,
val files: List<Pair<String, java.io.InputStream>> = emptyList()
)

@Serializable
Expand All @@ -40,3 +40,8 @@ data class WebhookEditMessageRequest(
val embeds: Optional<List<EmbedRequest>> = Optional.Missing(),
val allowedMentions: Optional<AllowedMentions> = Optional.Missing()
)

data class MultipartWebhookEditMessageRequest(
val request: WebhookEditMessageRequest,
val files: List<Pair<String, java.io.InputStream>> = emptyList()
)
14 changes: 14 additions & 0 deletions rest/src/main/kotlin/service/InteractionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ class InteractionService(requestHandler: RequestHandler) : RestService(requestHa
keys[Route.MessageId] = messageId
}

suspend fun modifyFollowupMessage(
applicationId: Snowflake,
interactionToken: String,
messageId: Snowflake,
request: MultipartFollowupMessageModifyRequest
) = call(Route.FollowupMessageModify) {
keys[Route.ApplicationId] = applicationId
keys[Route.InteractionToken] = interactionToken
keys[Route.MessageId] = messageId
body(FollowupMessageModifyRequest.serializer(), request.request)
request.files.forEach { file(it) }
}


suspend fun modifyFollowupMessage(
applicationId: Snowflake,
interactionToken: String,
Expand Down
5 changes: 3 additions & 2 deletions rest/src/main/kotlin/service/WebhookService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class WebhookService(requestHandler: RequestHandler) : RestService(requestHandle
parameter("wait", "$wait")
val request = ExecuteWebhookBuilder().apply(builder).toRequest()
body(WebhookExecuteRequest.serializer(), request.request)
request.file?.let { file(it) }
request.files.forEach { file(it) }
}
}

Expand Down Expand Up @@ -157,7 +157,8 @@ class WebhookService(requestHandler: RequestHandler) : RestService(requestHandle
keys[Route.WebhookToken] = token
keys[Route.MessageId] = messageId
val body = EditWebhookMessageBuilder().apply(builder).toRequest()
body(WebhookEditMessageRequest.serializer(), body)
body(WebhookEditMessageRequest.serializer(), body.request)
body.files.onEach { file(it) }
}
}
}