aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/romangraef/jdacommander/start.kt
diff options
context:
space:
mode:
authorRoman Gräf <romangraef@loves.dicksinhisan.us>2020-08-21 19:11:42 +0200
committerRoman Gräf <romangraef@loves.dicksinhisan.us>2020-08-21 19:11:42 +0200
commit69f28613499e3ce418326d05c66e5c856acb8269 (patch)
tree01376f981dce3b44918389860b6eabed92bf9ba1 /src/main/kotlin/com/romangraef/jdacommander/start.kt
downloaddiscord-jda-jcommander-master.tar.gz
discord-jda-jcommander-master.tar.bz2
discord-jda-jcommander-master.zip
Initial commitHEADmaster
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