Skip to content

Commit

Permalink
feat: urlReplacement for Discord -> Minecraft
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jan 12, 2024
1 parent 0a248e4 commit f29e33d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mineinabyss.chatty
import com.charleskorn.kaml.YamlComment
import com.mineinabyss.chatty.components.ChannelType
import com.mineinabyss.idofront.serialization.DurationSerializer
import com.mineinabyss.idofront.textcomponents.miniMsg
import kotlinx.serialization.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
Expand All @@ -29,7 +30,20 @@ data class ChattyConfig(
@YamlComment("Valid formats: STRIKETHROUGH, CENSOR, DELETE, BLOCK", "STRIKETHROUGH: Replaces filtered words with a strikethrough", "CENSOR: Replaces filtered words with a censor", "DELETE: Deletes filtered words", "BLOCK: Blocks filtered words from being sent")
val filterFormat: FilterFormat = FilterFormat.CENSOR,
@SerialName("filters") val _filters: List<String> = listOf(),
val formatURLs: Boolean = true,
val urlReplacements: Set<UrlReplacements> = setOf(
UrlReplacements("^https:\\/\\/cdn\\.discordapp\\.com\\/attachments\\/[^\\/]+\\/[^\\/]+\\.png\\?.*\$", "[Discord Image]"),
UrlReplacements("^https:\\/\\/cdn\\.discordapp\\.com\\/attachments\\/[^\\/]+\\/[^\\/]+\\.mp4\\?.*\$", "[Discord Video]"),
UrlReplacements("youtube.com", "[YouTube]"),
)
) {

@Serializable
data class UrlReplacements(private val pattern: String, @SerialName("replacement") private val _replacement: String) {
@Transient val regex = pattern.toRegex()
@Transient val replacement = _replacement.miniMsg()
}

enum class FilterFormat {
STRIKETHROUGH, CENSOR, DELETE, BLOCK
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,16 @@ fun handleChatFilters(message: Component, player: Player, audience: Player?) : C
}

return finalMessage.compact().takeIf { it != Component.empty() }
}

fun handleUrlReplacements(message: Component, player: Player?): Component {
var component = message
component.clickEvent()?.takeIf { it.action() == ClickEvent.Action.OPEN_URL }?.let { clickEvent ->
val (regex, textReplacement) = chatty.config.chat.urlReplacements.firstOrNull { it.regex in clickEvent.value() }?.let { it.regex to it.replacement } ?: return@let
component = component.replaceText(TextReplacementConfig.builder().match(regex.pattern).replacement(textReplacement).build())

val hoverComponent = Component.text(clickEvent.value()).style(Style.style(TextDecoration.UNDERLINED))
component = component.hoverEvent(HoverEventSource.unbox(HoverEvent.showText(hoverComponent)))
}
return component.children(component.children().map { handleUrlReplacements(it, player) })
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mineinabyss.chatty.chatty
import com.mineinabyss.chatty.chattyProxyChannel
import com.mineinabyss.chatty.components.ChannelData
import com.mineinabyss.chatty.helpers.handleChatFilters
import com.mineinabyss.chatty.helpers.handleUrlReplacements
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
Expand All @@ -15,6 +16,7 @@ import github.scarsz.discordsrv.dependencies.jda.api.entities.Message
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import github.scarsz.discordsrv.objects.MessageFormat
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component as ComponentDSV

Expand All @@ -27,16 +29,21 @@ class DiscordListener {

@Subscribe(priority = ListenerPriority.HIGHEST)
fun DiscordGuildMessagePostProcessEvent.sendDiscordToProxy() {
chatty.plugin.server.sendPluginMessage(chatty.plugin, chattyProxyChannel, gson.serialize(ComponentDSV.textOfChildren(minecraftMessage)).toByteArray())
val minecraftMessage = ComponentDSV.textOfChildren(if (chatty.config.chat.formatURLs) handleUrlReplacements(minecraftMessage.toComponent(), null).toComponentDSV() else minecraftMessage)
chatty.plugin.server.sendPluginMessage(chatty.plugin, chattyProxyChannel, gson.serialize(minecraftMessage).toByteArray())
setMinecraftMessage(minecraftMessage)
}

private fun ComponentDSV.toComponent() = mm.serialize(this).miniMsg()
private fun Component.toComponentDSV() = mm.deserialize(this.serialize())

@Subscribe(priority = ListenerPriority.NORMAL)
fun GameChatMessagePreProcessEvent.onChat() {
val channel = player.toGeary().get<ChannelData>()?.channel ?: return
val filteredMessage = handleChatFilters(mm.serialize(messageComponent).miniMsg(), player, null)
val filteredMessage = handleChatFilters(messageComponent.toComponent(), player, null)?.toComponentDSV()

if (!channel.discordsrv || filteredMessage == null) isCancelled = true
else messageComponent = mm.deserialize(filteredMessage.serialize())
else messageComponent = filteredMessage
}

@Subscribe
Expand Down

0 comments on commit f29e33d

Please sign in to comment.