Skip to content

Commit

Permalink
Implement rudimentary minecraft -> discord chat sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdragh committed Dec 3, 2023
1 parent b01f626 commit ead068c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
15 changes: 8 additions & 7 deletions common/src/main/kotlin/dev/erdragh/astralbot/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ 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")
}

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,
GatewayIntent.GUILD_MESSAGES,
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'
Expand Down Expand Up @@ -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()
}
}
10 changes: 6 additions & 4 deletions fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -22,5 +20,9 @@ object BotMod : ModInitializer {
ServerLifecycleEvents.SERVER_STOPPING.register {
stopAstralbot()
}

ServerMessageEvents.CHAT_MESSAGE.register { message, player, _ ->
minecraftHandler?.sendChatToDiscord(player, message.signedContent())
}
}
}
7 changes: 7 additions & 0 deletions forge/src/main/kotlin/dev/erdragh/astralbot/forge/BotMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -27,4 +30,8 @@ object BotMod {
private fun onServerStop(event: ServerStoppingEvent) {
stopAstralbot()
}

private fun onChatMessage(event: ServerChatEvent) {
minecraftHandler?.sendChatToDiscord(event.player, event.message.string)
}
}

0 comments on commit ead068c

Please sign in to comment.