Skip to content

kord annotations

rawr edited this page Feb 17, 2022 · 2 revisions

Initial Setup

It is recommended to follow the gradle or maven build tool guide.

Setting up the Platform

fun main()
{
    val client = Kord("<your-token>")

    Platforms
        .usePlatform<KordCommandPlatform> {
            this.use(client) // this sets up the listeners for the command
        }
        .useWrapper<AnnotationCommandWrapper>() // it is required to call this after usePlatform. this class uses the platform to do certain things

    client.login()
}

After setting up the platform you are able to register the commands through the same Platforms class, view example beneath.

Platforms.registerCommand(TestCommand)

Basic Command Examples

Basic parent-subcommand command

@Command("name|alias1|alias2")
class TestCommand
{
    @Default // parent command, uses @Command parameter of parent class.
    @CommandPermission("permission")
    suspend fun default(executor: KordExecutor, amount: Int)
    {
        for (i in 0..amount)
        {
            executor.replyToMessage("iteration $amount")
        }
    }

    @Command("subcommand|subcommandalias1") // subcommand of parent command
    @CommandPermission("permission")
    suspend fun subcommand(executor: KordExecutor, message: Array<String>)
    {
        executor.replyToMessage(message.joinToString(" "))
    }
}

Basic multi-parent command class

class TestCommand
{
    @Command("name|alias1|alias2") // first parent command
    @CommandPermission("permission")
    suspend fun command1(executor: KordExecutor, amount: Int)
    {
        for (i in 0..amount)
        {
            executor.replyToMessage("iteration $amount")
        }
    }

    @Command("command2name|command2alias") // other parent command
    @CommandPermission("permission")
    suspend fun command2(executor: KordExecutor, message: Array<String>)
    {
        executor.replyToMessage(message.joinToString(" "))
    }
}

Utilizing the automatic help generation

@Command("hey|how|test")
object TestCommand
{
    @Help
    @Default
    @HelpDescription("Show this menu")
    @CommandPermission("hello.test")
    suspend fun test()
    {
        println("called test() body") // this won't print!
    }

    @Command("bye")
    @HelpDescription("Broadcast a message.")
    suspend fun bye(executor: KordExecutor, lol: Array<String>)
    {
        val message = executor.message
        
        executor.replyToMessage("${message.joinToString(" ")} ${message.content}")
    }

    @Command("yo")
    @HelpDescription("Broadcast your name.")
    fun yo(executor: KordExecutor, @Value("1") amount: Int)
    {
        for (i in 0..amount)
        {
            executor.replyToMessage("iteration $amount")
        }
    }
}

Clone this wiki locally