From ead068c1826d6dc483e5658fd825a51a7f1c09a3 Mon Sep 17 00:00:00 2001 From: Erdragh Date: Sun, 3 Dec 2023 15:10:44 +0100 Subject: [PATCH] Implement rudimentary minecraft -> discord chat sync --- .../src/main/kotlin/dev/erdragh/astralbot/Bot.kt | 15 ++++++++------- .../astralbot/handlers/MinecraftHandler.kt | 15 ++++++++++++++- .../kotlin/dev/erdragh/astralbot/fabric/BotMod.kt | 10 ++++++---- .../kotlin/dev/erdragh/astralbot/forge/BotMod.kt | 7 +++++++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/common/src/main/kotlin/dev/erdragh/astralbot/Bot.kt b/common/src/main/kotlin/dev/erdragh/astralbot/Bot.kt index 9c14d7b..abf958d 100644 --- a/common/src/main/kotlin/dev/erdragh/astralbot/Bot.kt +++ b/common/src/main/kotlin/dev/erdragh/astralbot/Bot.kt @@ -18,7 +18,12 @@ var jda: JDA? = null lateinit var baseDirectory: File fun startAstralbot(server: MinecraftServer) { - minecraftHandler = MinecraftHandler(server) + val env = System.getenv() + if (!env.containsKey("DISCORD_TOKEN")) { + LOGGER.warn("Not starting AstralBot because of missing DISCORD_TOKEN environment variable.") + return + } + baseDirectory = File(server.serverDirectory, MODID) if (baseDirectory.mkdir()) { LOGGER.debug("Created $MODID directory") @@ -26,12 +31,6 @@ fun startAstralbot(server: MinecraftServer) { FAQHandler.start() - val env = System.getenv() - if (!env.containsKey("DISCORD_TOKEN")) { - LOGGER.warn("Not starting AstralBot because of missing DISCORD_TOKEN environment variable.") - return - } - jda = JDABuilder.createLight( env["DISCORD_TOKEN"], GatewayIntent.MESSAGE_CONTENT, @@ -39,6 +38,8 @@ fun startAstralbot(server: MinecraftServer) { GatewayIntent.GUILD_MEMBERS ).addEventListeners(CommandHandlingListener).build() + minecraftHandler = MinecraftHandler(server, jda) + // This makes sure that the extra parallel tasks from this // mod/bot combo get shut down even if the Server Shutdown // Event never gets triggered. diff --git a/common/src/main/kotlin/dev/erdragh/astralbot/handlers/MinecraftHandler.kt b/common/src/main/kotlin/dev/erdragh/astralbot/handlers/MinecraftHandler.kt index 716de1a..eb48bdc 100644 --- a/common/src/main/kotlin/dev/erdragh/astralbot/handlers/MinecraftHandler.kt +++ b/common/src/main/kotlin/dev/erdragh/astralbot/handlers/MinecraftHandler.kt @@ -1,7 +1,12 @@ package dev.erdragh.astralbot.handlers import com.mojang.authlib.GameProfile +import net.dv8tion.jda.api.JDA +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel +import net.minecraft.network.chat.ChatType +import net.minecraft.network.chat.PlayerChatMessage import net.minecraft.server.MinecraftServer +import net.minecraft.server.level.ServerPlayer import java.util.* import kotlin.jvm.optionals.getOrNull @@ -10,7 +15,9 @@ import kotlin.jvm.optionals.getOrNull * methods for fetching [GameProfile]s * @author Erdragh */ -class MinecraftHandler(private val server: MinecraftServer) { +class MinecraftHandler(private val server: MinecraftServer, private val api: JDA?) { + private var channel: TextChannel? = null + /** * Fetches all currently online players' [GameProfile]s * @return a [Collection] of all currently online players' @@ -45,4 +52,10 @@ class MinecraftHandler(private val server: MinecraftServer) { fun byName(name: String): GameProfile? { return server.profileCache?.get(name)?.getOrNull() } + + fun sendChatToDiscord(player: ServerPlayer, message: String) { + if (channel == null) channel = api?.getTextChannelsByName("chat", true)?.get(0) + channel?.sendMessage("<${player.name.string}> $message")?.setSuppressedNotifications(true) + ?.setSuppressEmbeds(true)?.queue() + } } \ No newline at end of file diff --git a/fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt b/fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt index 0074664..7435f8d 100644 --- a/fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt +++ b/fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt @@ -1,13 +1,11 @@ package dev.erdragh.astralbot.fabric -import dev.erdragh.astralbot.LOGGER -import dev.erdragh.astralbot.MODID +import dev.erdragh.astralbot.* import dev.erdragh.astralbot.config.AstralBotConfig -import dev.erdragh.astralbot.startAstralbot -import dev.erdragh.astralbot.stopAstralbot import fuzs.forgeconfigapiport.api.config.v2.ForgeConfigRegistry import net.fabricmc.api.ModInitializer import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents +import net.fabricmc.fabric.api.message.v1.ServerMessageEvents import net.minecraftforge.fml.config.ModConfig object BotMod : ModInitializer { @@ -22,5 +20,9 @@ object BotMod : ModInitializer { ServerLifecycleEvents.SERVER_STOPPING.register { stopAstralbot() } + + ServerMessageEvents.CHAT_MESSAGE.register { message, player, _ -> + minecraftHandler?.sendChatToDiscord(player, message.signedContent()) + } } } \ No newline at end of file diff --git a/forge/src/main/kotlin/dev/erdragh/astralbot/forge/BotMod.kt b/forge/src/main/kotlin/dev/erdragh/astralbot/forge/BotMod.kt index 73232cb..2fcf34e 100644 --- a/forge/src/main/kotlin/dev/erdragh/astralbot/forge/BotMod.kt +++ b/forge/src/main/kotlin/dev/erdragh/astralbot/forge/BotMod.kt @@ -2,8 +2,10 @@ package dev.erdragh.astralbot.forge import dev.erdragh.astralbot.LOGGER import dev.erdragh.astralbot.config.AstralBotConfig +import dev.erdragh.astralbot.minecraftHandler import dev.erdragh.astralbot.startAstralbot import dev.erdragh.astralbot.stopAstralbot +import net.minecraftforge.event.ServerChatEvent import net.minecraftforge.event.server.ServerStartedEvent import net.minecraftforge.event.server.ServerStoppingEvent import net.minecraftforge.fml.ModLoadingContext @@ -17,6 +19,7 @@ object BotMod { ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, AstralBotConfig.SPEC) FORGE_BUS.addListener(::onServerStart) FORGE_BUS.addListener(::onServerStop) + FORGE_BUS.addListener(::onChatMessage) } private fun onServerStart(event: ServerStartedEvent) { @@ -27,4 +30,8 @@ object BotMod { private fun onServerStop(event: ServerStoppingEvent) { stopAstralbot() } + + private fun onChatMessage(event: ServerChatEvent) { + minecraftHandler?.sendChatToDiscord(event.player, event.message.string) + } } \ No newline at end of file