Skip to content

Commit

Permalink
Add message templates and join/leave messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Nov 21, 2015
1 parent 27d8fe4 commit b96fdea
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 31 deletions.
31 changes: 31 additions & 0 deletions src/main/kotlin/gg/obsidian/discordbridge/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gg.obsidian.discordbridge

class Configuration(val plugin: Plugin) {

var SERVER_ID: String = ""
var CHANNEL: String = ""
var USERNAME: String = ""
var EMAIL: String = ""
var PASSWORD: String = ""
var DEBUG: Boolean = false
var TEMPLATES_DISCORD_CHAT_MESSAGE = ""
var TEMPLATES_DISCORD_PLAYER_JOIN = ""
var TEMPLATES_DISCORD_PLAYER_LEAVE = ""
var TEMPLATES_MINECRAFT_CHAT_MESSAGE = ""

fun load() {
plugin.reloadConfig()

SERVER_ID = plugin.config.getString("settings.server-id")
CHANNEL = plugin.config.getString("settings.channel")
USERNAME = plugin.config.getString("settings.username")
EMAIL = plugin.config.getString("settings.email")
PASSWORD = plugin.config.getString("settings.password")
DEBUG = plugin.config.getBoolean("settings.debug", false)

TEMPLATES_DISCORD_CHAT_MESSAGE = plugin.config.getString("settings.templates.discord.chat_message", "<%u> %m")
TEMPLATES_DISCORD_PLAYER_JOIN = plugin.config.getString("settings.templates.discord.player_join", "%u joined the server")
TEMPLATES_DISCORD_PLAYER_LEAVE = plugin.config.getString("settings.templates.discord.player_leave", "%u left the server")
TEMPLATES_MINECRAFT_CHAT_MESSAGE = plugin.config.getString("settings.templates.minecraft.chat_message", "<%u&b(discord)&r> %m")
}
}
10 changes: 5 additions & 5 deletions src/main/kotlin/gg/obsidian/discordbridge/DiscordConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import me.itsghost.jdiscord.Server
import me.itsghost.jdiscord.talkable.Group

class DiscordConnection(val plugin: Plugin) : Runnable {
var api: DiscordAPI = DiscordBuilder(plugin.email, plugin.password).build()
var api: DiscordAPI = DiscordBuilder(plugin.configuration.EMAIL, plugin.configuration.PASSWORD).build()
var server: Server? = null
var channel: Group? = null

Expand All @@ -20,14 +20,14 @@ class DiscordConnection(val plugin: Plugin) : Runnable {

}

fun send(name: String, message: String) {
server = if (server == null) getServerById(plugin.serverID) else server
fun send(message: String) {
server = if (server == null) getServerById(plugin.configuration.SERVER_ID) else server
if (server == null) return

channel = if (channel == null) getGroupByName(server!!, plugin.channel) else channel
channel = if (channel == null) getGroupByName(server!!, plugin.configuration.CHANNEL) else channel
if (channel == null) return

channel!!.sendMessage("<$name> $message")
channel!!.sendMessage(message)
}

private fun getServerById(id: String): Server? {
Expand Down
11 changes: 4 additions & 7 deletions src/main/kotlin/gg/obsidian/discordbridge/DiscordListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@ package gg.obsidian.discordbridge
import me.itsghost.jdiscord.DiscordAPI
import me.itsghost.jdiscord.event.EventListener
import me.itsghost.jdiscord.events.UserChatEvent
import org.bukkit.ChatColor

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

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

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

if (!e.group.name.equals(plugin.channel, true)) {
if (!e.group.name.equals(plugin.configuration.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)) {
if (username.equals(plugin.configuration.USERNAME, true)) {
plugin.logDebug("Ignoring message ${e.msg.id} from Discord: it matches the server's username")
return
}

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)
plugin.sendToMinecraft(username, e.msg.message)
}
}
65 changes: 46 additions & 19 deletions src/main/kotlin/gg/obsidian/discordbridge/Plugin.kt
Original file line number Diff line number Diff line change
@@ -1,58 +1,85 @@
package gg.obsidian.discordbridge

import org.bukkit.ChatColor
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.AsyncPlayerChatEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.java.JavaPlugin

class Plugin : JavaPlugin(), Listener {

var serverID: String = ""
var channel: String = ""
var username: String = ""
var email: String = ""
var password: String = ""
var debug: Boolean = false

val configuration = Configuration(this)
var connection: DiscordConnection? = null

override fun onEnable() {
updateConfig(description.version)

this.serverID = config.getString("settings.server-id")
this.channel = config.getString("settings.channel")
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)

server.scheduler.runTaskAsynchronously(this, connection)
server.pluginManager.registerEvents(this, this)
}

// Event Handlers

@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)
val username = ChatColor.stripColor(event.player.name)
val formattedMessage = configuration.TEMPLATES_DISCORD_CHAT_MESSAGE
.replace("%u", username)
.replace("%m", event.message)
sendToDiscord(formattedMessage)
}

fun send(name: String, message: String) {
logDebug("Sending chat message to Discord - $name: $message")
connection!!.send(name, message)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun onPlayerJoin(event: PlayerJoinEvent) {
val username = ChatColor.stripColor(event.player.name)
logDebug("Received a join event for $username")
val formattedMessage = configuration.TEMPLATES_DISCORD_PLAYER_JOIN
.replace("%u", username)
sendToDiscord(formattedMessage)
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun onPlayerQuit(event: PlayerQuitEvent) {
val username = ChatColor.stripColor(event.player.name)
logDebug("Received a leave event for $username")
val formattedMessage = configuration.TEMPLATES_DISCORD_PLAYER_LEAVE
.replace("%u", username)
sendToDiscord(formattedMessage)
}

// Message senders

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

fun sendToMinecraft(username: String, message: String) {
val formattedMessage = ChatColor.translateAlternateColorCodes('&',
configuration.TEMPLATES_MINECRAFT_CHAT_MESSAGE
.replace("%u", username)
.replace("%m", message))
server.broadcastMessage(formattedMessage)
}

// Utilities

fun updateConfig(version: String) {
this.saveDefaultConfig()
config.options().copyDefaults(true)
config.set("version", version)
saveConfig()
configuration.load()
}

fun logDebug(msg: String) {
if (!debug) return;
if (!configuration.DEBUG) return;
logger.info(msg)
}
}
7 changes: 7 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ settings:
email: '[email protected]'
password: 'password'
debug: false
templates:
discord:
chat_message: '<%u> %m'
player_join: '%u joined the server'
player_leave: '%u left the server'
minecraft:
chat_message: '<%u&b(discord)&r> %m'

0 comments on commit b96fdea

Please sign in to comment.