aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/romangraef/jdacommander/start.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/com/romangraef/jdacommander/start.kt')
-rw-r--r--src/main/kotlin/com/romangraef/jdacommander/start.kt45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/main/kotlin/com/romangraef/jdacommander/start.kt b/src/main/kotlin/com/romangraef/jdacommander/start.kt
new file mode 100644
index 0000000..465d79c
--- /dev/null
+++ b/src/main/kotlin/com/romangraef/jdacommander/start.kt
@@ -0,0 +1,45 @@
+package com.romangraef.jdacommander
+
+import com.beust.jcommander.JCommander
+import net.dv8tion.jda.api.JDABuilder
+import net.dv8tion.jda.api.events.message.MessageReceivedEvent
+import net.dv8tion.jda.api.hooks.ListenerAdapter
+import org.reflections.Reflections
+
+fun loadCommands(packageName: String): Map<String, Class<out ICommand>> {
+ val reflections = Reflections(packageName)
+ return reflections.getTypesAnnotatedWith(Command::class.java)
+ .map {
+ println(it)
+ it
+ }
+ .filter { ICommand::class.java.isAssignableFrom(it) }
+ .map {
+ it.getAnnotation(Command::class.java).value to (it as Class<out ICommand>)
+ }.toMap()
+}
+
+fun main() {
+ val allCommands = loadCommands("com.romangraef.jdacommander.commands")
+ JDABuilder.createDefault(System.getenv("DISCORD_TOKEN"))
+ .addEventListeners(object : ListenerAdapter() {
+ override fun onMessageReceived(event: MessageReceivedEvent) {
+ val text = event.message.contentRaw
+ if (!text.startsWith("!")) return
+ val jCommander = JCommander()
+ val bi = BaseInformation()
+ allCommands.forEach { (name, `class`) ->
+ jCommander.addCommand(name, `class`.newInstance())
+ }
+ jCommander.addObject(bi)
+ jCommander.parse(*text.substring(1).split("\\s+".toRegex()).toTypedArray())
+ val parsedCommand = jCommander.parsedCommand
+ jCommander.findCommandByAlias(parsedCommand).objects.filterIsInstance<ICommand>().forEach {
+ it.base = bi
+ it.message = event.message
+ it.run()
+ }
+ }
+ })
+ .build()
+} \ No newline at end of file