diff --git a/src/main/kotlin/Command.kt b/src/main/kotlin/Command.kt index 48b0fe5e..34525085 100644 --- a/src/main/kotlin/Command.kt +++ b/src/main/kotlin/Command.kt @@ -1,9 +1,11 @@ +import com.mojang.brigadier.CommandDispatcher import com.mojang.brigadier.builder.LiteralArgumentBuilder import com.mojang.brigadier.tree.LiteralCommandNode import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.coroutineScope import net.ayataka.kordis.event.events.message.MessageReceiveEvent +import org.reflections.Reflections import java.util.concurrent.ConcurrentLinkedQueue /** @@ -33,12 +35,36 @@ class Cmd(val event: MessageReceiveEvent) { object CommandManager { /* Name, Literal Command */ - val commands = hashMapOf>() - val commandClasses = hashMapOf() + private val commands = hashMapOf>() + private val commandClasses = hashMapOf() fun isCommand(name: String) = commands.containsKey(name) fun getCommand(name: String) = commands[name] fun getCommandClass(name: String) = commandClasses[name] + + /** + * Uses reflection to get a list of classes in the commands package which extend [Command] + * and register said classes's instances with Brigadier. + */ + fun registerCommands(dispatcher: CommandDispatcher) { + val reflections = Reflections("commands") + + val subTypes: Set> = reflections.getSubTypesOf(Command::class.java) + + println("Registering commands...") + + for (command in subTypes) { + val literalCommand = command.getField("INSTANCE").get(null) as LiteralArgumentBuilder + val commandAsInstanceOfCommand = command.getField("INSTANCE").get(null) as Command + commandClasses[literalCommand.literal] = commandAsInstanceOfCommand + commands[literalCommand.literal] = dispatcher.register(literalCommand) + } + + var registeredCommands = "" + commands.forEach { entry -> registeredCommands += "\n> ;${entry.key}" } + + println("Registered commands!$registeredCommands\n") + } } \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index fb453b10..726920f5 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,20 +1,21 @@ -import Main.currentVersion +import CommandManager.registerCommands +import UpdateHelper.updateCheck import com.mojang.brigadier.CommandDispatcher -import com.mojang.brigadier.builder.LiteralArgumentBuilder import com.mojang.brigadier.exceptions.CommandSyntaxException import kotlinx.coroutines.runBlocking import net.ayataka.kordis.Kordis import net.ayataka.kordis.event.EventHandler import net.ayataka.kordis.event.events.message.MessageReceiveEvent -import org.reflections.Reflections import java.awt.Color -import java.io.File - fun main() = runBlocking { Bot().start() } +/** + * @author dominikaaaa + * @since 16/08/20 17:30 + */ class Bot { private val dispatcher = CommandDispatcher() private var hasUpdate = false @@ -39,7 +40,7 @@ class Bot { addListener(this@Bot) } - registerCommands() + registerCommands(dispatcher) println("Initialized bot!\n" + "Startup took ${System.currentTimeMillis() - started}ms") } @@ -65,46 +66,6 @@ class Bot { } } } - - private fun updateCheck() { - if (File("noUpdateCheck").exists()) return - val versionConfig = FileManager.readConfig(ConfigType.VERSION, false) - - if (versionConfig?.version == null) { - println("Couldn't access remote version when checking for update") - return - } - - if (versionConfig.version != currentVersion) { - println("Not up to date:\nCurrent version: $currentVersion\nLatest Version: ${versionConfig.version}\n") - } else { - println("Up to date! Running on $currentVersion") - } - } - - /** - * Uses reflection to get a list of classes in the commands package which extend [Command] - * and register said classes's instances with Brigadier. - */ - private fun registerCommands() { - val reflections = Reflections("commands") - - val subTypes: Set> = reflections.getSubTypesOf(Command::class.java) - - println("Registering commands...") - - for (command in subTypes) { - val literalCommand = command.getField("INSTANCE").get(null) as LiteralArgumentBuilder - val commandAsInstanceOfCommand = command.getField("INSTANCE").get(null) as Command - CommandManager.commandClasses[literalCommand.literal] = commandAsInstanceOfCommand - CommandManager.commands[literalCommand.literal] = dispatcher.register(literalCommand) - } - - var registeredCommands = "" - CommandManager.commands.forEach { entry -> registeredCommands += "\n> ;${entry.key}" } - - println("Registered commands!$registeredCommands\n") - } } object Main { diff --git a/src/main/kotlin/UpdateHelper.kt b/src/main/kotlin/UpdateHelper.kt new file mode 100644 index 00000000..b1cfdccc --- /dev/null +++ b/src/main/kotlin/UpdateHelper.kt @@ -0,0 +1,29 @@ +import Main.currentVersion +import java.io.File + +object UpdateHelper { + fun updateCheck() { + if (File("noUpdateCheck").exists()) return + val versionConfig = FileManager.readConfig(ConfigType.VERSION, false) + + if (versionConfig?.version == null) { + println("Couldn't access remote version when checking for update") + return + } + + if (versionConfig.version != currentVersion) { + println("Not up to date:\nCurrent version: $currentVersion\nLatest Version: ${versionConfig.version}\n") + } else { + println("Up to date! Running on $currentVersion") + } + } + + /** + * TODO: FINISH + */ + fun updateBot(version: String) { + if (!File("autoUpdate").exists()) return + + println() + } +} \ No newline at end of file diff --git a/src/main/kotlin/commands/DiscussCommand.kt b/src/main/kotlin/commands/DiscussCommand.kt index b1ff2883..1497c95a 100644 --- a/src/main/kotlin/commands/DiscussCommand.kt +++ b/src/main/kotlin/commands/DiscussCommand.kt @@ -1,7 +1,6 @@ package commands import Command -import Main import Main.Colors import arg import doesLater