fix: Fix alias not being registered for group subcommands

This commit is contained in:
Ahmad Ansori Palembani 2025-05-23 15:29:16 +07:00
parent 513959d4a7
commit b22cf439ad
5 changed files with 44 additions and 7 deletions

View file

@ -16,7 +16,9 @@ val generalModule = botModules("General") {
groups("group") {
commands("test", arguments = ::Test2Arguments) { ctx, args -> ctx.reply("Hello world ${args.user}") }
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}")
}
}
}

View file

@ -1,6 +1,7 @@
package io.github.null2264.tsukumogami.core.commands
import io.github.null2264.tsukumogami.core.Context
import io.github.null2264.tsukumogami.core.module.BotModule
import kotlin.reflect.KFunction
open class Command(
@ -11,6 +12,22 @@ open class Command(
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) {
val parsedArguments = arguments.call()
parsedArguments.parse(context)

View file

@ -1,7 +1,9 @@
package io.github.null2264.tsukumogami.core.commands
import io.github.null2264.tsukumogami.core.Context
import io.github.null2264.tsukumogami.core.annotation.TsukumogamiInternalApi
@OptIn(TsukumogamiInternalApi::class)
class Group(
name: String,
alias: Set<String>,
@ -18,6 +20,11 @@ class Group(
override val allCommands: MutableMap<String, Command> = mutableMapOf()
override fun _addCommand(command: Command) {
command.parent = this
super._addCommand(command)
}
override suspend fun invoke(context: Context) {
val subcommandName = context.message.content
.substringAfter("${context.prefix}${context.invokedWith}")

View file

@ -1,14 +1,18 @@
package io.github.null2264.tsukumogami.core.commands
import io.github.null2264.tsukumogami.core.Context
import io.github.null2264.tsukumogami.core.annotation.TsukumogamiInternalApi
import kotlin.reflect.KFunction
@OptIn(TsukumogamiInternalApi::class)
interface IGroup {
val allCommands: MutableMap<String, Command>
private fun addCommand(command: Command) {
@TsukumogamiInternalApi
fun _addCommand(command: Command) {
allCommands[command.name] = command
command.alias.forEach { allCommands[it] = command }
}
fun commands(
@ -18,7 +22,7 @@ interface IGroup {
handler: suspend (Context) -> Unit,
) {
val command = Command(name, alias, description, ::EmptyArguments) { ctx, _ -> handler(ctx) }
addCommand(command)
_addCommand(command)
}
fun <Args : Arguments> commands(
@ -29,7 +33,7 @@ interface IGroup {
handler: suspend (Context, Args) -> Unit,
) {
val command = Command(name, alias, description, arguments) { ctx, args -> handler(ctx, args as Args) }
addCommand(command)
_addCommand(command)
}
fun groups(
@ -40,6 +44,6 @@ interface IGroup {
) {
val group = Group(name, alias, description)
declaration(group)
addCommand(group)
_addCommand(group)
}
}

View file

@ -1,20 +1,27 @@
package io.github.null2264.tsukumogami.core.module
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.IGroup
@OptIn(TsukumogamiInternalApi::class)
open class BotModule constructor(val name: String) : IGroup {
override val allCommands: MutableMap<String, Command> = mutableMapOf()
override fun _addCommand(command: Command) {
command.module = this
super._addCommand(command)
}
internal fun install(bot: Bot): BotModule {
allCommands.values.forEach(bot::addCommand)
allCommands.values.distinctBy { it.name }.forEach(bot::addCommand)
return this
}
internal fun uninstall(bot: Bot): BotModule {
allCommands.values.forEach(bot::removeCommand)
allCommands.values.distinctBy { it.name }.forEach(bot::removeCommand)
return this
}
}