fix: Fix alias not being registered for group subcommands
This commit is contained in:
parent
513959d4a7
commit
b22cf439ad
5 changed files with 44 additions and 7 deletions
|
@ -16,7 +16,9 @@ val generalModule = botModules("General") {
|
||||||
groups("group") {
|
groups("group") {
|
||||||
commands("test", arguments = ::Test2Arguments) { ctx, args -> ctx.reply("Hello world ${args.user}") }
|
commands("test", arguments = ::Test2Arguments) { ctx, args -> ctx.reply("Hello world ${args.user}") }
|
||||||
groups("group") {
|
groups("group") {
|
||||||
commands("test", arguments = ::TestArguments) { ctx, args -> ctx.reply("Hello world ${args.test}") }
|
commands("test", alias = setOf("t")) { ctx ->
|
||||||
|
ctx.reply("Hello world ${ctx.command?.module?.name}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package io.github.null2264.tsukumogami.core.commands
|
package io.github.null2264.tsukumogami.core.commands
|
||||||
|
|
||||||
import io.github.null2264.tsukumogami.core.Context
|
import io.github.null2264.tsukumogami.core.Context
|
||||||
|
import io.github.null2264.tsukumogami.core.module.BotModule
|
||||||
import kotlin.reflect.KFunction
|
import kotlin.reflect.KFunction
|
||||||
|
|
||||||
open class Command(
|
open class Command(
|
||||||
|
@ -11,6 +12,22 @@ open class Command(
|
||||||
private val handler: suspend (Context, Arguments) -> Unit,
|
private val handler: suspend (Context, Arguments) -> Unit,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return parent command if this command is a subcommand otherwise it returns null
|
||||||
|
*/
|
||||||
|
var parent: Command? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The module this command belong to
|
||||||
|
*/
|
||||||
|
var module: BotModule? = null
|
||||||
|
get() {
|
||||||
|
if (parent != null) {
|
||||||
|
field = parent?.module
|
||||||
|
}
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
|
||||||
open suspend fun invoke(context: Context) {
|
open suspend fun invoke(context: Context) {
|
||||||
val parsedArguments = arguments.call()
|
val parsedArguments = arguments.call()
|
||||||
parsedArguments.parse(context)
|
parsedArguments.parse(context)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package io.github.null2264.tsukumogami.core.commands
|
package io.github.null2264.tsukumogami.core.commands
|
||||||
|
|
||||||
import io.github.null2264.tsukumogami.core.Context
|
import io.github.null2264.tsukumogami.core.Context
|
||||||
|
import io.github.null2264.tsukumogami.core.annotation.TsukumogamiInternalApi
|
||||||
|
|
||||||
|
@OptIn(TsukumogamiInternalApi::class)
|
||||||
class Group(
|
class Group(
|
||||||
name: String,
|
name: String,
|
||||||
alias: Set<String>,
|
alias: Set<String>,
|
||||||
|
@ -18,6 +20,11 @@ class Group(
|
||||||
|
|
||||||
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
||||||
|
|
||||||
|
override fun _addCommand(command: Command) {
|
||||||
|
command.parent = this
|
||||||
|
super._addCommand(command)
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun invoke(context: Context) {
|
override suspend fun invoke(context: Context) {
|
||||||
val subcommandName = context.message.content
|
val subcommandName = context.message.content
|
||||||
.substringAfter("${context.prefix}${context.invokedWith}")
|
.substringAfter("${context.prefix}${context.invokedWith}")
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
package io.github.null2264.tsukumogami.core.commands
|
package io.github.null2264.tsukumogami.core.commands
|
||||||
|
|
||||||
import io.github.null2264.tsukumogami.core.Context
|
import io.github.null2264.tsukumogami.core.Context
|
||||||
|
import io.github.null2264.tsukumogami.core.annotation.TsukumogamiInternalApi
|
||||||
import kotlin.reflect.KFunction
|
import kotlin.reflect.KFunction
|
||||||
|
|
||||||
|
@OptIn(TsukumogamiInternalApi::class)
|
||||||
interface IGroup {
|
interface IGroup {
|
||||||
|
|
||||||
val allCommands: MutableMap<String, Command>
|
val allCommands: MutableMap<String, Command>
|
||||||
|
|
||||||
private fun addCommand(command: Command) {
|
@TsukumogamiInternalApi
|
||||||
|
fun _addCommand(command: Command) {
|
||||||
allCommands[command.name] = command
|
allCommands[command.name] = command
|
||||||
|
command.alias.forEach { allCommands[it] = command }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun commands(
|
fun commands(
|
||||||
|
@ -18,7 +22,7 @@ interface IGroup {
|
||||||
handler: suspend (Context) -> Unit,
|
handler: suspend (Context) -> Unit,
|
||||||
) {
|
) {
|
||||||
val command = Command(name, alias, description, ::EmptyArguments) { ctx, _ -> handler(ctx) }
|
val command = Command(name, alias, description, ::EmptyArguments) { ctx, _ -> handler(ctx) }
|
||||||
addCommand(command)
|
_addCommand(command)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <Args : Arguments> commands(
|
fun <Args : Arguments> commands(
|
||||||
|
@ -29,7 +33,7 @@ interface IGroup {
|
||||||
handler: suspend (Context, Args) -> Unit,
|
handler: suspend (Context, Args) -> Unit,
|
||||||
) {
|
) {
|
||||||
val command = Command(name, alias, description, arguments) { ctx, args -> handler(ctx, args as Args) }
|
val command = Command(name, alias, description, arguments) { ctx, args -> handler(ctx, args as Args) }
|
||||||
addCommand(command)
|
_addCommand(command)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun groups(
|
fun groups(
|
||||||
|
@ -40,6 +44,6 @@ interface IGroup {
|
||||||
) {
|
) {
|
||||||
val group = Group(name, alias, description)
|
val group = Group(name, alias, description)
|
||||||
declaration(group)
|
declaration(group)
|
||||||
addCommand(group)
|
_addCommand(group)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,27 @@
|
||||||
package io.github.null2264.tsukumogami.core.module
|
package io.github.null2264.tsukumogami.core.module
|
||||||
|
|
||||||
import io.github.null2264.tsukumogami.core.Bot
|
import io.github.null2264.tsukumogami.core.Bot
|
||||||
|
import io.github.null2264.tsukumogami.core.annotation.TsukumogamiInternalApi
|
||||||
import io.github.null2264.tsukumogami.core.commands.Command
|
import io.github.null2264.tsukumogami.core.commands.Command
|
||||||
import io.github.null2264.tsukumogami.core.commands.IGroup
|
import io.github.null2264.tsukumogami.core.commands.IGroup
|
||||||
|
|
||||||
|
@OptIn(TsukumogamiInternalApi::class)
|
||||||
open class BotModule constructor(val name: String) : IGroup {
|
open class BotModule constructor(val name: String) : IGroup {
|
||||||
|
|
||||||
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
||||||
|
|
||||||
|
override fun _addCommand(command: Command) {
|
||||||
|
command.module = this
|
||||||
|
super._addCommand(command)
|
||||||
|
}
|
||||||
|
|
||||||
internal fun install(bot: Bot): BotModule {
|
internal fun install(bot: Bot): BotModule {
|
||||||
allCommands.values.forEach(bot::addCommand)
|
allCommands.values.distinctBy { it.name }.forEach(bot::addCommand)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun uninstall(bot: Bot): BotModule {
|
internal fun uninstall(bot: Bot): BotModule {
|
||||||
allCommands.values.forEach(bot::removeCommand)
|
allCommands.values.distinctBy { it.name }.forEach(bot::removeCommand)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue