Skip to content

Commit

Permalink
Fix oversights in #561 (#565)
Browse files Browse the repository at this point in the history
* DiscordVoiceState.channelId is now optional instead of nullable

* remove Deprecation for GuildRoleCreateRequest.id

* docs
  • Loading branch information
lukellmann committed Mar 12, 2022
1 parent 75d044d commit 0744e13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion rest/src/main/kotlin/json/request/GuildRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public data class GuildRoleCreateRequest(
@SerialName("unicode_emoji")
val unicodeEmoji: Optional<String?> = Optional.Missing(),
val mentionable: OptionalBoolean = OptionalBoolean.Missing,
@Deprecated("This is not part of Discord's current documentation.")
/** Only use this when creating a guild with roles. */
val id: OptionalSnowflake = OptionalSnowflake.Missing,
) {
@Deprecated("Renamed to 'hoist'.", ReplaceWith("this.hoist"), DeprecationLevel.ERROR)
Expand Down
34 changes: 20 additions & 14 deletions voice/src/main/kotlin/handlers/VoiceUpdateEventHandler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.kord.voice.handlers

import dev.kord.common.annotation.KordVoice
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.gateway.VoiceServerUpdate
import dev.kord.gateway.VoiceStateUpdate
import dev.kord.voice.VoiceConnection
Expand Down Expand Up @@ -32,22 +33,27 @@ internal class VoiceUpdateEventHandler(
on<VoiceStateUpdate> { event ->
if (!event.isRelatedToConnection(connection)) return@on

// we're not in a voice channel anymore. anything might've happened
// discord doesn't tell us whether the channel was deleted or if we were just moved
// let's just detach in 5 seconds and let it be cancelled if we join a new voice channel in that time.
if (event.voiceState.channelId == null) {
voiceUpdateLogger.trace { "detected a change to a null voice channel for guild ${connection.data.guildId}. waiting ${CONNECTION_DETACH_DURATION_IN_MILLIS}ms before shutdown to see if we were moved." }
when (event.voiceState.channelId) {

detachJob?.cancel()
detachJob = launch {
delay(CONNECTION_DETACH_DURATION_IN_MILLIS)
// we're not in a voice channel anymore. anything might've happened
// discord doesn't tell us whether the channel was deleted or if we were just moved
// let's just detach in 5 seconds and let it be cancelled if we join a new voice channel in that time.
OptionalSnowflake.Missing -> {
voiceUpdateLogger.trace { "detected a change to a null voice channel for guild ${connection.data.guildId}. waiting ${CONNECTION_DETACH_DURATION_IN_MILLIS}ms before shutdown to see if we were moved." }

connection.shutdown()
detachJob?.cancel()
detachJob = launch {
delay(CONNECTION_DETACH_DURATION_IN_MILLIS)

connection.shutdown()
}
}

is OptionalSnowflake.Value -> {
voiceUpdateLogger.trace { "detected a voice channel change for guild ${connection.data.guildId}, cancelling detachment." }
detachJob?.cancel()
detachJob = null
}
} else if (event.voiceState.channelId != null) {
voiceUpdateLogger.trace { "detected a voice channel change for guild ${connection.data.guildId}, cancelling detachment." }
detachJob?.cancel()
detachJob = null
}
}

Expand Down Expand Up @@ -82,4 +88,4 @@ private fun VoiceServerUpdate.isRelatedToConnection(connection: VoiceConnection)
@KordVoice
private fun VoiceStateUpdate.isRelatedToConnection(connection: VoiceConnection): Boolean {
return voiceState.guildId.value == connection.data.guildId && voiceState.userId == connection.data.selfId
}
}

0 comments on commit 0744e13

Please sign in to comment.