A JDA Starter Project in Kotlin with a few features.
- Create a class that extends CommandAbstract and put CommandInfo annotation on it.
- After that, to register your command, put RegisterCommand annotation OR change your autoRegisterClass in CommandInfo annotation to true (default is false).
without command options (Ping.kt)
package me.monmcgt.code.commands.impl
import me.monmcgt.code.commands.CommandAbstract
import me.monmcgt.code.commands.CommandInfo
import me.monmcgt.code.commands.RegisterCommand
@RegisterCommand
@CommandInfo(name = "ping", description = "responds with pong")
class Ping : CommandAbstract() {
override fun onSlashCommandInteraction() {
sendEmphemralMessage("Pong!")
}
}
without command options (UserAvatar.kt)
package me.monmcgt.code.commands.impl
import me.monmcgt.code.commands.CommandAbstract
import me.monmcgt.code.commands.CommandInfo
import me.monmcgt.code.commands.RegisterCommand
@RegisterCommand
@CommandInfo(name = "useravatar", description = "Get the avatar of a user")
class UserAvatar : CommandAbstract() {
override fun onSlashCommandInteraction() {
val member = event.member
if (member == null) {
sendEmphemralMessage("Something went wrong, please try again later")
return
}
sendEmphemralMessage(member.user.effectiveAvatarUrl)
}
}
with command options (YourName.kt)
package me.monmcgt.code.commands.impl
import me.monmcgt.code.commands.CommandAbstract
import me.monmcgt.code.commands.CommandInfo
import me.monmcgt.code.commands.RegisterCommand
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.OptionData
@RegisterCommand
@CommandInfo(name = "yourname", description = "Returns your name")
class YourName : CommandAbstract() {
override val optionDataArray: Array<OptionData>
get() = arrayOf(OptionData(OptionType.STRING, "name", "Your name", true))
override fun onSlashCommandInteraction() {
event.getOption("name")?.let {
sendEmphemralMessage("Your name is ${it.asString}")
}
}
}
- Spring Framework - Commands registering system (Java Reflection)