Skip to content

Commit

Permalink
Old properties for Update events (#438)
Browse files Browse the repository at this point in the history
* add old value to various core events

* Fix thread handling

* Add toString old value representation
  • Loading branch information
HopeBaron authored Nov 9, 2021
1 parent 07e64fb commit fc81710
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public interface ThreadChannel : GuildMessageChannel, ThreadChannelBehavior {

}

internal fun ThreadChannel(data: ChannelData, kord: Kord, supplier: EntitySupplier): ThreadChannel {
internal fun ThreadChannel(data: ChannelData, kord: Kord, supplier: EntitySupplier = kord.defaultSupplier): ThreadChannel {
return object : ThreadChannel {

override val data: ChannelData
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/event/channel/ChannelDeleteEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class UnknownChannelDeleteEvent(
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)

) : ChannelCreateEvent, CoroutineScope by coroutineScope {
) : ChannelDeleteEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "UnknownChannelDeleteEvent(channel=$channel, shard=$shard)"
}
Expand Down
26 changes: 18 additions & 8 deletions core/src/main/kotlin/event/channel/ChannelUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,99 @@ import dev.kord.core.event.kordCoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext


public interface ChannelUpdateEvent : Event {
public val channel: Channel
public val old: Channel?
override val kord: Kord
get() = channel.kord
}

public class CategoryUpdateEvent(
override val channel: Category,
override val old: Category?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "CategoryUpdateEvent(channel=$channel, shard=$shard)"
return "CategoryUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}

public class DMChannelUpdateEvent(
override val channel: DmChannel,
override val old: DmChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "DMChannelUpdateEvent(channel=$channel, shard=$shard)"
return "DMChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}

public class NewsChannelUpdateEvent(
override val channel: NewsChannel,
override val old: NewsChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "NewsChannelUpdateEvent(channel=$channel, shard=$shard)"
return "NewsChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}

public class StoreChannelUpdateEvent(
override val channel: StoreChannel,
override val old: StoreChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "StoreChannelUpdateEvent(channel=$channel, shard=$shard)"
return "StoreChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}

public class TextChannelUpdateEvent(
override val channel: TextChannel,
override val old: TextChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "TextChannelUpdateEvent(channel=$channel, shard=$shard)"
return "TextChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}

public class VoiceChannelUpdateEvent(
override val channel: VoiceChannel,
override val old: VoiceChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope{
override fun toString(): String {
return "VoiceChannelUpdateEvent(channel=$channel, shard=$shard)"
return "VoiceChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}


public class StageChannelUpdateEvent(
override val channel: StageChannel,
override val old: StageChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "StageChannelUpdateEvent(channel=$channel, shard=$shard)"
return "StageChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}

public class UnknownChannelUpdateEvent(
override val channel: Channel,
override val old: Channel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ChannelUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "UnknownChannelUpdateEvent(channel=$channel, shard=$shard)"
return "UnknownChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}
10 changes: 6 additions & 4 deletions core/src/main/kotlin/event/channel/thread/ThreadUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.event.channel.ChannelUpdateEvent
import dev.kord.core.event.kordCoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext

public sealed interface ThreadUpdateEvent : ChannelUpdateEvent {
override val channel: ThreadChannel
Expand All @@ -15,33 +14,36 @@ public sealed interface ThreadUpdateEvent : ChannelUpdateEvent {

public class TextChannelThreadUpdateEvent(
override val channel: TextChannelThread,
override val old: TextChannelThread?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) :
ThreadUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "TextThreadChannelUpdateEvent(channel=$channel, shard=$shard)"
return "TextThreadChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}


public class NewsChannelThreadUpdateEvent(
override val channel: NewsChannelThread,
override val old: NewsChannelThread?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ThreadUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "NewsThreadChannelUpdateEvent(channel=$channel, shard=$shard)"
return "NewsThreadChannelUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}


public class UnknownChannelThreadUpdateEvent(
override val channel: ThreadChannel,
override val old: ThreadChannel?,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(channel.kord)
) : ThreadUpdateEvent, CoroutineScope by coroutineScope {
override fun toString(): String {
return "UnknownChannelThreadUpdateEvent(channel=$channel, shard=$shard)"
return "UnknownChannelThreadUpdateEvent(channel=$channel, old=$old, shard=$shard)"
}
}
5 changes: 3 additions & 2 deletions core/src/main/kotlin/event/guild/EmojisUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlin.coroutines.CoroutineContext
public class EmojisUpdateEvent(
public val guildId: Snowflake,
public val emojis: Set<GuildEmoji>,
public val old: Set<GuildEmoji>?,
override val kord: Kord,
override val shard: Int,
override val supplier: EntitySupplier = kord.defaultSupplier,
Expand All @@ -29,9 +30,9 @@ public class EmojisUpdateEvent(
public suspend fun getGuildOrNull(): Guild? = supplier.getGuildOrNull(guildId)

override fun withStrategy(strategy: EntitySupplyStrategy<*>): EmojisUpdateEvent =
EmojisUpdateEvent(guildId, emojis, kord, shard, strategy.supply(kord))
EmojisUpdateEvent(guildId, emojis, old, kord, shard, strategy.supply(kord))

override fun toString(): String {
return "EmojisUpdateEvent(guildId=$guildId, emojis=$emojis, kord=$kord, shard=$shard, supplier=$supplier)"
return "EmojisUpdateEvent(guildId=$guildId, emojis=$emojis, old=$old, kord=$kord, shard=$shard, supplier=$supplier)"
}
}
4 changes: 3 additions & 1 deletion core/src/main/kotlin/event/guild/MemberLeaveEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.entity.Guild
import dev.kord.core.entity.Member
import dev.kord.core.entity.User
import dev.kord.core.event.Event
import dev.kord.core.event.kordCoroutineScope
Expand All @@ -12,6 +13,7 @@ import kotlin.coroutines.CoroutineContext

public class MemberLeaveEvent(
public val user: User,
public val old: Member?,
public val guildId: Snowflake,
override val shard: Int,
public val coroutineScope: CoroutineScope = kordCoroutineScope(user.kord)
Expand All @@ -26,7 +28,7 @@ public class MemberLeaveEvent(
public suspend fun getGuildOrNull(): Guild? = guild.asGuildOrNull()

override fun toString(): String {
return "MemberLeaveEvent(user=$user, guildId=$guildId, shard=$shard)"
return "MemberLeaveEvent(user=$user, old=$old, guildId=$guildId, shard=$shard)"
}

}
5 changes: 3 additions & 2 deletions core/src/main/kotlin/event/role/RoleUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlin.coroutines.CoroutineContext

public class RoleUpdateEvent(
public val role: Role,
public val old: Role?,
override val shard: Int,
override val supplier: EntitySupplier = role.kord.defaultSupplier,
public val coroutineScope: CoroutineScope = kordCoroutineScope(role.kord)
Expand All @@ -31,9 +32,9 @@ public class RoleUpdateEvent(
public suspend fun getGuildOrNull(): Guild? = supplier.getGuildOrNull(guildId)

override fun withStrategy(strategy: EntitySupplyStrategy<*>): RoleUpdateEvent =
RoleUpdateEvent(role, shard, strategy.supply(kord))
RoleUpdateEvent(role, old, shard, strategy.supply(kord))

override fun toString(): String {
return "RoleUpdateEvent(role=$role, shard=$shard, supplier=$supplier)"
return "RoleUpdateEvent(role=$role, old=$old, shard=$shard, supplier=$supplier)"
}
}
20 changes: 10 additions & 10 deletions core/src/main/kotlin/gateway/handler/ChannelEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import dev.kord.core.event.channel.data.ChannelPinsUpdateEventData
import dev.kord.core.event.channel.data.TypingStartEventData
import dev.kord.gateway.*
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import dev.kord.core.event.Event as CoreEvent

@Suppress("EXPERIMENTAL_API_USAGE")
Expand Down Expand Up @@ -59,18 +58,19 @@ internal class ChannelEventHandler(

private suspend fun handle(event: ChannelUpdate, shard: Int, kord: Kord, coroutineScope: CoroutineScope): CoreEvent? {
val data = ChannelData.from(event.channel)
val oldData = cache.query<ChannelData> { idEq(ChannelData::id, data.id) }.singleOrNull()
cache.put(data)

val old = oldData?.let { Channel.from(it, kord) }
val coreEvent = when (val channel = Channel.from(data, kord)) {
is NewsChannel -> NewsChannelUpdateEvent(channel, shard, coroutineScope)
is StoreChannel -> StoreChannelUpdateEvent(channel, shard, coroutineScope)
is DmChannel -> DMChannelUpdateEvent(channel, shard, coroutineScope)
is TextChannel -> TextChannelUpdateEvent(channel, shard, coroutineScope)
is StageChannel -> StageChannelUpdateEvent(channel, shard, coroutineScope)
is VoiceChannel -> VoiceChannelUpdateEvent(channel, shard, coroutineScope)
is Category -> CategoryUpdateEvent(channel, shard, coroutineScope)
is NewsChannel -> NewsChannelUpdateEvent(channel, old as? NewsChannel, shard, coroutineScope)
is StoreChannel -> StoreChannelUpdateEvent(channel, old as? StoreChannel, shard, coroutineScope)
is DmChannel -> DMChannelUpdateEvent(channel, old as? DmChannel, shard, coroutineScope)
is TextChannel -> TextChannelUpdateEvent(channel, old as? TextChannel, shard, coroutineScope)
is StageChannel -> StageChannelUpdateEvent(channel, old as? StageChannel, shard, coroutineScope)
is VoiceChannel -> VoiceChannelUpdateEvent(channel, old as? VoiceChannel, shard, coroutineScope)
is Category -> CategoryUpdateEvent(channel, old as? Category, shard, coroutineScope)
is ThreadChannel -> return null
else -> UnknownChannelUpdateEvent(channel, shard, coroutineScope)
else -> UnknownChannelUpdateEvent(channel, old, shard, coroutineScope)

}

Expand Down
25 changes: 22 additions & 3 deletions core/src/main/kotlin/gateway/handler/GuildEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ internal class GuildEventHandler(
coroutineScope: CoroutineScope
): EmojisUpdateEvent =
with(event.emoji) {

val data = emojis.map { EmojiData.from(guildId, it.id!!, it) }
val old = cache.query<EmojiData> { idEq(EmojiData::guildId, guildId) }.asFlow().map {
GuildEmoji(it, kord)
}.toSet()
cache.putAll(data)

val emojis = data.map { GuildEmoji(it, kord) }
Expand All @@ -153,7 +157,7 @@ internal class GuildEventHandler(
it.copy(emojis = emojis.map { emoji -> emoji.id })
}

return EmojisUpdateEvent(guildId, emojis.toSet(), kord, shard, coroutineScope = coroutineScope)
return EmojisUpdateEvent(guildId, emojis.toSet(), old, kord, shard, coroutineScope = coroutineScope)
}


Expand Down Expand Up @@ -191,10 +195,18 @@ internal class GuildEventHandler(
): MemberLeaveEvent =
with(event.member) {
val userData = UserData.from(user)

val oldData = cache.query<MemberData> {
idEq(MemberData::userId, userData.id)
idEq(MemberData::guildId, guildId)
}.singleOrNull()

cache.query<UserData> { idEq(UserData::id, userData.id) }.remove()

val user = User(userData, kord)
val old = oldData?.let { Member(it, userData, kord) }

return MemberLeaveEvent(user, guildId, shard, coroutineScope = coroutineScope)
return MemberLeaveEvent(user, old, guildId, shard, coroutineScope = coroutineScope)
}

private suspend fun handle(
Expand Down Expand Up @@ -238,9 +250,16 @@ internal class GuildEventHandler(
coroutineScope: CoroutineScope
): RoleUpdateEvent {
val data = RoleData.from(event.role)

val oldData = cache.query<RoleData> {
idEq(RoleData::id, data.id)
idEq(RoleData::guildId, data.guildId) // TODO("Is this worth keeping?")
}.singleOrNull()

val old = oldData?.let { Role(it, kord) }
cache.put(data)

return RoleUpdateEvent(Role(data, kord), shard, coroutineScope = coroutineScope)
return RoleUpdateEvent(Role(data, kord), old, shard, coroutineScope = coroutineScope)
}

private suspend fun handle(
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/kotlin/gateway/handler/ThreadEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ public class ThreadEventHandler(

public suspend fun handle(event: ThreadUpdate, shard: Int, kord: Kord, coroutineScope: CoroutineScope): CoreEvent? {
val channelData = event.channel.toData()
val oldData = cache.query<ChannelData> {
idEq(ChannelData::id, event.channel.id)
idEq(ChannelData::guildId, event.channel.guildId.value)
}.singleOrNull()
cache.put(channelData)

val old = oldData?.let { ThreadChannel(it, kord) }

val coreEvent = when (val channel = Channel.from(channelData, kord)) {
is NewsChannelThread -> NewsChannelThreadUpdateEvent(channel, shard, coroutineScope)
is TextChannelThread -> TextChannelThreadUpdateEvent(channel, shard, coroutineScope)
is ThreadChannel -> UnknownChannelThreadUpdateEvent(channel, shard, coroutineScope)
is NewsChannelThread -> NewsChannelThreadUpdateEvent(channel, old as? NewsChannelThread, shard, coroutineScope)
is TextChannelThread -> TextChannelThreadUpdateEvent(channel, old as? TextChannelThread, shard, coroutineScope)
is ThreadChannel -> UnknownChannelThreadUpdateEvent(channel,old, shard, coroutineScope)
else -> return null
}

Expand Down

0 comments on commit fc81710

Please sign in to comment.