Skip to content

Commit 2a306cd

Browse files
committed
Add PollBehavior
1 parent 65a578f commit 2a306cd

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

core/api/core.api

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,37 @@ public final class dev/kord/core/behavior/MessageCommandBehavior$DefaultImpls {
641641
public static fun compareTo (Ldev/kord/core/behavior/MessageCommandBehavior;Ldev/kord/core/entity/Entity;)I
642642
}
643643

644+
public abstract interface class dev/kord/core/behavior/PollBehavior : dev/kord/core/behavior/MessageBehavior {
645+
public abstract fun end (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
646+
public abstract fun getAnswerVoters (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
647+
}
648+
649+
public final class dev/kord/core/behavior/PollBehavior$DefaultImpls {
650+
public static fun addReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/GuildEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
651+
public static fun addReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
652+
public static fun asMessage (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
653+
public static fun asMessageOrNull (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
654+
public static fun compareTo (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/Entity;)I
655+
public static fun delete (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/Snowflake;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
656+
public static fun delete (Ldev/kord/core/behavior/PollBehavior;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
657+
public static fun deleteAllReactions (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
658+
public static fun deleteOwnReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
659+
public static fun deleteReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/common/entity/Snowflake;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
660+
public static fun deleteReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
661+
public static fun end (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
662+
public static fun fetchMessage (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
663+
public static fun fetchMessageOrNull (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
664+
public static fun getAnswerVoters (Ldev/kord/core/behavior/PollBehavior;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
665+
public static fun getChannel (Ldev/kord/core/behavior/PollBehavior;)Ldev/kord/core/behavior/channel/MessageChannelBehavior;
666+
public static fun getChannel (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
667+
public static fun getChannelOrNull (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
668+
public static fun getReactors (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;)Lkotlinx/coroutines/flow/Flow;
669+
public static fun pin (Ldev/kord/core/behavior/PollBehavior;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
670+
public static fun publish (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
671+
public static fun unpin (Ldev/kord/core/behavior/PollBehavior;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
672+
public static fun withStrategy (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/behavior/MessageBehavior;
673+
}
674+
644675
public abstract interface class dev/kord/core/behavior/RoleBehavior : dev/kord/core/entity/KordEntity, dev/kord/core/entity/Strategizable {
645676
public abstract fun asRole (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
646677
public abstract fun asRoleOrNull (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@@ -6922,7 +6953,7 @@ public final class dev/kord/core/entity/PermissionOverwriteEntity : dev/kord/cor
69226953
public synthetic fun withStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/entity/Strategizable;
69236954
}
69246955

6925-
public final class dev/kord/core/entity/Poll : dev/kord/core/entity/Message {
6956+
public final class dev/kord/core/entity/Poll : dev/kord/core/behavior/PollBehavior, dev/kord/core/entity/Message {
69266957
public fun <init> (Ldev/kord/core/cache/data/MessageData;Ldev/kord/core/Kord;Ldev/kord/core/supplier/EntitySupplier;)V
69276958
public synthetic fun <init> (Ldev/kord/core/cache/data/MessageData;Ldev/kord/core/Kord;Ldev/kord/core/supplier/EntitySupplier;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
69286959
public fun addReaction (Ldev/kord/core/entity/GuildEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@@ -6937,10 +6968,12 @@ public final class dev/kord/core/entity/Poll : dev/kord/core/entity/Message {
69376968
public fun deleteOwnReaction (Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
69386969
public fun deleteReaction (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
69396970
public fun deleteReaction (Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
6971+
public fun end (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
69406972
public fun equals (Ljava/lang/Object;)Z
69416973
public fun fetchMessage (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
69426974
public fun fetchMessageOrNull (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
69436975
public fun getActionRows ()Ljava/util/List;
6976+
public fun getAnswerVoters (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
69446977
public fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
69456978
public fun getAttachments ()Ljava/util/Set;
69466979
public fun getAuthor ()Ldev/kord/core/entity/User;

core/api/core.klib.api

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,11 @@ abstract interface dev.kord.core.behavior/MessageCommandBehavior : dev.kord.core
625625
abstract suspend fun edit(kotlin.coroutines/SuspendFunction1<dev.kord.rest.builder.interaction/MessageCommandModifyBuilder, kotlin/Unit>): dev.kord.core.entity.application/MessageCommand // dev.kord.core.behavior/MessageCommandBehavior.edit|edit(kotlin.coroutines.SuspendFunction1<dev.kord.rest.builder.interaction.MessageCommandModifyBuilder,kotlin.Unit>){}[0]
626626
}
627627

628+
abstract interface dev.kord.core.behavior/PollBehavior : dev.kord.core.behavior/MessageBehavior { // dev.kord.core.behavior/PollBehavior|null[0]
629+
open suspend fun end(): dev.kord.core.entity/Poll // dev.kord.core.behavior/PollBehavior.end|end(){}[0]
630+
open suspend fun getAnswerVoters(kotlin/Int): kotlin.collections/List<dev.kord.core.entity/User> // dev.kord.core.behavior/PollBehavior.getAnswerVoters|getAnswerVoters(kotlin.Int){}[0]
631+
}
632+
628633
abstract interface dev.kord.core.behavior/RoleBehavior : dev.kord.core.entity/KordEntity, dev.kord.core.entity/Strategizable { // dev.kord.core.behavior/RoleBehavior|null[0]
629634
abstract val guildId // dev.kord.core.behavior/RoleBehavior.guildId|{}guildId[0]
630635
abstract fun <get-guildId>(): dev.kord.common.entity/Snowflake // dev.kord.core.behavior/RoleBehavior.guildId.<get-guildId>|<get-guildId>(){}[0]
@@ -8081,7 +8086,7 @@ final class dev.kord.core.entity/PermissionOverwriteEntity : dev.kord.core.entit
80818086
final suspend fun getGuildOrNull(): dev.kord.core.entity/Guild? // dev.kord.core.entity/PermissionOverwriteEntity.getGuildOrNull|getGuildOrNull(){}[0]
80828087
}
80838088

8084-
final class dev.kord.core.entity/Poll : dev.kord.core.entity/Message { // dev.kord.core.entity/Poll|null[0]
8089+
final class dev.kord.core.entity/Poll : dev.kord.core.behavior/PollBehavior, dev.kord.core.entity/Message { // dev.kord.core.entity/Poll|null[0]
80858090
constructor <init>(dev.kord.core.cache.data/MessageData, dev.kord.core/Kord, dev.kord.core.supplier/EntitySupplier = ...) // dev.kord.core.entity/Poll.<init>|<init>(dev.kord.core.cache.data.MessageData;dev.kord.core.Kord;dev.kord.core.supplier.EntitySupplier){}[0]
80868091

80878092
final val data // dev.kord.core.entity/Poll.data|{}data[0]

core/src/commonMain/kotlin/behavior/MessageBehavior.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package dev.kord.core.behavior
22

3-
import dev.kord.common.entity.Permission
43
import dev.kord.common.entity.Snowflake
54
import dev.kord.common.exception.RequestException
65
import dev.kord.core.Kord
76
import dev.kord.core.behavior.channel.MessageChannelBehavior
87
import dev.kord.core.behavior.channel.createMessage
98
import dev.kord.core.cache.data.MessageData
9+
import dev.kord.core.cache.data.UserData
1010
import dev.kord.core.entity.*
1111
import dev.kord.core.entity.channel.MessageChannel
1212
import dev.kord.core.exception.EntityNotFoundException
13+
import dev.kord.core.hash
1314
import dev.kord.core.supplier.EntitySupplier
1415
import dev.kord.core.supplier.EntitySupplyStrategy
1516
import dev.kord.core.supplier.getChannelOf
@@ -20,7 +21,6 @@ import dev.kord.rest.builder.message.modify.WebhookMessageModifyBuilder
2021
import dev.kord.rest.request.RestRequestException
2122
import dev.kord.rest.service.RestClient
2223
import kotlinx.coroutines.flow.Flow
23-
import dev.kord.core.hash
2424
import kotlin.contracts.InvocationKind
2525
import kotlin.contracts.contract
2626

@@ -215,7 +215,32 @@ public interface MessageBehavior : KordEntity, Strategizable {
215215
override fun withStrategy(
216216
strategy: EntitySupplyStrategy<*>,
217217
): MessageBehavior = MessageBehavior(channelId, id, kord, strategy)
218+
}
219+
220+
/**
221+
* Behavior of a Poll message.
222+
*/
223+
public interface PollBehavior : MessageBehavior {
224+
225+
/**
226+
* Retrieves the voters who voted for the specified [answer][answerId].
227+
*/
228+
public suspend fun getAnswerVoters(answerId: Int): List<User> =
229+
kord.rest.channel.getPollUsersByAnswer(channelId, id, answerId).map {
230+
val data = UserData.from(it)
218231

232+
User(data, kord, supplier)
233+
}
234+
235+
/**
236+
* Ends this poll.
237+
*/
238+
public suspend fun end(): Poll {
239+
val message = kord.rest.channel.expirePoll(channelId, id)
240+
val data = MessageData.from(message)
241+
242+
return Message(data, kord, supplier) as Poll
243+
}
219244
}
220245

221246
public fun MessageBehavior(

core/src/commonMain/kotlin/entity/Message.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dev.kord.common.entity.optional.*
66
import dev.kord.common.exception.RequestException
77
import dev.kord.core.Kord
88
import dev.kord.core.behavior.MessageBehavior
9+
import dev.kord.core.behavior.PollBehavior
910
import dev.kord.core.behavior.UserBehavior
1011
import dev.kord.core.behavior.channel.ChannelBehavior
1112
import dev.kord.core.behavior.interaction.response.InteractionResponseBehavior
@@ -369,7 +370,7 @@ public class Poll(
369370
override val data: MessageData,
370371
override val kord: Kord,
371372
override val supplier: EntitySupplier = kord.defaultSupplier,
372-
) : Message {
373+
) : Message, PollBehavior {
373374
/**
374375
* The [poll][DiscordPoll].
375376
*/

core/src/jvmTest/kotlin/rest/RestTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.kord.core.rest
22

33
import dev.kord.common.Color
4+
import dev.kord.common.annotation.KordUnsafe
45
import dev.kord.common.entity.*
56
import dev.kord.core.Kord
67
import dev.kord.core.behavior.*
@@ -21,6 +22,7 @@ import kotlinx.coroutines.flow.first
2122
import kotlinx.coroutines.flow.firstOrNull
2223
import kotlinx.coroutines.flow.toList
2324
import kotlinx.coroutines.runBlocking
25+
import kotlinx.coroutines.test.runTest
2426
import org.junit.jupiter.api.*
2527
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
2628
import kotlin.io.path.toPath
@@ -535,6 +537,20 @@ class RestServiceTest {
535537

536538
}
537539

540+
@OptIn(KordUnsafe::class)
541+
@Test
542+
@Order(29)
543+
fun `poll in channel`() = runTest {
544+
val poll = channel.createPoll {
545+
question("Is Kord great?")
546+
answer("Yes")
547+
answer("no")
548+
}
549+
550+
assertEquals(emptyList(), poll.getAnswerVoters(1))
551+
poll.end()
552+
}
553+
538554
@Test
539555
@Order(Int.MAX_VALUE - 2)
540556
fun `delete channel`(): Unit = runBlocking {

0 commit comments

Comments
 (0)