112 lines
4.5 KiB
Kotlin
112 lines
4.5 KiB
Kotlin
package nl.voidcorp.cordplugin
|
|
|
|
import khttp.get
|
|
import net.dv8tion.jda.api.JDA
|
|
import net.dv8tion.jda.api.JDABuilder
|
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent
|
|
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
|
import nl.voidcorp.mainplugin.VoidPluginBase
|
|
import nl.voidcorp.mainplugin.adapter
|
|
import nl.voidcorp.mainplugin.messaging.Message
|
|
import nl.voidcorp.mainplugin.messaging.MessageType
|
|
import nl.voidcorp.mainplugin.moshi
|
|
import org.bukkit.Bukkit
|
|
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
|
|
|
|
class VoidCord(override val comment: String = "Chat with Discord!") : VoidPluginBase() {
|
|
lateinit var conf: DiscordConfig
|
|
lateinit var jda: JDA
|
|
|
|
override fun enable() {
|
|
send("VoidPlugin", MessageType.GET_CONFIG)
|
|
send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter<DiscordConfig>().toJson(conf))
|
|
if (conf.botToken.isBlank()) {
|
|
logger.warning("Please add a bot token to the config, I can't send messages to discord without...")
|
|
this.disable()
|
|
return
|
|
}
|
|
|
|
|
|
jda = JDABuilder(conf.botToken).addEventListeners(EventManager(conf)).build()
|
|
server.pluginManager.registerEvents(ChatMapper(jda, conf), this)
|
|
}
|
|
|
|
class ChatMapper(val jda: JDA, val conf: DiscordConfig) : Listener {
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
fun onChat(evt: AsyncPlayerChatEvent) {
|
|
val msg = evt.message
|
|
if (msg.startsWith("/")) {
|
|
return
|
|
} else {
|
|
val channels = conf.channels.map { jda.getTextChannelById(it) }.filterNotNull()
|
|
channels.forEach {
|
|
it.sendMessage("(Minecraft) ${evt.player.displayName}: $msg").queue()
|
|
}
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
fun onJoin(evt: PlayerJoinEvent) {
|
|
val msg = "${evt.player.displayName} Joined!"
|
|
val channels = conf.channels.map { jda.getTextChannelById(it) }.filterNotNull()
|
|
channels.forEach {
|
|
it.sendMessage("(Minecraft) $msg").queue()
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
fun onLeave(evt: PlayerQuitEvent) {
|
|
val msg = "${evt.player.displayName} Left!"
|
|
val channels = conf.channels.map { jda.getTextChannelById(it) }.filterNotNull()
|
|
channels.forEach {
|
|
it.sendMessage("(Minecraft) $msg").queue()
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun disable() {
|
|
jda.shutdown()
|
|
}
|
|
|
|
override fun recieve(message: Message) {
|
|
when (message.messageType) {
|
|
MessageType.GET_CONFIG -> {
|
|
conf = moshi.adapter<DiscordConfig>().fromJson(message.content)!!
|
|
}
|
|
else -> Unit
|
|
}
|
|
}
|
|
|
|
class EventManager(private val config: DiscordConfig) : ListenerAdapter() {
|
|
private val italics = "(_.*_)|(\\*.*\\*)".toRegex()
|
|
private val bold = "(\\*\\*.*\\*\\*)".toRegex()
|
|
private val underline = "(__.*__)".toRegex()
|
|
private val strikethrough = "(~~.*~~)".toRegex()
|
|
override fun onGuildMessageReceived(event: GuildMessageReceivedEvent) {
|
|
if (event.author.idLong == event.jda.selfUser.idLong) return
|
|
if (event.channel.idLong in config.channels) {
|
|
val disc = event.message.contentDisplay
|
|
.replace(strikethrough) {
|
|
ChatColor.STRIKETHROUGH.toString() + it.groupValues[0].removeSurrounding("~~") + ChatColor.RESET.toString()
|
|
}
|
|
.replace(underline) {
|
|
ChatColor.UNDERLINE.toString() + it.groupValues[0].removeSurrounding("__") + ChatColor.RESET.toString()
|
|
}
|
|
.replace(bold) {
|
|
ChatColor.BOLD.toString() + it.groupValues[0].removeSurrounding("**") + ChatColor.RESET.toString()
|
|
}
|
|
.replace(italics) {
|
|
ChatColor.ITALIC.toString() + it.groupValues[0].removeSurrounding("_").removeSurrounding("*") + ChatColor.RESET.toString()
|
|
}
|
|
Bukkit.broadcastMessage("<${ChatColor.BLUE}${event.member!!.effectiveName}${ChatColor.RESET}>: $disc")
|
|
}
|
|
}
|
|
|
|
}
|
|
} |