commonMain.live.LiveMember.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kord-core Show documentation
Show all versions of kord-core Show documentation
Idiomatic Kotlin Wrapper for The Discord API
The newest version!
package dev.kord.core.live
import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Member
import dev.kord.core.event.Event
import dev.kord.core.event.guild.BanAddEvent
import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.event.guild.MemberLeaveEvent
import dev.kord.core.event.guild.MemberUpdateEvent
import dev.kord.core.live.exception.LiveCancellationException
import kotlinx.coroutines.*
@KordPreview
public fun Member.live(
coroutineScope: CoroutineScope = kord + SupervisorJob(kord.coroutineContext.job)
): LiveMember = LiveMember(this, coroutineScope)
@KordPreview
public inline fun Member.live(
coroutineScope: CoroutineScope = kord + SupervisorJob(kord.coroutineContext.job),
block: LiveMember.() -> Unit
): LiveMember = this.live(coroutineScope).apply(block)
@KordPreview
public fun LiveMember.onUpdate(scope: CoroutineScope = this, block: suspend (MemberUpdateEvent) -> Unit): Job =
on(scope = scope, consumer = block)
@KordPreview
public class LiveMember(
member: Member,
coroutineScope: CoroutineScope = member.kord + SupervisorJob(member.kord.coroutineContext.job)
) : AbstractLiveKordEntity(member.kord, coroutineScope), KordEntity {
override val id: Snowflake
get() = member.id
public var member: Member = member
private set
override fun filter(event: Event): Boolean = when (event) {
is MemberLeaveEvent -> member.id == event.user.id
is MemberUpdateEvent -> member.id == event.member.id
is BanAddEvent -> member.id == event.user.id
is GuildDeleteEvent -> member.guildId == event.guildId
else -> false
}
override fun update(event: Event): Unit = when (event) {
is MemberLeaveEvent -> shutDown(LiveCancellationException(event, "The member has left"))
is BanAddEvent -> shutDown(LiveCancellationException(event, "The member is banned"))
is GuildDeleteEvent -> shutDown(LiveCancellationException(event, "The guild is deleted"))
is MemberUpdateEvent -> member = event.member
else -> Unit
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy