feat: URL and URI converter
This commit is contained in:
parent
2e3c3cdd66
commit
fc9c4ff84a
9 changed files with 92 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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? =
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue