Add basic channel and chat bridge.

This commit is contained in:
Logan Gorence
2021-12-22 01:38:22 +00:00
parent be7df6dcc6
commit 69380906e6
8 changed files with 144 additions and 14 deletions

View File

@ -1,5 +1,7 @@
dependencies {
implementation("net.dv8tion:JDA:5.0.0-alpha.2")
implementation("net.dv8tion:JDA:5.0.0-alpha.2") {
exclude(module = "opus-java")
}
compileOnly(project(":foundation-core"))
}

View File

@ -1,13 +1,68 @@
package cloud.kubelet.foundation.bifrost
import cloud.kubelet.foundation.bifrost.model.BifrostConfig
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.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.hooks.EventListener
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.plugin.java.JavaPlugin
import kotlin.io.path.inputStream
class FoundationBifrostPlugin : JavaPlugin(), EventListener, Listener {
private lateinit var config: BifrostConfig
private lateinit var jda: JDA
class FoundationBifrostPlugin : JavaPlugin() {
override fun onEnable() {
slF4JLogger.info("Enabling!")
val foundation = server.pluginManager.getPlugin("Foundation") as FoundationCorePlugin
slF4JLogger.info("Plugin data path: ${foundation.pluginDataPath}")
val configPath = Util.copyDefaultConfig(slF4JLogger, foundation.pluginDataPath, "bifrost.yaml")
config = Yaml.default.decodeFromStream(BifrostConfig.serializer(), configPath.inputStream())
server.pluginManager.registerEvents(this, this)
jda = JDABuilder
.createDefault(config.authentication.token)
.addEventListeners(this)
.build()
}
override fun onEvent(e: GenericEvent) {
when (e) {
is MessageReceivedEvent -> {
// Prevent this bot from receiving its own messages and creating a feedback loop.
if (e.author.id == jda.selfUser.id) return
slF4JLogger.debug(
"${e.guild.name} - ${e.channel.name} - ${e.author.name}: ${e.message.contentDisplay}"
)
server.sendMessage(Component.text("${e.author.name} - ${e.message.contentDisplay}"))
}
}
}
@EventHandler
private fun onPlayerChat(e: AsyncChatEvent) {
val channel = jda.getTextChannelById(config.channel.id)
if (channel == null) {
slF4JLogger.error("Failed to retrieve channel ${config.channel.id}")
return
}
val message = e.message()
if (message is TextComponent) {
channel.sendMessage("${e.player.name}: ${message.content()}").queue()
} else {
slF4JLogger.error("Not sure what to do here, message != TextComponent: ${message.javaClass}")
}
}
}

View File

@ -0,0 +1,19 @@
package cloud.kubelet.foundation.bifrost.model
import kotlinx.serialization.Serializable
@Serializable
data class BifrostConfig(
val authentication: BifrostAuthentication,
val channel: BifrostChannel,
)
@Serializable
data class BifrostAuthentication(
val token: String,
)
@Serializable
data class BifrostChannel(
val id: String,
)