Skip to content

Commit

Permalink
Add PollBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Aug 25, 2024
1 parent 65a578f commit 2a306cd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
35 changes: 34 additions & 1 deletion core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,37 @@ public final class dev/kord/core/behavior/MessageCommandBehavior$DefaultImpls {
public static fun compareTo (Ldev/kord/core/behavior/MessageCommandBehavior;Ldev/kord/core/entity/Entity;)I
}

public abstract interface class dev/kord/core/behavior/PollBehavior : dev/kord/core/behavior/MessageBehavior {
public abstract fun end (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getAnswerVoters (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class dev/kord/core/behavior/PollBehavior$DefaultImpls {
public static fun addReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/GuildEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun addReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun asMessage (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun asMessageOrNull (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun compareTo (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/Entity;)I
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;
public static fun delete (Ldev/kord/core/behavior/PollBehavior;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun deleteAllReactions (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun deleteOwnReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
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;
public static fun deleteReaction (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun end (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun fetchMessage (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun fetchMessageOrNull (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun getAnswerVoters (Ldev/kord/core/behavior/PollBehavior;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun getChannel (Ldev/kord/core/behavior/PollBehavior;)Ldev/kord/core/behavior/channel/MessageChannelBehavior;
public static fun getChannel (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun getChannelOrNull (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun getReactors (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/entity/ReactionEmoji;)Lkotlinx/coroutines/flow/Flow;
public static fun pin (Ldev/kord/core/behavior/PollBehavior;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun publish (Ldev/kord/core/behavior/PollBehavior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun unpin (Ldev/kord/core/behavior/PollBehavior;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun withStrategy (Ldev/kord/core/behavior/PollBehavior;Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/behavior/MessageBehavior;
}

public abstract interface class dev/kord/core/behavior/RoleBehavior : dev/kord/core/entity/KordEntity, dev/kord/core/entity/Strategizable {
public abstract fun asRole (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun asRoleOrNull (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -6922,7 +6953,7 @@ public final class dev/kord/core/entity/PermissionOverwriteEntity : dev/kord/cor
public synthetic fun withStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/entity/Strategizable;
}

public final class dev/kord/core/entity/Poll : dev/kord/core/entity/Message {
public final class dev/kord/core/entity/Poll : dev/kord/core/behavior/PollBehavior, dev/kord/core/entity/Message {
public fun <init> (Ldev/kord/core/cache/data/MessageData;Ldev/kord/core/Kord;Ldev/kord/core/supplier/EntitySupplier;)V
public synthetic fun <init> (Ldev/kord/core/cache/data/MessageData;Ldev/kord/core/Kord;Ldev/kord/core/supplier/EntitySupplier;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun addReaction (Ldev/kord/core/entity/GuildEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand All @@ -6937,10 +6968,12 @@ public final class dev/kord/core/entity/Poll : dev/kord/core/entity/Message {
public fun deleteOwnReaction (Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun deleteReaction (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun deleteReaction (Ldev/kord/core/entity/ReactionEmoji;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun end (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun equals (Ljava/lang/Object;)Z
public fun fetchMessage (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun fetchMessageOrNull (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getActionRows ()Ljava/util/List;
public fun getAnswerVoters (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public fun getAttachments ()Ljava/util/Set;
public fun getAuthor ()Ldev/kord/core/entity/User;
Expand Down
7 changes: 6 additions & 1 deletion core/api/core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,11 @@ abstract interface dev.kord.core.behavior/MessageCommandBehavior : dev.kord.core
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]
}

abstract interface dev.kord.core.behavior/PollBehavior : dev.kord.core.behavior/MessageBehavior { // dev.kord.core.behavior/PollBehavior|null[0]
open suspend fun end(): dev.kord.core.entity/Poll // dev.kord.core.behavior/PollBehavior.end|end(){}[0]
open suspend fun getAnswerVoters(kotlin/Int): kotlin.collections/List<dev.kord.core.entity/User> // dev.kord.core.behavior/PollBehavior.getAnswerVoters|getAnswerVoters(kotlin.Int){}[0]
}

abstract interface dev.kord.core.behavior/RoleBehavior : dev.kord.core.entity/KordEntity, dev.kord.core.entity/Strategizable { // dev.kord.core.behavior/RoleBehavior|null[0]
abstract val guildId // dev.kord.core.behavior/RoleBehavior.guildId|{}guildId[0]
abstract fun <get-guildId>(): dev.kord.common.entity/Snowflake // dev.kord.core.behavior/RoleBehavior.guildId.<get-guildId>|<get-guildId>(){}[0]
Expand Down Expand Up @@ -8081,7 +8086,7 @@ final class dev.kord.core.entity/PermissionOverwriteEntity : dev.kord.core.entit
final suspend fun getGuildOrNull(): dev.kord.core.entity/Guild? // dev.kord.core.entity/PermissionOverwriteEntity.getGuildOrNull|getGuildOrNull(){}[0]
}

final class dev.kord.core.entity/Poll : dev.kord.core.entity/Message { // dev.kord.core.entity/Poll|null[0]
final class dev.kord.core.entity/Poll : dev.kord.core.behavior/PollBehavior, dev.kord.core.entity/Message { // dev.kord.core.entity/Poll|null[0]
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]

final val data // dev.kord.core.entity/Poll.data|{}data[0]
Expand Down
29 changes: 27 additions & 2 deletions core/src/commonMain/kotlin/behavior/MessageBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package dev.kord.core.behavior

import dev.kord.common.entity.Permission
import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.channel.MessageChannelBehavior
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.cache.data.MessageData
import dev.kord.core.cache.data.UserData
import dev.kord.core.entity.*
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.hash
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.core.supplier.getChannelOf
Expand All @@ -20,7 +21,6 @@ import dev.kord.rest.builder.message.modify.WebhookMessageModifyBuilder
import dev.kord.rest.request.RestRequestException
import dev.kord.rest.service.RestClient
import kotlinx.coroutines.flow.Flow
import dev.kord.core.hash
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

Expand Down Expand Up @@ -215,7 +215,32 @@ public interface MessageBehavior : KordEntity, Strategizable {
override fun withStrategy(
strategy: EntitySupplyStrategy<*>,
): MessageBehavior = MessageBehavior(channelId, id, kord, strategy)
}

/**
* Behavior of a Poll message.
*/
public interface PollBehavior : MessageBehavior {

/**
* Retrieves the voters who voted for the specified [answer][answerId].
*/
public suspend fun getAnswerVoters(answerId: Int): List<User> =
kord.rest.channel.getPollUsersByAnswer(channelId, id, answerId).map {
val data = UserData.from(it)

User(data, kord, supplier)
}

/**
* Ends this poll.
*/
public suspend fun end(): Poll {
val message = kord.rest.channel.expirePoll(channelId, id)
val data = MessageData.from(message)

return Message(data, kord, supplier) as Poll
}
}

public fun MessageBehavior(
Expand Down
3 changes: 2 additions & 1 deletion core/src/commonMain/kotlin/entity/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.kord.common.entity.optional.*
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.MessageBehavior
import dev.kord.core.behavior.PollBehavior
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.behavior.interaction.response.InteractionResponseBehavior
Expand Down Expand Up @@ -369,7 +370,7 @@ public class Poll(
override val data: MessageData,
override val kord: Kord,
override val supplier: EntitySupplier = kord.defaultSupplier,
) : Message {
) : Message, PollBehavior {
/**
* The [poll][DiscordPoll].
*/
Expand Down
16 changes: 16 additions & 0 deletions core/src/jvmTest/kotlin/rest/RestTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.kord.core.rest

import dev.kord.common.Color
import dev.kord.common.annotation.KordUnsafe
import dev.kord.common.entity.*
import dev.kord.core.Kord
import dev.kord.core.behavior.*
Expand All @@ -21,6 +22,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.*
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
import kotlin.io.path.toPath
Expand Down Expand Up @@ -535,6 +537,20 @@ class RestServiceTest {

}

@OptIn(KordUnsafe::class)
@Test
@Order(29)
fun `poll in channel`() = runTest {
val poll = channel.createPoll {
question("Is Kord great?")
answer("Yes")
answer("no")
}

assertEquals(emptyList(), poll.getAnswerVoters(1))
poll.end()
}

@Test
@Order(Int.MAX_VALUE - 2)
fun `delete channel`(): Unit = runBlocking {
Expand Down

0 comments on commit 2a306cd

Please sign in to comment.