Skip to content

Commit

Permalink
Switch from jDiscord to JDA API library
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Feb 8, 2016
1 parent 82e0d3e commit 9d698f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
23 changes: 11 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.0.0-beta-1103</kotlin.version>
<kotlin.version>1.0.0-rc-1036</kotlin.version>
</properties>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>xyz.gghost</id>
<url>http://gghost.xyz/maven/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -45,10 +41,9 @@
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>xyz.gghost</groupId>
<artifactId>jdiscord</artifactId>
<version>1.3</version>
<scope>compile</scope>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>1.2.1_106</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -101,13 +96,17 @@
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<!-- Enabling minimizeJar currently breaks this plugin -->
<minimizeJar>false</minimizeJar>

<createDependencyReducedPom>false</createDependencyReducedPom>
<artifactSet>
<includes>
<include>org.jetbrains.kotlin:*</include>
<include>xyz.gghost:*</include>
<include>*</include>
</includes>
<excludes>
<exclude>org.spigotmc:spigot-api</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
Expand Down
24 changes: 11 additions & 13 deletions src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package gg.obsidian.discordbridge

import me.itsghost.jdiscord.DiscordAPI
import me.itsghost.jdiscord.DiscordBuilder
import me.itsghost.jdiscord.Server
import me.itsghost.jdiscord.talkable.Group
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: DiscordAPI = DiscordBuilder(plugin.configuration.EMAIL, plugin.configuration.PASSWORD).build()
var server: Server? = null
var channel: Group? = null
var api = JDABuilder(plugin.configuration.EMAIL, plugin.configuration.PASSWORD).build()
var server: Guild? = null
var channel: TextChannel? = null

override fun run() {
try {
api.login()
api.eventManager.registerListener(DiscordListener(plugin, api))
api.addEventListener(DiscordListener(plugin, api))
} catch (e: Exception) {
plugin.logger.severe("Error connecting to Discord: " + e)
}
Expand All @@ -30,15 +28,15 @@ class DiscordConnection(val plugin: Plugin) : Runnable {
channel!!.sendMessage(message)
}

private fun getServerById(id: String): Server? {
for (server in api.availableServers)
private fun getServerById(id: String): Guild? {
for (server in api.guilds)
if (server.id.equals(id, true))
return server
return null
}

private fun getGroupByName(server: Server, name: String): Group? {
for (group in server.groups)
private fun getGroupByName(server: Guild, name: String): TextChannel? {
for (group in server.textChannels)
if (group.name.equals(name))
return group
return null
Expand Down
34 changes: 20 additions & 14 deletions src/main/kotlin/gg/obsidian/discordbridge/DiscordListener.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package gg.obsidian.discordbridge

import me.itsghost.jdiscord.DiscordAPI
import me.itsghost.jdiscord.event.EventListener
import me.itsghost.jdiscord.events.UserChatEvent
import com.neovisionaries.ws.client.WebSocket
import com.neovisionaries.ws.client.WebSocketException
import net.dv8tion.jda.JDA
import net.dv8tion.jda.events.message.MessageReceivedEvent
import net.dv8tion.jda.hooks.ListenerAdapter

class DiscordListener(val plugin: Plugin, val api: DiscordAPI) : EventListener {
class DiscordListener(val plugin: Plugin, val api: JDA) : ListenerAdapter() {

fun userChat(e: UserChatEvent) {
plugin.logDebug("Received message ${e.msg.id} from Discord")
override fun onMessageReceived(event: MessageReceivedEvent) {
plugin.logDebug("Received message ${event.message.id} from Discord")

if (!e.server.id.equals(plugin.configuration.SERVER_ID)) {
plugin.logDebug("Ignoring message ${e.msg.id} from Discord: server does not match")
if (!event.guild.id.equals(plugin.configuration.SERVER_ID)) {
plugin.logDebug("Ignoring message ${event.message.id} from Discord: server does not match")
return
}

if (!e.group.name.equals(plugin.configuration.CHANNEL, true)) {
plugin.logDebug("Ignoring message ${e.msg.id} from Discord: channel does not match")
if (!event.textChannel.name.equals(plugin.configuration.CHANNEL, true)) {
plugin.logDebug("Ignoring message ${event.message.id} from Discord: channel does not match")
return
}

val username: String = e.user.user.username
val username: String = event.author.username

if (username.equals(plugin.configuration.USERNAME, true)) {
plugin.logDebug("Ignoring message ${e.msg.id} from Discord: it matches the server's username")
plugin.logDebug("Ignoring message ${event.message.id} from Discord: it matches the server's username")
return
}

plugin.logDebug("Broadcasting message ${e.msg.id} from Discord as user $username")
plugin.sendToMinecraft(username, e.msg.message)
plugin.logDebug("Broadcasting message ${event.message.id} from Discord as user $username")
plugin.sendToMinecraft(username, event.message.content)
}

fun onUnexpectedError(ws: WebSocket, wse: WebSocketException) {
plugin.logger.severe("Unexpected error from DiscordBridge: ${wse.message}")
}
}

0 comments on commit 9d698f2

Please sign in to comment.