Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
- Update to kotlinx.serialization 1.7.1
- Update to ksp 2.0.0-1.0.22
- Fix default translation bundle for music plugin
- Remove Launcher workaround
- Add support for default locale at project level in Gradle plugin
- Fix invalid translations
- Reuse code from queuing system in music channel
  • Loading branch information
DRSchlaubi committed Jul 1, 2024
1 parent a26fe8b commit f3c6be4
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.schlaubi.mikbot.plugin.api.util

import com.kotlindiscord.kord.extensions.commands.CommandContext
import dev.kord.core.behavior.MessageBehavior
import dev.kord.core.behavior.interaction.followup.FollowupMessageBehavior
import dev.kord.rest.builder.message.create.MessageCreateBuilder
import dev.kord.rest.builder.message.modify.MessageModifyBuilder
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
import dev.schlaubi.mikbot.gradle.addRepositories
import java.util.Locale

plugins {
dev.schlaubi.mikbot.`gradle-plugin`
Expand All @@ -15,6 +16,7 @@ subprojects {
mikbotPlugin {
license = "MIT License"
provider = "Mikbot Official Plugins"
defaultLocale = Locale.Builder().setLanguageTag("en-GB").build()
}

pluginPublishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ private val Project.pluginMainFile: Provider<RegularFile>
internal data class AssemblyTask(val assembleTask: TaskProvider<Zip>, val installBotTask: InstallBotTask)

context(Project)
internal fun TaskContainer.createAssembleTasks(): AssemblyTask {
internal fun TaskContainer.createAssembleTasks(generateDefaultTranslationBundle: TaskProvider<GenerateDefaultTranslationBundleTask>): AssemblyTask {
val patchPropertiesTask = createPatchPropertiesTask()

val jar = tasks.findByName("jar") as Jar? ?: pluginNotAppliedError("Kotlin")
jar.dependsOn(patchPropertiesTask)
jar.dependsOn(patchPropertiesTask, generateDefaultTranslationBundle)
val assembleTask = createAssemblePluginTask(jar)
val installBotTask = tasks.create("installBot", InstallBotTask::class.java)
createAssembleBotTask(assembleTask, installBotTask)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ abstract class GenerateDefaultTranslationBundleTask : DefaultTask() {
val to = project.layout.buildDirectory.dir("generated/mikbot/main/resources")

val result = project.copy {
val locale = defaultLocale.orNull?.resourceBundleKey
?: error("Please specify a default locale")
from(from)
into(to)

bundles.get().ifEmpty { listOf("strings") }.forEach {
val name = "${it}_${locale}.properties"
include("translations/${project.bundle}/$name")
rename(name, "${it}.properties")
}
val locale = defaultLocale.orNull?.resourceBundleKey
?: error("Please specify a default locale")

from(from)
into(to)

bundles.get().ifEmpty { listOf("strings") }.forEach {
val name = "${it}_${locale}.properties"
include("translations/${project.bundle}/$name")
rename(name, "${it}.properties")
}
}
didWork = result.didWork
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.gradle.api.Project
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Zip
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.*

@Suppress("unused")
Expand Down Expand Up @@ -35,7 +34,10 @@ class MikBotPluginGradlePlugin : Plugin<Project> {
}

private fun Project.configureTasks() {
val (assemblePlugin, installBotTask) = tasks.createAssembleTasks()
val generateDefaultTranslationBundle by tasks.registering(GenerateDefaultTranslationBundleTask::class) {
defaultLocale = mikbotPluginExtension.defaultLocale
}
val (assemblePlugin, installBotTask) = tasks.createAssembleTasks(generateDefaultTranslationBundle)
createTestBotTasks(assemblePlugin, installBotTask)
createPublishingTasks(assemblePlugin)
}
Expand All @@ -45,7 +47,7 @@ class MikBotPluginGradlePlugin : Plugin<Project> {
named("main") {
java {
val optionalKspSourceSet = mikbotPluginExtension.enableKordexProcessor.map {
if(it) {
if (it) {
layout.buildDirectory.dir("/generated/ksp/main/kotlin/")
} else {
fileTree("never_exists") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.schlaubi.mikbot.gradle.extension
import org.gradle.api.Project
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import java.util.Locale

/** Extension for configuring plugins for PF4J. */
abstract class PluginExtension {
Expand Down Expand Up @@ -51,6 +52,11 @@ abstract class PluginExtension {
* See documentation [here](https://github.com/Kord-Extensions/kord-extensions/tree/root/annotations/src/main/kotlin/com/kotlindiscord/kord/extensions/modules/annotations/converters)
*/
abstract val enableKordexProcessor: Property<Boolean>

/**
* The default locale.
*/
abstract val defaultLocale: Property<Locale>
}

internal val Project.pluginId: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.schlaubi.mikbot.gradle.BuildRepositoryExtension
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.plugins.ExtensionContainer
import java.util.*

const val pluginExtensionName = "mikbotPlugin"

Expand Down Expand Up @@ -34,6 +35,7 @@ fun Project.createExtensions() {
enableKordexProcessor
.convention(base.enableKordexProcessor.convention(false))
.convention(false)
defaultLocale.convention(base.defaultLocale.convention(Locale.ENGLISH))
}
}
extensions.create<BuildRepositoryExtension>(pluginPublishingExtensionName).apply {
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ kotlin = "2.0.0"
kordex = "1.8.1-SNAPSHOT"
kmongo = "5.1.0"
coroutines = "1.8.1"
serialization = "1.7.0-RC"
serialization = "1.7.1"
ktor = "2.3.11"
kord = "0.14.0"
api = "3.33.1"
ksp = "2.0.0-1.0.21"
api = "3.33.2"
ksp = "2.0.0-1.0.22"
lavakord = "7.0.1"

[libraries]
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.8.5-SNAPSHOT"
version = "3.8.6-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.schlaubi.mikmusic.checks.joinSameChannelCheck
import dev.schlaubi.mikmusic.core.MusicModule
import dev.schlaubi.mikmusic.player.MusicPlayer
import dev.schlaubi.mikmusic.player.queue.QueueOptions
import dev.schlaubi.mikmusic.player.queue.SearchQuery
import dev.schlaubi.mikmusic.player.queue.queueTracks

const val playActionName = "Play as track"
Expand All @@ -21,24 +22,14 @@ suspend fun MusicModule.playMessageAction() = ephemeralMessageCommand {
action {
val query = event.interaction.messages.values.first().attachmentOrContentQuery

val arguments = PlayMessageActionArguments(query)
val arguments = SearchQuery(query)

queue(arguments, musicPlayer)
}
}

class PlayMessageActionArguments(override val query: String) : QueueOptions {
override val force: Boolean = false
override val top: Boolean = false
override val searchProvider: QueueOptions.SearchProvider? = null
override val shuffle: Boolean? = null
override val loop: Boolean? = null
override val loopQueue: Boolean? = null

}

private suspend fun EphemeralMessageCommandContext<*>.queue(
arguments: PlayMessageActionArguments,
arguments: SearchQuery,
musicPlayer: MusicPlayer
) = queueTracks(musicPlayer, true, arguments, { respond { it() } }) {
editingPaginator { it() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ suspend fun SettingsModule.fixMusicChannel() = ephemeralSlashCommand {
musicModule.getMusicPlayer(safeGuild).updateMusicChannelMessage()

respond {
content = translate("commands.fix_music_channel.success")
content = translate("commands.fix_music_channel.success", "music")
}
}
}
1 change: 1 addition & 0 deletions music/player/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ testing {
mikbotPlugin {
pluginId = "music-player"
description = "Plugin providing full music functionality for the bot"
bundle = "music"
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@ package dev.schlaubi.mikmusic.musicchannel
import com.kotlindiscord.kord.extensions.checks.guildFor
import com.kotlindiscord.kord.extensions.checks.inChannel
import com.kotlindiscord.kord.extensions.extensions.event
import dev.arbjerg.lavalink.protocol.v4.LoadResult
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.kord.core.behavior.channel.withTyping
import dev.kord.core.behavior.interaction.response.EphemeralMessageInteractionResponseBehavior
import dev.kord.core.behavior.interaction.response.createEphemeralFollowup
import dev.kord.core.behavior.reply
import dev.kord.core.event.interaction.ComponentInteractionCreateEvent
import dev.kord.core.event.message.MessageCreateEvent
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.rest.loadItem
import dev.schlaubi.mikbot.plugin.api.PluginContext
import dev.schlaubi.mikbot.plugin.api.module.MikBotModule
import dev.schlaubi.mikbot.plugin.api.util.*
import dev.schlaubi.mikmusic.checks.joinSameChannelCheck
import dev.schlaubi.mikmusic.checks.musicControlCheck
import dev.schlaubi.mikmusic.core.Config
import dev.schlaubi.mikmusic.core.MusicModule
import dev.schlaubi.mikmusic.core.checkOtherSchedulerOptions
import dev.schlaubi.mikmusic.core.settings.MusicSettingsDatabase
import dev.schlaubi.mikmusic.player.MusicPlayer
import dev.schlaubi.mikmusic.player.enableAutoPlay
import dev.schlaubi.mikmusic.player.queue.takeFirstMatch
import dev.schlaubi.mikmusic.player.resetAutoPlay
import dev.schlaubi.mikmusic.util.mapToQueuedTrack
import kotlin.reflect.KMutableProperty1

class MusicInteractionModule(context: PluginContext) : MikBotModule(context) {
override val name = "music interaction handler"
override val bundle: String = "music"
val musicModule: MusicModule by extension()

override suspend fun setup() {
Expand Down Expand Up @@ -57,7 +54,7 @@ class MusicInteractionModule(context: PluginContext) : MikBotModule(context) {

suspend fun updateSchedulerOptions(
myProperty: KMutableProperty1<MusicPlayer, Boolean>,
vararg properties: KMutableProperty1<MusicPlayer, Boolean>
vararg properties: KMutableProperty1<MusicPlayer, Boolean>,
) {
ack.updateSchedulerOptions(
player,
Expand All @@ -75,14 +72,17 @@ class MusicInteractionModule(context: PluginContext) : MikBotModule(context) {
MusicPlayer::loopQueue,
MusicPlayer::shuffle, MusicPlayer::repeat
)

repeatOne -> updateSchedulerOptions(
MusicPlayer::repeat,
MusicPlayer::loopQueue, MusicPlayer::shuffle
)

shuffle -> updateSchedulerOptions(
MusicPlayer::shuffle,
MusicPlayer::loopQueue, MusicPlayer::repeat
)

autoPlay -> {
if (player.autoPlay == null) {
player.enableAutoPlay()
Expand Down Expand Up @@ -122,19 +122,13 @@ class MusicInteractionModule(context: PluginContext) : MikBotModule(context) {
event.message.channel.withTyping {
val guild = event.getGuildOrNull() ?: error("Could not find guild")
val player = musicModule.getMusicPlayer(guild)
val tracks = player.takeFirstMatch(event.message.content)

if (tracks.isEmpty()) {
event.message
.reply { content = translate("music.queue.no_matches", "music") }
.deleteAfterwards()
} else {
player.queueTrack(
force = false,
onTop = false,
tracks = tracks.mapToQueuedTrack(event.message.author!!)
)
}
val track = takeFirstMatch(player, event.message.content) { event.message.reply { it() } }
?: return@withTyping
player.queueTrack(
force = false,
onTop = false,
tracks = track.tracks.mapToQueuedTrack(event.message.author!!)
)

event.message.delete("Music channel interaction")
}
Expand All @@ -143,27 +137,11 @@ class MusicInteractionModule(context: PluginContext) : MikBotModule(context) {
}
}

suspend fun Link.takeFirstMatch(query: String): List<Track> {
val isUrl = query.startsWith("http")
val queryString = if (isUrl) {
query
} else {
"${Config.DEFAULT_SEARCH_PROVIDER}: $query"
}

return when (val result = loadItem(queryString)) {
is LoadResult.TrackLoaded -> listOf(result.data)
is LoadResult.PlaylistLoaded -> result.data.tracks
is LoadResult.SearchResult -> result.data.tracks.take(1)
else -> emptyList()
}
}

private suspend fun EphemeralMessageInteractionResponseBehavior.updateSchedulerOptions(
musicPlayer: MusicPlayer,
translate: Translator,
myProperty: KMutableProperty1<MusicPlayer, Boolean>,
vararg properties: KMutableProperty1<MusicPlayer, Boolean>
vararg properties: KMutableProperty1<MusicPlayer, Boolean>,
) = checkOtherSchedulerOptions(
musicPlayer,
translate,
Expand All @@ -179,7 +157,7 @@ private suspend fun EphemeralMessageInteractionResponseBehavior.updateSchedulerO

private suspend fun EphemeralMessageInteractionResponseBehavior.confirmation(
messageBuilder: MessageBuilder,
translate: Translator
translate: Translator,
): Confirmation = confirmation(
{
createEphemeralFollowup {
Expand Down
Loading

0 comments on commit f3c6be4

Please sign in to comment.