Skip to content

Commit

Permalink
Add support for LavaSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 30, 2023
1 parent 4deebee commit 521b9c6
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

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

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ allprojects {
maven("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven")
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://maven.topi.wtf/snapshots")
maven("https://maven.topi.wtf/releases")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.schlaubi.lavakord.audio.internal

import dev.arbjerg.lavalink.protocol.v4.*
import dev.arbjerg.lavalink.protocol.v4.Error
import dev.arbjerg.lavalink.protocol.v4.LavalinkSerializersModule
import dev.arbjerg.lavalink.protocol.v4.Message
import dev.arbjerg.lavalink.protocol.v4.VoiceState
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.LavaKordOptions
import dev.schlaubi.lavakord.RestException
import dev.schlaubi.lavakord.audio.*
import dev.schlaubi.lavakord.internal.HttpEngine
import dev.schlaubi.lavakord.internal.RestNodeImpl
import dev.schlaubi.lavakord.rest.updatePlayer
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.*
Expand Down
53 changes: 53 additions & 0 deletions plugins/lavasearch/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinMultiplatform
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`lavalink-module`
`lavalink-publishing`
kotlin("plugin.serialization")
alias(libs.plugins.ksp)
}

kotlin {
jvm {
compilations.all {
compilerOptions.configure {
jvmTarget = JvmTarget.JVM_11
}
}
}
sourceSets {
all {
languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
languageSettings.optIn("dev.schlaubi.lavakord.PluginApi")
languageSettings.optIn("dev.schlaubi.lavakord.UnsafeRestApi")
}
commonMain {
kotlin.srcDir(layout.buildDirectory.dir("generated/ksp/metadata/commonMain/kotlin"))
dependencies {
api(projects.core)
api(libs.lavasearch.protocol)

implementation(libs.ktor.client.resources)
implementation(libs.kord.ksp.annotations)
}
}
}
}

dependencies {
kspCommonMainMetadata(libs.kord.ksp.processors)
}

tasks {
listOf("sourcesJar", "jsSourcesJar", "jvmSourcesJar", "compileKotlinJs", "compileKotlinJvm", "dokkaHtml").forEach {
named(it) {
dependsOn("kspCommonMainKotlinMetadata")
}
}
}

mavenPublishing {
configure(KotlinMultiplatform(JavadocJar.Dokka("dokkaHtml")))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// THIS FILE IS AUTO-GENERATED BY KordEnumProcessor.kt, DO NOT EDIT!
@file:Suppress(names = arrayOf("RedundantVisibilityModifier", "IncorrectFormatting",
"ReplaceArrayOfWithLiteral", "SpellCheckingInspection", "GrazieInspection"))

package dev.schlaubi.lavakord.plugins.lavasearch.model

import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlin.String
import kotlin.Suppress
import kotlin.collections.List
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
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

/**
* See [SearchType]s in the
* [Discord Developer Documentation](https://github.com/topi314/LavaSearch?tab=readme-ov-file#api).
*/
@Serializable(with = SearchType.Serializer::class)
public sealed class SearchType(
/**
* The raw value used by Discord.
*/
public val `value`: String,
) {
public final override fun equals(other: Any?): Boolean = this === other ||
(other is SearchType && this.value == other.value)

public final override fun hashCode(): Int = value.hashCode()

public final override fun toString(): String =
"SearchType.${this::class.simpleName}(value=$value)"

/**
* An unknown [SearchType].
*
* This is used as a fallback for [SearchType]s that haven't been added to Kord yet.
*/
public class Unknown(
`value`: String,
) : SearchType(value)

public object Track : SearchType("track")

public object Album : SearchType("album")

public object Artist : SearchType("artist")

public object Playlist : SearchType("playlist")

/**
* Search suggestions
*/
public object Text : SearchType("text")

internal object Serializer : KSerializer<SearchType> {
public override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.schlaubi.lavakord.plugins.lavasearch.model.SearchType",
PrimitiveKind.STRING)

public override fun serialize(encoder: Encoder, `value`: SearchType) =
encoder.encodeString(value.value)

public override fun deserialize(decoder: Decoder) =
when (val value = decoder.decodeString()) {
"track" -> Track
"album" -> Album
"artist" -> Artist
"playlist" -> Playlist
"text" -> Text
else -> Unknown(value)
}
}

public companion object {
/**
* A [List] of all known [SearchType]s.
*/
public val entries: List<SearchType> by lazy(mode = PUBLICATION) {
listOf(
Track,
Album,
Artist,
Playlist,
Text,
)
}

}
}
17 changes: 17 additions & 0 deletions plugins/lavasearch/src/commonMain/kotlin/Plugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.schlaubi.lavakord.plugins.lavasearch

