Skip to content

Commit

Permalink
Update to Kordex 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 5, 2024
1 parent f29c444 commit 205229a
Show file tree
Hide file tree
Showing 28 changed files with 184 additions and 151 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

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

Binary file removed .idea/misc.xml
Binary file not shown.
20 changes: 10 additions & 10 deletions app/desktop/uwp_helper/Cargo.lock

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

5 changes: 4 additions & 1 deletion app/desktop/uwp_helper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ tasks {

compileJava {
dependsOn(generateBindingsWithJextract)
options.compilerArgs.add("--enable-preview")
}
}

java {
sourceCompatibility = JavaVersion.VERSION_22
}
4 changes: 0 additions & 4 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dev.schlaubi.tonbrett.gradle.androidSdk
import dev.schlaubi.tonbrett.gradle.apiUrl
import org.gradle.configurationcache.extensions.capitalized
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
Expand All @@ -11,9 +10,6 @@ plugins {
alias(libs.plugins.ksp)
}

// TODO: https://github.com/google/ksp/issues/1823
project.setProperty("ksp.useKSP2", false)

@OptIn(ExperimentalKotlinGradlePluginApi::class)
kotlin {
applyDefaultHierarchyTemplate {
Expand Down
11 changes: 6 additions & 5 deletions bot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dev.schlaubi.mikbot.gradle.mikbot
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import java.util.Locale

plugins {
alias(libs.plugins.ksp)
Expand Down Expand Up @@ -40,13 +39,15 @@ mikbotPlugin {
pluginId = "tonbrett"
license = "MIT"
enableKordexProcessor = true
}

tasks {
generateDefaultTranslationBundle {
defaultLocale = Locale.Builder().setLanguage("en").setRegion("US").build()
i18n {
classPackage = "dev.schlaubi.tonbrett.bot.translations"
translationBundle = "soundboard"
className = "SoundboardTranslations"
}
}

tasks {
val buildWebApp = register<Copy>("buildWebApp") {
val webApp = project(":app:web")
from(webApp.tasks.named("jsBrowserDistribution"))
Expand Down
18 changes: 10 additions & 8 deletions bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

package dev.schlaubi.tonbrett.bot

import com.kotlindiscord.kord.extensions.builders.ExtensibleBotBuilder
import com.kotlindiscord.kord.extensions.extensions.primaryEntryPointCommand
import com.kotlindiscord.kord.extensions.modules.unsafe.annotations.UnsafeAPI
import dev.kordex.core.extensions.primaryEntryPointCommand
import dev.kord.common.entity.PrimaryEntryPointCommandHandlerType
import dev.kord.gateway.Intent
import dev.kordex.core.builders.ExtensionsBuilder
import dev.kordex.core.i18n.toKey
import dev.kordex.core.i18n.types.Key
import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI
import dev.schlaubi.mikbot.plugin.api.Plugin
import dev.schlaubi.mikbot.plugin.api.PluginContext
import dev.schlaubi.mikbot.plugin.api.PluginMain
import dev.schlaubi.mikbot.plugin.api.module.SubCommandModule
import dev.schlaubi.tonbrett.bot.commands.*
import dev.schlaubi.tonbrett.bot.core.VoiceStateWatcher
import dev.schlaubi.tonbrett.bot.translations.SoundboardTranslations
import dev.schlaubi.tonbrett.common.TonbrettSerializersModule
import org.litote.kmongo.serialization.registerModule

Expand All @@ -22,16 +25,15 @@ class Plugin(context: PluginContext) : Plugin(context) {
registerModule(TonbrettSerializersModule)
}

override fun ExtensibleBotBuilder.ExtensionsBuilder.addExtensions() {
override fun ExtensionsBuilder.addExtensions() {
add(::Module)
add(::VoiceStateWatcher)
}
}

private class Module(context: PluginContext) : SubCommandModule(context) {
override val name: String = "tonbrett"
override val bundle: String = "soundboard"
override val commandName: String = "sound"
override val commandName: Key = SoundboardTranslations.Commands.Sound.name
override suspend fun overrideSetup() {
intents.add(Intent.GuildVoiceStates)
addCommand()
Expand All @@ -41,8 +43,8 @@ private class Module(context: PluginContext) : SubCommandModule(context) {
playCommand()

primaryEntryPointCommand {
name = "Tonbrett"
description = "commands.entry_point.description"
name = "Tonbrett".toKey()
description = SoundboardTranslations.Commands.EntryPoint.description

handler = PrimaryEntryPointCommandHandlerType.DiscordLaunchActivity
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package dev.schlaubi.tonbrett.bot.command

import com.kotlindiscord.kord.extensions.annotations.UnexpectedFunctionBehaviour
import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.CommandContext
import com.kotlindiscord.kord.extensions.commands.converters.Converter
import com.kotlindiscord.kord.extensions.commands.converters.ConverterToOptional
import com.kotlindiscord.kord.extensions.commands.converters.OptionalCoalescingConverter
import com.kotlindiscord.kord.extensions.commands.converters.builders.ConverterBuilder
import com.kotlindiscord.kord.extensions.commands.converters.builders.OptionalCoalescingConverterBuilder
import com.kotlindiscord.kord.extensions.commands.converters.impl.emoji
import com.kotlindiscord.kord.extensions.commands.converters.impl.string
import com.kotlindiscord.kord.extensions.modules.unsafe.annotations.UnsafeAPI
import com.kotlindiscord.kord.extensions.modules.unsafe.converters.union
import dev.kord.core.entity.GuildEmoji
import dev.kord.core.entity.StandardEmoji
import dev.kord.x.emoji.Emojis
import dev.kordex.core.annotations.UnexpectedFunctionBehaviour
import dev.kordex.core.commands.Arguments
import dev.kordex.core.commands.converters.Converter
import dev.kordex.core.commands.converters.ConverterToOptional
import dev.kordex.core.commands.converters.OptionalCoalescingConverter
import dev.kordex.core.commands.converters.builders.ConverterBuilder
import dev.kordex.core.commands.converters.builders.OptionalCoalescingConverterBuilder
import dev.kordex.core.commands.converters.impl.emoji
import dev.kordex.core.commands.converters.impl.string
import dev.kordex.core.i18n.types.Key
import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI
import dev.kordex.modules.dev.unsafe.converters.union
import dev.schlaubi.tonbrett.bot.translations.SoundboardTranslations
import dev.schlaubi.tonbrett.common.Sound

@OptIn(UnsafeAPI::class, ConverterToOptional::class, UnexpectedFunctionBehaviour::class)
fun Arguments.emoji(name: String, description: String): OptionalCoalescingConverter<Any> {
fun Arguments.emoji(name: Key, description: Key): OptionalCoalescingConverter<Any> {
fun ConverterBuilder<*>.applyName() {
this.name = name
this.description = description
Expand All @@ -31,7 +32,7 @@ fun Arguments.emoji(name: String, description: String): OptionalCoalescingConver

validate {
if (Emojis[value] == null) {
fail(translate("arguments.emoji.invalid"))
fail(SoundboardTranslations.Arguments.Emoji.invalid)
}
}
}
Expand All @@ -41,7 +42,6 @@ fun Arguments.emoji(name: String, description: String): OptionalCoalescingConver
return arg(name, description, brokenConverter.toOptional().withBuilder(DummyBuilder))
}

context(CommandContext)
fun Any.toEmoji(): Sound.Emoji = when (this) {
is String -> Sound.DiscordEmoji(this)
is GuildEmoji -> Sound.GuildEmoji(id, isAnimated)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package dev.schlaubi.tonbrett.bot.command

import com.kotlindiscord.kord.extensions.DiscordRelayedException
import com.kotlindiscord.kord.extensions.commands.Argument
import com.kotlindiscord.kord.extensions.commands.CommandContext
import com.kotlindiscord.kord.extensions.commands.converters.SingleConverter
import com.kotlindiscord.kord.extensions.commands.converters.Validator
import com.kotlindiscord.kord.extensions.commands.converters.builders.ConverterBuilder
import com.kotlindiscord.kord.extensions.modules.annotations.converters.Converter
import com.kotlindiscord.kord.extensions.modules.annotations.converters.ConverterType
import com.kotlindiscord.kord.extensions.parser.StringParser
import dev.kord.core.behavior.interaction.suggestString
import dev.kord.core.entity.interaction.AutoCompleteInteraction
import dev.kord.core.entity.interaction.OptionValue
import dev.kord.core.entity.interaction.StringOptionValue
import dev.kord.rest.builder.interaction.OptionsBuilder
import dev.kord.rest.builder.interaction.StringChoiceBuilder
import dev.kordex.core.DiscordRelayedException
import dev.kordex.core.annotations.InternalAPI
import dev.kordex.core.annotations.converters.Converter
import dev.kordex.core.annotations.converters.ConverterType
import dev.kordex.core.commands.Argument
import dev.kordex.core.commands.CommandContext
import dev.kordex.core.commands.OptionWrapper
import dev.kordex.core.commands.converters.SingleConverter
import dev.kordex.core.commands.converters.Validator
import dev.kordex.core.commands.converters.builders.ConverterBuilder
import dev.kordex.core.i18n.EMPTY_KEY
import dev.kordex.core.i18n.types.Key
import dev.kordex.parser.StringParser
import dev.schlaubi.mikbot.plugin.api.util.safeInput
import dev.schlaubi.tonbrett.bot.io.SoundBoardDatabase
import dev.schlaubi.tonbrett.bot.io.search
import dev.schlaubi.tonbrett.bot.translations.SoundboardTranslations
import dev.schlaubi.tonbrett.common.Sound
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.take
Expand All @@ -32,7 +36,7 @@ private const val idPrefix = "id:"
types = [ConverterType.SINGLE]
)
class SoundConverter(validator: Validator<Sound>) : SingleConverter<Sound>(validator) {
override val signatureTypeString: String = "Sound"
override val signatureType: Key = EMPTY_KEY
override fun withBuilder(builder: ConverterBuilder<Sound>): SingleConverter<Sound> {
val builderWithAutoComplete = builder.apply {
autoComplete { onAutoComplete() }
Expand Down Expand Up @@ -69,7 +73,11 @@ class SoundConverter(validator: Validator<Sound>) : SingleConverter<Sound>(valid
parsed = foundByName
} else {
handleError(
DiscordRelayedException(context.translate("commands.generic.not_found", arrayOf(text))),
DiscordRelayedException(
SoundboardTranslations.Commands.Generic.notFound.withOrdinalPlaceholders(
text
)
),
context
)
}
Expand All @@ -78,8 +86,9 @@ class SoundConverter(validator: Validator<Sound>) : SingleConverter<Sound>(valid
return true
}

override suspend fun toSlashOption(arg: Argument<*>): OptionsBuilder =
StringChoiceBuilder(arg.displayName, arg.description).apply { required = true }
@OptIn(InternalAPI::class)
override suspend fun toSlashOption(arg: Argument<*>): OptionWrapper<*> =
OptionWrapper(arg.displayName, arg.description, { required = true }, StringChoiceBuilder::class)

private suspend fun AutoCompleteInteraction.onAutoComplete() {
val input = focusedOption.safeInput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package dev.schlaubi.tonbrett.bot.command

import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString
import dev.kord.core.behavior.interaction.suggestString
import dev.kordex.core.commands.Arguments
import dev.kordex.core.commands.converters.impl.optionalString
import dev.kordex.core.i18n.types.Key
import dev.schlaubi.mikbot.plugin.api.util.safeInput
import dev.schlaubi.tonbrett.bot.io.SoundBoardDatabase
import dev.schlaubi.tonbrett.bot.io.findAllTags
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.toList

fun Arguments.tagArgument(name: String, description: String) = optionalString {
fun Arguments.tagArgument(name: Key, description: Key) = optionalString {
this.name = name
this.description = description

Expand Down
Loading

0 comments on commit 205229a

Please sign in to comment.