diff --git a/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt b/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt index 60b6ff4..3b0176e 100644 --- a/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt +++ b/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt @@ -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}") + } } } diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Command.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Command.kt index 382e66a..d0e66dd 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Command.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Command.kt @@ -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) diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Group.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Group.kt index 0c41d48..30cde51 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Group.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/Group.kt @@ -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, @@ -18,6 +20,11 @@ class Group( override val allCommands: MutableMap = 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}") diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/IGroup.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/IGroup.kt index dcc1640..78d06c7 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/IGroup.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/commands/IGroup.kt @@ -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 - 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 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) } } diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt index de5571c..bdf2b85 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt @@ -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 = 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 } }