import dev.schlaubi.lavakord.Plugin

/**
* Bindings for the [LavaSrc plugin](https://github.com/topi314/LavaSearch).
*
* ```kotlin
* plugins {
* install(LavaSearch)
* }
* ```
*/
public object LavaSearch : Plugin {
override val name: String = "lavasearch-plugin"
override val version: String = "4.0.0-beta.3"
}
36 changes: 36 additions & 0 deletions plugins/lavasearch/src/commonMain/kotlin/model/SearchType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@file:GenerateKordEnum(
"SearchType",
GenerateKordEnum.ValueType.STRING,
docUrl = "https://github.com/topi314/LavaSearch?tab=readme-ov-file#api",
entries = [
GenerateKordEnum.Entry(
"Track",
stringValue = "track",
kDoc = "Tracks."
),
GenerateKordEnum.Entry(
"Album",
stringValue = "album",
kDoc = "Album."
),
GenerateKordEnum.Entry(
"Artist",
stringValue = "artist",
kDoc = "Artists."
),
GenerateKordEnum.Entry(
"Playlist",
stringValue = "playlist",
kDoc = "Playlists."
),
GenerateKordEnum.Entry(
"Text",
stringValue = "text",
kDoc = "Search suggestions."
)
]
)

package dev.schlaubi.lavakord.plugins.lavasearch.model

import dev.kord.ksp.GenerateKordEnum
41 changes: 41 additions & 0 deletions plugins/lavasearch/src/commonMain/kotlin/rest/Rest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.schlaubi.lavakord.plugins.lavasearch.rest

import com.github.topi314.lavasearch.protocol.SearchResult
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.plugins.lavasearch.model.SearchType
import dev.schlaubi.lavakord.rest.get

/**
* Searches for [query].
*
* @param types the allowed [SearchTypes][SearchType]
* @see SearchResult
*/
public suspend fun Node.search(query: String, vararg types: SearchType): SearchResult =
search(query, types.asIterable())

/**
* Searches for [query].
*
* @param types the allowed [SearchTypes][SearchType]
* @see SearchResult
*/
public suspend fun Node.search(query: String, types: Iterable<SearchType>): SearchResult =
get(LavaSearchRoute(query, types.toList()))

/**
* Searches for [query].
*
* @param types the allowed [SearchTypes][SearchType]
* @see SearchResult
*/
public suspend fun Link.search(query: String, vararg types: SearchType): SearchResult = node.search(query, *types)

/**
* Searches for [query].
*
* @param types the allowed [SearchTypes][SearchType]
* @see SearchResult
*/
public suspend fun Link.search(query: String, types: Iterable<SearchType>): SearchResult = node.search(query, types)
8 changes: 8 additions & 0 deletions plugins/lavasearch/src/commonMain/kotlin/rest/Route.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.schlaubi.lavakord.plugins.lavasearch.rest

import dev.schlaubi.lavakord.plugins.lavasearch.model.SearchType
import dev.schlaubi.lavakord.rest.routes.V4Api
import io.ktor.resources.*

@Resource("loadsearch")
internal data class LavaSearchRoute(val query: String, val types: List<SearchType>, val player: V4Api = V4Api())
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include(
":plugins:kspProcessor",
":plugins:sponsorblock",
":plugins:lavasrc",
":plugins:lavasearch",
"java",
"jda",
"jda-java"
Expand Down Expand Up @@ -54,6 +55,7 @@ dependencyResolutionManagement {
library("kotlinx-nodejs", "org.jetbrains.kotlin-wrappers", "kotlin-node").version("18.16.12-pre.594")

library("lavalink-protocol", "dev.arbjerg.lavalink", "protocol").version("4.0.0-beta.5")
library("lavasearch-protocol", "com.github.topi314.lavasearch", "lavasearch-protocol").version("1.0.0-beta.3")

library("kotlinpoet", "com.squareup", "kotlinpoet-ksp")
.version("1.14.2")
Expand Down

0 comments on commit 521b9c6

Please sign in to comment.