Skip to content

Commit da6b0b0

Browse files
committed
Add fields to channel position modify
discord/discord-api-docs#1776
1 parent 0054154 commit da6b0b0

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

core/src/test/kotlin/com/gitlab/kordlib/core/rest/RestTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,20 @@ class RestServiceTest {
433433
assertEquals("TEST", message.content)
434434
}
435435

436+
@Test
437+
@Order(25)
438+
@OptIn(KordExperimental::class)
439+
fun `channel moves in guild`(): Unit = runBlocking {
440+
val category = guild.createCategory("move category")
441+
val textChannel = guild.createTextChannel("move me to a category")
442+
443+
guild.swapChannelPositions {
444+
move(textChannel.id){ parentId = category.id }
445+
}
446+
447+
val currentTextChannel = guild.getChannelOf<TextChannel>(textChannel.id)
448+
assertEquals(category.id,currentTextChannel.categoryId)
449+
}
436450

437451
@Test
438452
@Order(Int.MAX_VALUE - 2)

rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/channel/GuildChannelPositionModifyBuilder.kt

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,64 @@ package com.gitlab.kordlib.rest.builder.channel
22

33
import com.gitlab.kordlib.rest.builder.AuditRequestBuilder
44
import com.gitlab.kordlib.common.annotation.KordDsl
5+
import com.gitlab.kordlib.common.annotation.KordExperimental
56
import com.gitlab.kordlib.common.entity.Snowflake
7+
import com.gitlab.kordlib.rest.json.request.ChannelPositionSwapRequest
68
import com.gitlab.kordlib.rest.json.request.GuildChannelPositionModifyRequest
79

810
@KordDsl
911
class GuildChannelPositionModifyBuilder: AuditRequestBuilder<GuildChannelPositionModifyRequest> {
1012
override var reason: String? = null
11-
private val swaps: MutableList<Pair<Snowflake, Int>> = mutableListOf()
13+
var swaps: MutableList<GuildChannelSwapBuilder> = mutableListOf()
1214

1315
fun move(pair: Pair<Snowflake, Int>) {
14-
swaps += pair.first to pair.second
16+
move(pair.first) { position = pair.second }
1517
}
1618

1719
fun move(vararg pairs: Pair<Snowflake, Int>) {
18-
swaps += pairs.map { it.first to it.second }
20+
pairs.forEach { move(it) }
21+
}
22+
23+
inline fun move(channel: Snowflake, builder: GuildChannelSwapBuilder.() -> Unit){
24+
swaps.firstOrNull { it.channelId == channel }?.builder() ?: run {
25+
swaps.add(GuildChannelSwapBuilder(channel).also(builder))
26+
}
1927
}
2028

2129
override fun toRequest(): GuildChannelPositionModifyRequest =
22-
GuildChannelPositionModifyRequest(swaps)
23-
}
30+
GuildChannelPositionModifyRequest(swaps.map { it.toRequest() })
31+
}
32+
33+
34+
class GuildChannelSwapBuilder(var channelId: Snowflake) {
35+
36+
/**
37+
* The new position of this channel
38+
*/
39+
var position: Int? = null
40+
41+
/**
42+
* The new parent of this channel, has to be a category.
43+
*
44+
* **Only one channel can have a parent id modified per request**.
45+
*
46+
* This field is not officially supported by the Discord API, and might change/be removed in the future.
47+
*/
48+
@KordExperimental
49+
var parentId: Snowflake? = null
50+
51+
/**
52+
* Locks the permissions of this channel to the new category it is moved to.
53+
* Only works if [parentId] is set.
54+
*
55+
* This field is not officially supported by the Discord API, and might change/be removed in the future.
56+
*/
57+
@KordExperimental
58+
var lockPermissionsToParent: Boolean? = null
59+
60+
@OptIn(KordExperimental::class)
61+
fun toRequest(): ChannelPositionSwapRequest = ChannelPositionSwapRequest(
62+
channelId, position, lockPermissionsToParent, parentId
63+
)
64+
65+
}

rest/src/main/kotlin/com/gitlab/kordlib/rest/json/request/GuildRequests.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.gitlab.kordlib.rest.json.request
22

33
import com.gitlab.kordlib.common.Color
44
import com.gitlab.kordlib.common.annotation.DeprecatedSinceKord
5+
import com.gitlab.kordlib.common.annotation.KordExperimental
56
import com.gitlab.kordlib.common.entity.*
67
import com.gitlab.kordlib.common.entity.optional.Optional
78
import com.gitlab.kordlib.common.entity.optional.OptionalBoolean
@@ -52,29 +53,38 @@ data class GuildChannelCreateRequest(
5253
val id: OptionalSnowflake = OptionalSnowflake.Missing,
5354
)
5455

55-
@Serializable(with = GuildChannelPositionModifyRequest.Serializer::class)
56-
data class GuildChannelPositionModifyRequest(val swaps: List<Pair<Snowflake, Int>>) {
56+
@Serializable
57+
data class ChannelPositionSwapRequest(
58+
val id: Snowflake,
59+
val position: Int?,
60+
@KordExperimental
61+
@SerialName("lock_permissions")
62+
val lockPermissions: Boolean?,
63+
@KordExperimental
64+
@SerialName("parent_id")
65+
val parentId: Snowflake?
66+
)
5767

68+
@Serializable(with = GuildChannelPositionModifyRequest.Serializer::class)
69+
data class GuildChannelPositionModifyRequest(
70+
val swaps: List<ChannelPositionSwapRequest>
71+
) {
5872
internal object Serializer : KSerializer<GuildChannelPositionModifyRequest> {
73+
private val delegate = ListSerializer(ChannelPositionSwapRequest.serializer())
5974

6075
@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
6176
override val descriptor: SerialDescriptor
62-
get() = listSerialDescriptor(ChannelPosition.serializer().descriptor)
77+
get() = listSerialDescriptor(ChannelPositionSwapRequest.serializer().descriptor)
6378

6479
override fun serialize(encoder: Encoder, value: GuildChannelPositionModifyRequest) {
65-
val positions = value.swaps.map { ChannelPosition(it.first, it.second) }
66-
ListSerializer(ChannelPosition.serializer()).serialize(encoder, positions)
80+
delegate.serialize(encoder, value.swaps)
6781
}
6882

6983
override fun deserialize(decoder: Decoder): GuildChannelPositionModifyRequest {
70-
val values = decoder.decodeSerializableValue(ListSerializer(ChannelPosition.serializer()))
71-
return GuildChannelPositionModifyRequest(values.map { it.id to it.position })
84+
return GuildChannelPositionModifyRequest(decoder.decodeSerializableValue(delegate))
7285
}
7386

7487
}
75-
76-
@Serializable
77-
private data class ChannelPosition(val id: Snowflake, val position: Int)
7888
}
7989

8090
@Serializable

0 commit comments

Comments
 (0)