feat: URL and URI converter

This commit is contained in:
Ahmad Ansori Palembani 2025-05-26 12:43:15 +07:00
parent 2e3c3cdd66
commit fc9c4ff84a
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
9 changed files with 92 additions and 13 deletions

View file

@ -2,6 +2,7 @@ package io.github.null2264.tsukumogami.bot.core.module
import dev.kord.core.entity.effectiveName
import io.github.null2264.tsukumogami.bot.core.module.arguments.Test2Arguments
import io.github.null2264.tsukumogami.bot.core.module.arguments.Test3Arguments
import io.github.null2264.tsukumogami.bot.core.module.arguments.TestArguments
import io.github.null2264.tsukumogami.core.module.api.botModules
import kotlinx.datetime.Clock
@ -17,9 +18,8 @@ val generalModule = botModules("General") {
groups("group") {
commands("test", arguments = ::Test2Arguments) { ctx, args -> ctx.reply("Hello world ${args.user}") }
groups("group") {
commands("test", alias = setOf("t")) { ctx ->
ctx.reply("Hello world ${ctx.command?.module?.name}")
ctx.reply("Bot: ${ctx.bot.self().effectiveName}")
commands("test", alias = setOf("t"), arguments = ::Test3Arguments) { ctx, args ->
ctx.reply("URL: ${args.url}")
}
}
}

View file

@ -2,6 +2,7 @@ package io.github.null2264.tsukumogami.bot.core.module.arguments
import io.github.null2264.tsukumogami.core.commands.Arguments
import io.github.null2264.tsukumogami.core.commands.ext.string
import io.github.null2264.tsukumogami.core.commands.ext.url
import io.github.null2264.tsukumogami.core.commands.ext.user
class TestArguments : Arguments() {
@ -15,3 +16,6 @@ class TestArguments : Arguments() {
class Test2Arguments : Arguments() {
val user by user("User")
}
class Test3Arguments : Arguments() {
val url by url("Link")
}

View file

@ -13,8 +13,7 @@ abstract class Converter<OutputType: Any?> {
abstract var parsed: OutputType
// FIXME: Maybe change the output to a boolean, indicates whether or not it successfully consume the inputs
abstract suspend fun consume(context: Context, inputs: MutableList<String>): OutputType
abstract suspend fun consume(context: Context, inputs: MutableList<String>): Boolean
operator fun getValue(thisRef: Arguments, property: KProperty<*>): OutputType {
return this.parsed

View file

@ -7,7 +7,7 @@ class StringConverter : Converter<String>() {
override var parsed: String = ""
override suspend fun consume(context: Context, inputs: MutableList<String>): String {
override suspend fun consume(context: Context, inputs: MutableList<String>): Boolean {
this.parsed = if (isGreedy && inputs.size > 1) run {
val limit = inputs.size
var buffer = ""
@ -20,6 +20,6 @@ class StringConverter : Converter<String>() {
}
buffer
} else inputs.removeFirstOrNull().orEmpty()
return this.parsed
return this.parsed.isNotEmpty()
}
}

View file

@ -0,0 +1,25 @@
package io.github.null2264.tsukumogami.core.commands.converters.impl
import io.github.null2264.tsukumogami.core.Context
import io.github.null2264.tsukumogami.core.commands.converters.Converter
import io.github.null2264.tsukumogami.core.exceptions.CommandException
import java.net.URI
class URIConverter : Converter<URI>() {
override var isGreedy: Boolean
get() = false
set(_) { error("HttpConverter can't be greedy") }
override lateinit var parsed: URI
override suspend fun consume(context: Context, inputs: MutableList<String>): Boolean {
val input = inputs.getOrNull(0) ?: return false
this.parsed = try {
URI(input)
} catch (e: Exception) {
throw CommandException("Failed to parse URI", e)
}
return true
}
}

View file

@ -0,0 +1,26 @@
package io.github.null2264.tsukumogami.core.commands.converters.impl
import io.github.null2264.tsukumogami.core.Context
import io.github.null2264.tsukumogami.core.commands.converters.Converter
import io.github.null2264.tsukumogami.core.exceptions.CommandException
import java.net.URI
import java.net.URL
class URLConverter : Converter<URL>() {
override var isGreedy: Boolean
get() = false
set(_) { error("HttpConverter can't be greedy") }
override lateinit var parsed: URL
override suspend fun consume(context: Context, inputs: MutableList<String>): Boolean {
val input = inputs.getOrNull(0) ?: return false
this.parsed = try {
URL(input)
} catch (e: Exception) {
throw CommandException("Failed to parse URL", e)
}
return true
}
}

View file

@ -16,25 +16,28 @@ class UserConverter : Converter<User>() {
override lateinit var parsed: User
override suspend fun consume(context: Context, inputs: MutableList<String>): User {
val input = inputs.removeFirstOrNull() ?: throw CommandException("User ID is null")
override suspend fun consume(context: Context, inputs: MutableList<String>): Boolean {
val input = inputs.getOrNull(0) ?: throw CommandException("User ID is null")
if (input.equals("me", true)) {
val user = context.author
if (user != null) {
this.parsed = user
return this.parsed
inputs.removeAt(0)
return true
}
}
if (input.equals("you", true)) {
this.parsed = context.bot.client.getSelf()
return this.parsed
inputs.removeAt(0)
return true
}
this.parsed = context.findUser(input) ?:
throw CommandException("User not found")
return this.parsed
inputs.removeAt(0)
return true
}
private suspend fun Context.findUser(arg: String): User? =

View file

@ -4,7 +4,11 @@ import dev.kord.core.entity.User
import io.github.null2264.tsukumogami.core.commands.Arguments
import io.github.null2264.tsukumogami.core.commands.converters.Converter
import io.github.null2264.tsukumogami.core.commands.converters.impl.StringConverter
import io.github.null2264.tsukumogami.core.commands.converters.impl.URIConverter
import io.github.null2264.tsukumogami.core.commands.converters.impl.URLConverter
import io.github.null2264.tsukumogami.core.commands.converters.impl.UserConverter
import java.net.URI
import java.net.URL
fun Arguments.string(name: String, declaration: Converter<String>.() -> Unit = {}): Converter<String> {
val converter = StringConverter()
@ -17,3 +21,15 @@ fun Arguments.user(name: String, declaration: Converter<User>.() -> Unit = {}):
declaration(converter)
return args(name, converter)
}
fun Arguments.uri(name: String, declaration: Converter<URI>.() -> Unit = {}): Converter<URI> {
val converter = URIConverter()
declaration(converter)
return args(name, converter)
}
fun Arguments.url(name: String, declaration: Converter<URL>.() -> Unit = {}): Converter<URL> {
val converter = URLConverter()
declaration(converter)
return args(name, converter)
}

View file

@ -1,3 +1,9 @@
package io.github.null2264.tsukumogami.core.exceptions
open class CommandException(message: String = "Something went wrong while executing the command") : Exception(message)
open class CommandException(
message: String = "Something went wrong while executing the command",
/**
* Returns the original exception or null if this exception IS the original.
*/
original: Exception? = null,
) : Exception(message)