From e45f46dc710fdec41ea6902af47293fcfce7c1d9 Mon Sep 17 00:00:00 2001 From: Logan Gorence Date: Thu, 23 Dec 2021 00:39:36 +0000 Subject: [PATCH] Add join/quit messages to Bifrost. --- .../bifrost/FoundationBifrostPlugin.kt | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/foundation-bifrost/src/main/kotlin/cloud/kubelet/foundation/bifrost/FoundationBifrostPlugin.kt b/foundation-bifrost/src/main/kotlin/cloud/kubelet/foundation/bifrost/FoundationBifrostPlugin.kt index d0bceb3..0946686 100644 --- a/foundation-bifrost/src/main/kotlin/cloud/kubelet/foundation/bifrost/FoundationBifrostPlugin.kt +++ b/foundation-bifrost/src/main/kotlin/cloud/kubelet/foundation/bifrost/FoundationBifrostPlugin.kt @@ -5,8 +5,11 @@ import cloud.kubelet.foundation.core.FoundationCorePlugin import cloud.kubelet.foundation.core.Util import com.charleskorn.kaml.Yaml import io.papermc.paper.event.player.AsyncChatEvent +import net.dv8tion.jda.api.EmbedBuilder import net.dv8tion.jda.api.JDA import net.dv8tion.jda.api.JDABuilder +import net.dv8tion.jda.api.MessageBuilder +import net.dv8tion.jda.api.entities.TextChannel import net.dv8tion.jda.api.events.GenericEvent import net.dv8tion.jda.api.events.message.MessageReceivedEvent import net.dv8tion.jda.api.hooks.EventListener @@ -14,7 +17,11 @@ import net.kyori.adventure.text.Component import net.kyori.adventure.text.TextComponent import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerAdvancementDoneEvent +import org.bukkit.event.player.PlayerJoinEvent +import org.bukkit.event.player.PlayerQuitEvent import org.bukkit.plugin.java.JavaPlugin +import java.awt.Color import kotlin.io.path.inputStream class FoundationBifrostPlugin : JavaPlugin(), EventListener, Listener { @@ -50,15 +57,48 @@ class FoundationBifrostPlugin : JavaPlugin(), EventListener, Listener { } } - @EventHandler - private fun onPlayerChat(e: AsyncChatEvent) { + private fun getChannel(): TextChannel? { val channel = jda.getTextChannelById(config.channel.id) if (channel == null) { slF4JLogger.error("Failed to retrieve channel ${config.channel.id}") - return } + return channel + } + private fun message(f: MessageBuilder.() -> Unit) = MessageBuilder().apply(f).build() + private fun MessageBuilder.embed(f: EmbedBuilder.() -> Unit) { + setEmbeds(EmbedBuilder().apply(f).build()) + } + + @EventHandler + private fun onPlayerJoin(e: PlayerJoinEvent) { + val channel = getChannel() ?: return + + channel.sendMessage(message { + embed { + setAuthor("${e.player.name} joined the server") + setColor(Color.GREEN) + } + }).queue() + } + + @EventHandler + private fun onPlayerQuit(e: PlayerQuitEvent) { + val channel = getChannel() ?: return + + channel.sendMessage(message { + embed { + setAuthor("${e.player.name} left the server") + setColor(Color.RED) + } + }).queue() + } + + @EventHandler + private fun onPlayerChat(e: AsyncChatEvent) { + val channel = getChannel() ?: return val message = e.message() + if (message is TextComponent) { channel.sendMessage("${e.player.name}: ${message.content()}").queue() } else {