Skip to content

Commit

Permalink
Add debug flag for extra messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Nov 21, 2015
1 parent be83261 commit 3ee2f05
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/kotlin/gg/obsidian/discordbridge/DiscordBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DiscordBridge : JavaPlugin(), Listener {
var username: String = ""
var email: String = ""
var password: String = ""
var debug: Boolean = false

var connection: DiscordConnection? = null

Expand All @@ -24,6 +25,7 @@ class DiscordBridge : JavaPlugin(), Listener {
this.username = config.getString("settings.username")
this.email = config.getString("settings.email")
this.password = config.getString("settings.password")
this.debug = config.getBoolean("settings.debug", false)

this.connection = DiscordConnection(this)

Expand All @@ -33,10 +35,12 @@ class DiscordBridge : JavaPlugin(), Listener {

@EventHandler(priority = EventPriority.MONITOR)
fun onChat(event: AsyncPlayerChatEvent) {
logDebug("Received a chat event from ${event.player.name}: ${event.message}")
send(event.player.name, event.message)
}

fun send(name: String, message: String) {
logDebug("Sending chat message to Discord - $name: $message")
connection!!.send(name, message)
}

Expand All @@ -46,4 +50,9 @@ class DiscordBridge : JavaPlugin(), Listener {
config.set("version", version)
saveConfig()
}

fun logDebug(msg: String) {
if (!debug) return;
logger.info(msg)
}
}
8 changes: 7 additions & 1 deletion src/main/kotlin/gg/obsidian/discordbridge/DiscordListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@ import org.bukkit.ChatColor
class DiscordListener(val plugin: DiscordBridge, val api: DiscordAPI) : EventListener {

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

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

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

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

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

val broadcastMessage = "<" + username + ChatColor.AQUA + "(discord)" + ChatColor.RESET + "> " + e.msg.message
plugin.logDebug("Broadcasting message ${e.msg.id} from Discord as user $username")

val broadcastMessage = "<" + username + ChatColor.AQUA + "(discord)" + ChatColor.RESET + "> " + e.msg.message
plugin.server.broadcastMessage(broadcastMessage)
}
}
3 changes: 2 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ settings:
channel: 'test'
username: 'username'
email: '[email protected]'
password: 'password'
password: 'password'
debug: false

0 comments on commit 3ee2f05

Please sign in to comment.