Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
- Update Kotlin to 1.9.0
- Update Kmongo to 4.10.0
- Update coroutines to 1.7.3
- Update serialization to 1.6.0-RC
- Update KSP to 1.9.0-1.0.11
- Bump JDK requirement to 20
- Fix a bug in /fix command causing an exception when trying to re-establish voice connection
- Fix support for /redeploy in MusicPlugin
- Add support for Loom
- Use Loom to call YouTube client
- Use IO Dispatcher for Kord and related coroutines
  • Loading branch information
DRSchlaubi committed Aug 8, 2023
1 parent 8bc7c3d commit aa8d2e5
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.schlaubi.mikbot.plugin.api.util

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors

/**
* Coroutine dispatcher using [Executors.newVirtualThreadPerTaskExecutor] for dispatching.
*/
public val loomDispatcher: CoroutineDispatcher = Executors.newVirtualThreadPerTaskExecutor()
.asCoroutineDispatcher()

/**
* Executes the oncoming block using [loomDispatcher].
*/
public suspend fun <T> blocking(block: suspend CoroutineScope.() -> T): T = withContext(loomDispatcher, block)
3 changes: 1 addition & 2 deletions buildSrc/src/main/kotlin/mikbot-module.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import org.gradle.jvm.toolchain.internal.DefaultToolchainSpec
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
Expand All @@ -21,7 +20,7 @@ tasks {
}

kotlin {
jvmToolchain(19)
jvmToolchain(20)
}

ktlint {
Expand Down
9 changes: 8 additions & 1 deletion gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

@Suppress("DSL_SCOPE_VIOLATION") plugins {
alias(libs.plugins.gradle.publish)
Expand All @@ -22,7 +23,13 @@ dependencies {
}

kotlin {
jvmToolchain(19)
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

java {
sourceCompatibility = JavaVersion.VERSION_17
}

gradlePlugin {
Expand Down
14 changes: 7 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[versions]
kotlin = "1.8.22"
kotlin = "1.9.0"
kordex = "1.5.8-SNAPSHOT"
kmongo = "4.9.0"
coroutines = "1.7.1"
serialization = "1.5.1"
ktor = "2.3.1"
kmongo = "4.10.0"
coroutines = "1.7.3"
serialization = "1.6.0-RC"
ktor = "2.3.3"
kord = "0.10.0-SNAPSHOT"
jjwt = "0.11.5"
api = "3.20.0"
ksp = "1.8.22-1.0.11"
api = "3.22.0"
ksp = "1.9.0-1.0.11"
lavakord = "5.1.4"

[libraries]
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion music/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
subprojects {
version = "3.0.6-SNAPSHOT"
version = "3.1.0-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private object ReEstablishVoiceConnection : RecoveryStep {
val state = musicPlayer.toState()
val channelId = musicPlayer.link.lastChannelId!!

musicPlayer.link.disconnectAudio()
musicPlayer.stop()
delay(200.milliseconds)
musicPlayer.link.connectAudio(channelId)
state.applyToPlayer(musicPlayer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import dev.schlaubi.mikbot.plugin.api.PluginContext
import dev.schlaubi.mikbot.plugin.api.PluginMain
import dev.schlaubi.mikmusic.core.audio.LavalinkManager
import dev.schlaubi.mikmusic.musicchannel.MusicInteractionModule
import dev.schlaubi.mikmusic.util.JsonObjectSerializer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.litote.kmongo.serialization.registerSerializer

@PluginMain
class MusicPlugin(wrapper: PluginContext) : Plugin(wrapper) {

override fun start() {
registerSerializer(JsonObjectSerializer)
}
override fun ExtensibleBotBuilder.ExtensionsBuilder.addExtensions() {
add(::LavalinkManager)
add(::MusicModule)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
@file:UseSerializers(JsonObjectSerializer::class)
@file:Suppress("DataClassCanBeRecord")

package dev.schlaubi.mikmusic.player

import dev.arbjerg.lavalink.protocol.v4.Track
import dev.kord.common.entity.Snowflake
import dev.schlaubi.lavakord.plugins.sponsorblock.model.YouTubeChapter
import dev.schlaubi.mikmusic.util.JsonObjectSerializer
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import kotlin.time.Duration

@Serializable
sealed class QueuedTrack {

abstract val track: Track
abstract val queuedBy: Snowflake

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.schlaubi.mikmusic.util

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject

object JsonObjectSerializer : JsonElementSerializer<JsonObject>() {
override val serializer: KSerializer<JsonObject> = JsonObject.serializer()
}

abstract class JsonElementSerializer<T : JsonElement> : KSerializer<T> {
protected abstract val serializer: KSerializer<T>
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("JsonStringSerializer", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): T = Json.decodeFromString(serializer, decoder.decodeString())
override fun serialize(encoder: Encoder, value: T) =
encoder.encodeString(Json.encodeToString(serializer, value))
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import com.google.api.services.youtube.model.ChannelListResponse
import com.google.api.services.youtube.model.Video
import com.google.api.services.youtube.model.VideoListResponse
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.schlaubi.mikbot.plugin.api.util.blocking
import dev.schlaubi.mikmusic.core.Config
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

private val client: YouTube = YouTube.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
Expand All @@ -23,13 +22,13 @@ private val client: YouTube = YouTube.Builder(
.build()

private suspend fun getVideosById(videoId: String): VideoListResponse {
return withContext(Dispatchers.IO) {
return blocking {
client.videos().list(listOf("snippet", "localizations", "contentDetails")).setId(listOf(videoId)).execute()
}
}

private suspend fun getChannelsById(channelId: String): ChannelListResponse {
return withContext(Dispatchers.IO) {
return blocking {
client.channels().list(listOf("snippet")).setId(listOf(channelId)).execute()
}
}
Expand Down
11 changes: 2 additions & 9 deletions runtime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.archivesName
import java.nio.file.Files

plugins {
Expand All @@ -20,14 +19,7 @@ allprojects {
}
}

subprojects {
if (path != ":votebot:common") { // MPP projects don't work with ktlint
apply(plugin = "org.jlleitschuh.gradle.ktlint")
}
}

dependencies {

// Plugin system
implementation(libs.pf4j)
implementation(libs.pf4j.update)
Expand All @@ -46,6 +38,7 @@ dependencies {
application {
mainClass = "dev.schlaubi.musicbot.LauncherKt"
applicationName = "mikmusic"
applicationDefaultJvmArgs = listOf("--enable-preview")
}

tasks {
Expand Down Expand Up @@ -84,7 +77,7 @@ tasks {

distTar {
compression = Compression.GZIP
archivesName = "bot"
archiveBaseName = "bot"
archiveExtension = "tar.gz"
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/src/main/kotlin/dev/schlaubi/musicbot/core/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import dev.schlaubi.musicbot.core.sentry.SentryExtensionPoint
import dev.schlaubi.musicbot.module.owner.OwnerModuleImpl
import dev.schlaubi.musicbot.module.settings.SettingsModuleImpl
import dev.schlaubi.stdx.core.onEach
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import mu.KotlinLogging
Expand All @@ -52,6 +53,7 @@ class Bot : KordExKoinComponent, PluginContext {
kord {
eventFlow = (pluginSystem as DefaultPluginSystem).events

defaultDispatcher = Dispatchers.IO
stackTraceRecovery = true

cache {
Expand Down

0 comments on commit aa8d2e5

Please sign in to comment.