From ad69465900259981d10d7364487df491ef69ceee Mon Sep 17 00:00:00 2001 From: DiamondIceNS Date: Thu, 14 Dec 2017 19:25:38 -0600 Subject: [PATCH] Update version to 3.1.0 Modified: - Reorganized BotControllerManager to be a little more navigable - Minor syntax tweaks taking advantage of new Kotlin syntax - Added 'private' and removed some explicit type declarations so IDEA would make the squiggly lines go away --- build.gradle | 2 +- .../gg/obsidian/discordbridge/Plugin.kt | 26 +- .../obsidian/discordbridge/UserAliasConfig.kt | 4 +- .../controllers/BotControllerManager.kt | 272 ++++++++++-------- .../controllers/FunCommandsController.kt | 10 +- .../discordbridge/discord/Connection.kt | 2 +- .../discordbridge/discord/Listener.kt | 2 +- .../minecraft/CommandListener.kt | 2 +- .../discordbridge/minecraft/EventListener.kt | 2 +- 9 files changed, 170 insertions(+), 152 deletions(-) diff --git a/build.gradle b/build.gradle index d29f7b1..ce9bd56 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ plugins { apply plugin: 'kotlin' group = 'gg.obsidian' -version = '3.1.0-dev1' +version = '3.1.0' description = """Bridge chat between Minecraft and Discord""" ext.url = 'https://github.com/the-obsidian/DiscordBridge' diff --git a/src/main/kotlin/gg/obsidian/discordbridge/Plugin.kt b/src/main/kotlin/gg/obsidian/discordbridge/Plugin.kt index adf5411..b84c691 100644 --- a/src/main/kotlin/gg/obsidian/discordbridge/Plugin.kt +++ b/src/main/kotlin/gg/obsidian/discordbridge/Plugin.kt @@ -136,7 +136,7 @@ class Plugin : JavaPlugin() { /** * Saves all default configs where configs do not exist and reloads data from file into memory */ - fun updateConfig(version: String) { + private fun updateConfig(version: String) { this.saveDefaultConfig() config.options().copyDefaults(true) config.set("version", version) @@ -182,7 +182,7 @@ class Plugin : JavaPlugin() { val users = Connection.listUsers() val found: Member = users.find { it.user.name + "#" + it.user.discriminator == discriminator } ?: return null - val ua: UserAlias = UserAlias(player.uniqueId, found.user.id) + val ua = UserAlias(player.uniqueId, found.user.id) requests.add(ua) val msg = "Minecraft user '${server.getOfflinePlayer(ua.mcUuid).name}' has requested to become associated with your Discord" + " account. If this is you, respond '${Connection.JDA.selfUser.asMention} confirm'. If this is not" + @@ -203,8 +203,8 @@ class Plugin : JavaPlugin() { var response = "${CC.YELLOW}Discord users:" for (user in users) { - if (user.user.isBot) response += "\n${CC.GOLD}- ${user.effectiveName} (Bot) | ${user.user.name}#${user.user.discriminator}${CC.RESET}" - else response += "\n${CC.YELLOW}- ${user.effectiveName} | ${user.user.name}#${user.user.discriminator}${CC.RESET}" + response += if (user.user.isBot) "\n${CC.GOLD}- ${user.effectiveName} (Bot) | ${user.user.name}#${user.user.discriminator}${CC.RESET}" + else "\n${CC.YELLOW}- ${user.effectiveName} | ${user.user.name}#${user.user.discriminator}${CC.RESET}" } return response.trim() } @@ -218,25 +218,25 @@ class Plugin : JavaPlugin() { return "${CC.YELLOW}No Discord members could be found. Either server is empty or an error has occurred." var response = "" - if (onlineUsers.filter { it.onlineStatus == OnlineStatus.ONLINE }.isNotEmpty()) { + if (onlineUsers.any { it.onlineStatus == OnlineStatus.ONLINE }) { response += "\n${CC.DARK_GREEN}Online:${CC.RESET}" for (user in onlineUsers.filter { it.onlineStatus == OnlineStatus.ONLINE }) { - if (user.user.isBot) response += "\n${CC.DARK_GREEN}- ${user.effectiveName} (Bot)${CC.RESET}" - else response += "\n${CC.DARK_GREEN}- ${user.effectiveName}${CC.RESET}" + response += if (user.user.isBot) "\n${CC.DARK_GREEN}- ${user.effectiveName} (Bot)${CC.RESET}" + else "\n${CC.DARK_GREEN}- ${user.effectiveName}${CC.RESET}" } } - if (onlineUsers.filter { it.onlineStatus == OnlineStatus.IDLE }.isNotEmpty()) { + if (onlineUsers.any { it.onlineStatus == OnlineStatus.IDLE }) { response += "\n${CC.YELLOW}Idle:${CC.RESET}" for (user in onlineUsers.filter { it.onlineStatus == OnlineStatus.IDLE }) { - if (user.user.isBot) response += "\n${CC.YELLOW}- ${user.effectiveName} (Bot)${CC.RESET}" - else response += "\n${CC.YELLOW}- ${user.effectiveName}${CC.RESET}" + response += if (user.user.isBot) "\n${CC.YELLOW}- ${user.effectiveName} (Bot)${CC.RESET}" + else "\n${CC.YELLOW}- ${user.effectiveName}${CC.RESET}" } } - if (onlineUsers.filter { it.onlineStatus == OnlineStatus.DO_NOT_DISTURB }.isNotEmpty()) { + if (onlineUsers.any { it.onlineStatus == OnlineStatus.DO_NOT_DISTURB }) { response += "\n${CC.RED}Do Not Disturb:${CC.RESET}" for (user in onlineUsers.filter { it.onlineStatus == OnlineStatus.DO_NOT_DISTURB }) { - if (user.user.isBot) response += "\n${CC.RED}- ${user.effectiveName} (Bot)${CC.RESET}" - else response += "\n${CC.RED}- ${user.effectiveName}${CC.RESET}" + response += if (user.user.isBot) "\n${CC.RED}- ${user.effectiveName} (Bot)${CC.RESET}" + else "\n${CC.RED}- ${user.effectiveName}${CC.RESET}" } } diff --git a/src/main/kotlin/gg/obsidian/discordbridge/UserAliasConfig.kt b/src/main/kotlin/gg/obsidian/discordbridge/UserAliasConfig.kt index 2398afa..05e8b41 100644 --- a/src/main/kotlin/gg/obsidian/discordbridge/UserAliasConfig.kt +++ b/src/main/kotlin/gg/obsidian/discordbridge/UserAliasConfig.kt @@ -13,7 +13,7 @@ object UserAliasConfig { */ fun load(plugin: Plugin) { val list = plugin.users.data.getList("aliases") - if (list != null) aliases = list.checkItemsAre() ?: + if (list != null) aliases = list.checkItemsAre() ?: throw IllegalStateException("usernames.yml could not be read - list items are not properly formatted") else mutableListOf() } @@ -42,5 +42,5 @@ object UserAliasConfig { * A function to assert that all the items in a given list are of a specific type */ @Suppress("UNCHECKED_CAST") - inline fun List<*>.checkItemsAre() = if (all { it is T }) this as List else null + private inline fun List<*>.checkItemsAre() = if (all { it is T }) this as List else null } diff --git a/src/main/kotlin/gg/obsidian/discordbridge/commands/controllers/BotControllerManager.kt b/src/main/kotlin/gg/obsidian/discordbridge/commands/controllers/BotControllerManager.kt index 064d017..8f09c83 100644 --- a/src/main/kotlin/gg/obsidian/discordbridge/commands/controllers/BotControllerManager.kt +++ b/src/main/kotlin/gg/obsidian/discordbridge/commands/controllers/BotControllerManager.kt @@ -31,6 +31,9 @@ class BotControllerManager(val plugin: Plugin) { private val commands: MutableMap = mutableMapOf() private val controllers: MutableMap, IBotController> = mutableMapOf() + // ============================================= + // =============== SETUP METHODS =============== + /** * Adds an IBotController to the manager. * @@ -81,6 +84,9 @@ class BotControllerManager(val plugin: Plugin) { commands.put(command.name, command) } + // ============================================= + // ============== MESSAGE HANDLING ============= + /** * Reads an incoming message and attempts to parse and execute a command. * @@ -96,11 +102,11 @@ class BotControllerManager(val plugin: Plugin) { commandNotFound(event, event.command.name) return true } - return invokeCommand(command, controllers, event, event.args.asList().toTypedArray()) + return invokeBotCommand(command, controllers, event, event.args.asList().toTypedArray()) } // Short circuit scripted responses - if (scriptedResponse(event)) return true + if (sendScriptedResponse(event)) return true // command if (Config.COMMAND_PREFIX.isNotBlank() && event.rawMessage.startsWith(Config.COMMAND_PREFIX)) { @@ -120,47 +126,18 @@ class BotControllerManager(val plugin: Plugin) { return parseCommand(event, split, true) } - // Just relay the message if it is neither + // Just relay the message if no command is found relay(event, true) return true } - /** - * Attempt to parse and execute a command from an input string - * - * @param event the original event object - * @param args the input string broken up into an array of words, with the command as the first element - * @param defaultToTalk if true, failure to find a command to execute will execute Talk with Cleverbot using the - * full string as an argument. If false, failure will do nothing and the method call will return false. - */ - private fun parseCommand(event: IEventWrapper, args: Array, defaultToTalk: Boolean): Boolean { - val commandName = args[0].toLowerCase() - if (commandName == "") return true - var command = commands[commandName] - - if (command == null) { - // Attempt to run as a server command if sent from Discord - if (event is MessageWrapper && event.originalMessage.member.hasPermission(Permission.ADMINISTRATOR)) - if (serverCommand(event, args)) return true - - if (defaultToTalk) command = commands["talk"] - if (command == null) { - commandNotFound(event, commandName) - return false - } - } - - val slicedArgs = if (args.size > 1) args.slice(1 until args.size).toTypedArray() else arrayOf() - return invokeCommand(command, controllers, event, slicedArgs) - } - /** * Looks for a scripted trigger and returns a respective response * * @param event the incoming event object * @return true if a trigger was found and successfully responded to, false otherwise */ - private fun scriptedResponse(event: IEventWrapper): Boolean { + private fun sendScriptedResponse(event: IEventWrapper): Boolean { val responses = plugin.script.data.getList("responses").checkItemsAre