From a45009d6674580b35d2af8b9b78a88d8b53716f7 Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Mon, 18 Apr 2016 09:28:29 -0500 Subject: [PATCH] Update JDA, add ability to specify bot token --- build.gradle | 2 +- .../obsidian/discordbridge/Configuration.kt | 2 ++ .../discordbridge/DiscordConnection.kt | 33 ++++++++++++++----- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index e5e6be6..acd39c5 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ repositories { dependencies { compile group: 'org.spigotmc', name: 'spigot-api', version:'1.8.8-R0.1-SNAPSHOT' compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version:'1.0.0-rc-1036' - compile group: 'net.dv8tion', name: 'JDA', version:'1.3.0_188' + compile group: 'net.dv8tion', name: 'JDA', version:'1.4.0_238' } processResources { diff --git a/src/main/kotlin/gg/obsidian/discordbridge/Configuration.kt b/src/main/kotlin/gg/obsidian/discordbridge/Configuration.kt index b1d812f..9b0d1b1 100644 --- a/src/main/kotlin/gg/obsidian/discordbridge/Configuration.kt +++ b/src/main/kotlin/gg/obsidian/discordbridge/Configuration.kt @@ -7,6 +7,7 @@ class Configuration(val plugin: Plugin) { var USERNAME: String = "" var EMAIL: String = "" var PASSWORD: String = "" + var TOKEN: String = "" var DEBUG: Boolean = false var RELAY_CANCELLED_MESSAGES = true @@ -39,6 +40,7 @@ class Configuration(val plugin: Plugin) { USERNAME = plugin.config.getString("settings.username") EMAIL = plugin.config.getString("settings.email") PASSWORD = plugin.config.getString("settings.password") + TOKEN = plugin.config.getString("settings.token") DEBUG = plugin.config.getBoolean("settings.debug", false) RELAY_CANCELLED_MESSAGES = plugin.config.getBoolean("settings.relay_cancelled_messages", true) diff --git a/src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt b/src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt index 74e6bfc..d509d36 100644 --- a/src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt +++ b/src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt @@ -1,18 +1,19 @@ package gg.obsidian.discordbridge +import net.dv8tion.jda.JDA import net.dv8tion.jda.JDABuilder import net.dv8tion.jda.entities.Guild import net.dv8tion.jda.entities.TextChannel class DiscordConnection(val plugin: Plugin) : Runnable { - var api = JDABuilder(plugin.configuration.EMAIL, plugin.configuration.PASSWORD).setAudioEnabled(false).buildBlocking() - var listener = DiscordListener(plugin, api, this) + var api: JDA? = null + var listener: DiscordListener? = null var server: Guild? = null var channel: TextChannel? = null override fun run() { try { - api.addEventListener(listener) + connect() } catch (e: Exception) { plugin.logger.severe("Error connecting to Discord: " + e) } @@ -30,15 +31,29 @@ class DiscordConnection(val plugin: Plugin) : Runnable { } fun reconnect() { - api.removeEventListener(listener) - api.shutdown(false) - api = JDABuilder(plugin.configuration.EMAIL, plugin.configuration.PASSWORD).setAudioEnabled(false).buildBlocking() - listener = DiscordListener(plugin, api, this) - api.addEventListener(listener) + disconnect() + connect() + } + + private fun disconnect() { + api?.removeEventListener(listener) + api?.shutdown(false) + } + + private fun connect() { + var builder = JDABuilder().setAudioEnabled(false) + if (plugin.configuration.TOKEN != "") { + builder = builder.setBotToken(plugin.configuration.TOKEN) + } else { + builder = builder.setEmail(plugin.configuration.EMAIL).setPassword(plugin.configuration.PASSWORD) + } + api = builder.buildBlocking() + listener = DiscordListener(plugin, api as JDA, this) + api!!.addEventListener(listener) } private fun getServerById(id: String): Guild? { - for (server in api.guilds) + for (server in api!!.guilds) if (server.id.equals(id, true)) return server return null