diff options
Diffstat (limited to 'src/main')
7 files changed, 146 insertions, 0 deletions
diff --git a/src/main/kotlin/com/romangraef/jdacommander/BaseInformation.kt b/src/main/kotlin/com/romangraef/jdacommander/BaseInformation.kt new file mode 100644 index 0000000..fdf4a67 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jdacommander/BaseInformation.kt @@ -0,0 +1,8 @@ +package com.romangraef.jdacommander + +import com.beust.jcommander.Parameter + +class BaseInformation { + @Parameter(names = ["-p", "--private", "--direct", "--dm"]) + var sendToDM: Boolean = false +} diff --git a/src/main/kotlin/com/romangraef/jdacommander/Command.kt b/src/main/kotlin/com/romangraef/jdacommander/Command.kt new file mode 100644 index 0000000..a5178e6 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jdacommander/Command.kt @@ -0,0 +1,8 @@ +package com.romangraef.jdacommander + +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.CLASS) +annotation class Command + ( + val value: String +)
\ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jdacommander/ICommand.kt b/src/main/kotlin/com/romangraef/jdacommander/ICommand.kt new file mode 100644 index 0000000..99fee95 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jdacommander/ICommand.kt @@ -0,0 +1,14 @@ +package com.romangraef.jdacommander + +import net.dv8tion.jda.api.entities.Message +import net.dv8tion.jda.api.entities.MessageChannel +import net.dv8tion.jda.api.requests.RestAction + +abstract class ICommand { + lateinit var base: BaseInformation + lateinit var message: Message + abstract fun run() + val channel get() = message.channel + val jda get() = message.jda + val responseChannel: RestAction<out MessageChannel> get() = if (base.sendToDM) message.author.openPrivateChannel() else SyncRestAction(jda, channel) +} diff --git a/src/main/kotlin/com/romangraef/jdacommander/SyncRestAction.kt b/src/main/kotlin/com/romangraef/jdacommander/SyncRestAction.kt new file mode 100644 index 0000000..27b0b10 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jdacommander/SyncRestAction.kt @@ -0,0 +1,28 @@ +package com.romangraef.jdacommander + +import java.util.concurrent.CompletableFuture +import java.util.function.BooleanSupplier +import java.util.function.Consumer +import net.dv8tion.jda.api.JDA +import net.dv8tion.jda.api.requests.RestAction + +class SyncRestAction<I>(private val jda: JDA, private val value: I) : RestAction<I> { + override fun getJDA(): JDA = jda + + override fun setCheck(checks: BooleanSupplier?): RestAction<I> { + // No need for last Second checks. + // We don't execute HTTP requests anyways. + return this + } + + override fun queue(success: Consumer<in I>?, failure: Consumer<in Throwable>?) { + success?.accept(value) + } + + override fun complete(shouldQueue: Boolean): I = value + + override fun submit(shouldQueue: Boolean): CompletableFuture<I> = + CompletableFuture.supplyAsync { value } + + +}
\ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jdacommander/commands/EchoCommand.kt b/src/main/kotlin/com/romangraef/jdacommander/commands/EchoCommand.kt new file mode 100644 index 0000000..edf50ba --- /dev/null +++ b/src/main/kotlin/com/romangraef/jdacommander/commands/EchoCommand.kt @@ -0,0 +1,20 @@ +package com.romangraef.jdacommander.commands + +import com.beust.jcommander.Parameter +import com.romangraef.jdacommander.Command +import com.romangraef.jdacommander.ICommand + +@Command("echo") +class EchoCommand : ICommand() { + @Parameter(names = ["-c", "--caps", "--capitalize"], description = "Convert the text to upper case") + var caps: Boolean = false + + @Parameter(description = "Text to echo back") + lateinit var texts: List<String> + val text get() = texts.joinToString(" ") + + override fun run() = responseChannel.queue { + it.sendMessage(if (caps) text.toUpperCase() else text).queue() + } + +} diff --git a/src/main/kotlin/com/romangraef/jdacommander/commands/HelpCommand.kt b/src/main/kotlin/com/romangraef/jdacommander/commands/HelpCommand.kt new file mode 100644 index 0000000..63a69fc --- /dev/null +++ b/src/main/kotlin/com/romangraef/jdacommander/commands/HelpCommand.kt @@ -0,0 +1,23 @@ +package com.romangraef.jdacommander.commands + +import com.beust.jcommander.Parameter +import com.romangraef.jdacommander.Command +import com.romangraef.jdacommander.ICommand + +@Command("help") +class HelpCommand : ICommand() { + + @Parameter(description = "Help Topic") + var helpTopic: String? = null + + override fun run() = responseChannel.queue { + it.sendMessage( + if (helpTopic == null) { + "Helping...." + } else { + "Helping on topic $helpTopic" + } + ).queue() + } + +} 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 |