Skip to content

Commit

Permalink
Update JDA, add ability to specify bot token
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Apr 18, 2016
1 parent 20b4300 commit a45009d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/gg/obsidian/discordbridge/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
33 changes: 24 additions & 9 deletions src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit a45009d

Please sign in to comment.