From 0674cdae8f340898f6190eab804768ffb0b39572 Mon Sep 17 00:00:00 2001 From: BartArys Date: Mon, 30 Dec 2019 15:00:14 +0100 Subject: [PATCH] Add preview to all LiveEntity code --- .../gitlab/kordlib/core/live/LiveEntity.kt | 19 +++++++++++++++++-- .../kordlib/core/live/channel/LiveCategory.kt | 1 + .../kordlib/core/live/channel/LiveChannel.kt | 5 +++-- .../core/live/channel/LiveDmChannel.kt | 1 + .../core/live/channel/LiveNewsChannel.kt | 1 + .../core/live/channel/LiveStoreChannel.kt | 1 + .../core/live/channel/LiveTextChannel.kt | 1 + .../core/live/channel/LiveVoiceChannel.kt | 1 + 8 files changed, 26 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/LiveEntity.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/LiveEntity.kt index 7e0112346b81..ed7bf1e633ed 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/LiveEntity.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/LiveEntity.kt @@ -1,6 +1,7 @@ package com.gitlab.kordlib.core.live import com.gitlab.kordlib.common.annotation.KordPreview +import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.behavior.MessageBehavior import com.gitlab.kordlib.core.cache.data.ReactionData import com.gitlab.kordlib.core.entity.Entity @@ -11,13 +12,22 @@ import com.gitlab.kordlib.core.event.channel.ChannelDeleteEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent import com.gitlab.kordlib.core.event.message.* import com.gitlab.kordlib.core.kordLogger +import com.gitlab.kordlib.core.on import kotlinx.atomicfu.atomic import kotlinx.atomicfu.update +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +/** + * A Discord entity that only emits events *related* to this entity. + * + * For example, a [LiveMessage] will only emit [MessageUpdateEvents][MessageUpdateEvent] of that message, and only emit + * [reactions][ReactionAddEvent] to that message. + */ +@KordPreview interface LiveEntity : Entity { val events: Flow @@ -41,9 +51,14 @@ abstract class AbstractLiveEntity : LiveEntity { } -inline fun LiveEntity.on(noinline consumer: suspend (T) -> Unit) = +/** + * Convenience method that will invoke the [consumer] on every event [T], the consumer is launched in the given [scope] + * or [Kord] by default and will not propagate any exceptions. + */ +@KordPreview +inline fun LiveEntity.on(scope: CoroutineScope = kord, noinline consumer: suspend (T) -> Unit) = events.buffer(Channel.UNLIMITED).filterIsInstance().onEach { runCatching { consumer(it) }.onFailure { kordLogger.catching(it) } - }.catch { kordLogger.catching(it) }.launchIn(kord) + }.catch { kordLogger.catching(it) }.launchIn(scope) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveCategory.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveCategory.kt index 2fd37e3a35d6..f6c322211780 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveCategory.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveCategory.kt @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.CategoryDeleteEvent import com.gitlab.kordlib.core.event.channel.CategoryUpdateEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent +@KordPreview fun Category.live() = LiveCategory(this) @KordPreview diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveChannel.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveChannel.kt index 13c7cad59f89..499fc9831956 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveChannel.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveChannel.kt @@ -13,7 +13,8 @@ import com.gitlab.kordlib.core.event.guild.GuildUpdateEvent import com.gitlab.kordlib.core.event.message.* import com.gitlab.kordlib.core.live.AbstractLiveEntity -fun Channel.live() = when(this) { +@KordPreview +fun Channel.live() = when (this) { is DmChannel -> this.live() is NewsChannel -> this.live() is StoreChannel -> this.live() @@ -27,7 +28,7 @@ abstract class LiveChannel : AbstractLiveEntity() { abstract val channel: Channel - override fun filter(event: Event): Boolean = when(event) { + override fun filter(event: Event): Boolean = when (event) { is VoiceStateUpdateEvent -> event.state.channelId == channel.id is ReactionAddEvent -> event.channelId == channel.id diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveDmChannel.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveDmChannel.kt index c9c10848d9aa..18fc61782c38 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveDmChannel.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveDmChannel.kt @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.DMChannelDeleteEvent import com.gitlab.kordlib.core.event.channel.DMChannelUpdateEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent +@KordPreview fun DmChannel.live() = LiveDmChannel(this) @KordPreview diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveNewsChannel.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveNewsChannel.kt index db34578781a1..5f6d233f644b 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveNewsChannel.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveNewsChannel.kt @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.NewsChannelDeleteEvent import com.gitlab.kordlib.core.event.channel.NewsChannelUpdateEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent +@KordPreview fun NewsChannel.live() = LiveNewsChannel(this) @KordPreview diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveStoreChannel.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveStoreChannel.kt index 91e6c131441f..daf373337e08 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveStoreChannel.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveStoreChannel.kt @@ -10,6 +10,7 @@ import com.gitlab.kordlib.core.event.channel.StoreChannelDeleteEvent import com.gitlab.kordlib.core.event.channel.StoreChannelUpdateEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent +@KordPreview fun StoreChannel.live() = LiveStoreChannel(this) @KordPreview diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveTextChannel.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveTextChannel.kt index 9a0c47fb0d5b..f36d1e3d099a 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveTextChannel.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveTextChannel.kt @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.TextChannelDeleteEvent import com.gitlab.kordlib.core.event.channel.TextChannelUpdateEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent +@KordPreview fun TextChannel.live() = LiveTextChannel(this) @KordPreview diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveVoiceChannel.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveVoiceChannel.kt index e8df0aa6732e..c4500af10b5b 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveVoiceChannel.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/live/channel/LiveVoiceChannel.kt @@ -11,6 +11,7 @@ import com.gitlab.kordlib.core.event.channel.VoiceChannelDeleteEvent import com.gitlab.kordlib.core.event.channel.VoiceChannelUpdateEvent import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent +@KordPreview fun VoiceChannel.live() = LiveVoiceChannel(this) @KordPreview