Skip to content

Commit

Permalink
fix: block-formatting sending messages repeatedly
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 12, 2024
1 parent 0c7390f commit bbf42ca
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ChattyMessages(
data class ChatFilter(
val blockMessage: String = "<yellow>⚠<gray>Your message contained blocked words:<i>",
val deleteWordsEmptyMessage: String = "<yellow>⚠<gray>Your message was not sent as it contained only blocked words.",
val notifyStaff: String = "<yellow>⚠<gray><chatty_nickname> sent a message containing blocked words:<i>"
val notifyStaff: String = "<yellow>⚠<gray><chatty_nickname> sent a message containing blocked words: <i>"
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ fun formatPlayerPingMessage(source: Player, pingedPlayer: Player?, audience: Aud
} ?: message
}

fun handleChatFilters(message: Component, player: Player?, audience: Player?) : Component? {
fun handleChatFilters(message: Component, player: Player?, audience: Player?, silent: Boolean) : Component? {
var finalMessage = message
val serialized = finalMessage.asFlatTextContent()
if (player?.hasPermission(ChattyPermissions.BYPASS_CHAT_FILTERS_PERM) == true) return finalMessage
val staffAudience = audience?.takeUnless { silent }?.let { listOf(it) } ?: Bukkit.getServer().onlinePlayers.filter { !silent && it.hasPermission(ChattyPermissions.MODERATION_PERM) }

val matchResults = chatty.config.chat.filters.flatMap { it.regex.findAll(serialized).map { m -> m to it.format } }
val blockedWords = matchResults.joinToString(", ") { it.first.value }
val blockedWords = matchResults.distinctBy { it.first.value }.joinToString(", "){ it.first.value }
matchResults.forEach { (match, format) ->
finalMessage = finalMessage.replaceText(TextReplacementConfig.builder()
.matchLiteral(match.value)
Expand All @@ -194,13 +195,12 @@ fun handleChatFilters(message: Component, player: Player?, audience: Player?) :
FilterFormat.CENSOR -> Component.text("".repeat(match.value.length))
FilterFormat.DELETE -> Component.empty()
FilterFormat.BLOCK -> {
player?.sendFormattedMessage(chatty.messages.chatFilter.blockMessage, blockedWords)
if (audience?.hasPermission(ChattyPermissions.MODERATION_PERM) == true)
audience.sendFormattedMessage(chatty.messages.chatFilter.notifyStaff + blockedWords)
player?.takeUnless { silent }?.sendFormattedMessage(chatty.messages.chatFilter.blockMessage, blockedWords)
staffAudience.forEach { it.sendFormattedMessage(chatty.messages.chatFilter.notifyStaff + blockedWords, optionalPlayer = player) }
return null
}
}.takeIf { it != Component.empty() }?.let {
if (audience?.hasPermission(ChattyPermissions.MODERATION_PERM) == true)
if (!silent && audience?.hasPermission(ChattyPermissions.MODERATION_PERM) == true)
it.hoverEventShowText(Component.text(match.value.toCharArray().joinToString(Space.PLUS_1.unicode)).style(Style.style(TextDecoration.ITALIC)))
else it
} ?: Component.empty()
Expand All @@ -211,9 +211,8 @@ fun handleChatFilters(message: Component, player: Player?, audience: Player?) :
// If filterFormat is DELETE and message is empty, aka only containing blocked words
// Give feedback to player and notify staff
if (finalMessage == Component.empty() && matchResults.any { it.second == FilterFormat.DELETE }) {
if (audience == player) player?.sendFormattedMessage(chatty.messages.chatFilter.deleteWordsEmptyMessage)
else if (audience?.hasPermission(ChattyPermissions.MODERATION_PERM) == true)
audience.sendFormattedMessage(chatty.messages.chatFilter.notifyStaff, blockedWords, optionalPlayer = player)
if (audience == player && !silent) player?.sendFormattedMessage(chatty.messages.chatFilter.deleteWordsEmptyMessage)
staffAudience.forEach { it.sendFormattedMessage(chatty.messages.chatFilter.notifyStaff, blockedWords, optionalPlayer = player) }
}

return finalMessage.compact().takeIf { it != Component.empty() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ class ChatListener : Listener {
handleProxyMessage(player, channelId, channel, message(), simpleMessage)

val pingedPlayer = originalMessage().serialize().checkForPlayerPings(channelId)
val playerViewers = viewers().mapNotNull { it as? Player }.toSet()
val playerViewers = viewers().mapNotNull { it as? Player }.toMutableSet()
when {
playerViewers.isEmpty() -> player.sendFormattedMessage(chatty.messages.channels.emptyChannelMessage)
chatty.config.chat.disableChatSigning -> {
if (handleChatFilters(baseMessage, null, null, false) == null) playerViewers.clear()
playerViewers.forEach { receiver ->
var finalMessage = message()
finalMessage = handleChatFilters(finalMessage, player, receiver) ?: return@forEach
finalMessage = handleChatFilters(finalMessage, player, receiver, false) ?: return@forEach
finalMessage = formatPlayerPingMessage(player, pingedPlayer, receiver, finalMessage)
finalMessage = formatModerationMessage(
channel.messageDeletion,
Expand All @@ -105,22 +106,25 @@ class ChatListener : Listener {
//isCancelled = true
}

else -> renderer { source, _, message, audience ->
var finalMessage = message
finalMessage = handleChatFilters(finalMessage, player, audience as? Player)
?: return@renderer Component.empty()
finalMessage = formatPlayerPingMessage(source, pingedPlayer, audience, finalMessage)
finalMessage = formatModerationMessage(
channel.messageDeletion,
finalMessage,
simpleMessage,
signedMessage(),
audience,
source,
playerViewers
)

return@renderer finalMessage
else -> {
if (handleChatFilters(baseMessage, player, null, false) == null) viewers().clear()
renderer { source, _, message, audience ->
var finalMessage = message
finalMessage = handleChatFilters(finalMessage, player, audience as? Player, false)
?: return@renderer Component.empty()
finalMessage = formatPlayerPingMessage(source, pingedPlayer, audience, finalMessage)
finalMessage = formatModerationMessage(
channel.messageDeletion,
finalMessage,
simpleMessage,
signedMessage(),
audience,
source,
playerViewers
)

return@renderer finalMessage
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DiscordListener {
val channelId = ComponentDSV.text(channel.key)
val simpleMessage = mm.deserialize(message.author.name + ": " + message.contentRaw)
var message = handleUrlReplacements(minecraftMessage.toComponent(), null)
message = handleChatFilters(message, null, null) ?: run { isCancelled = true; return }
message = handleChatFilters(message, null, null, true) ?: run { isCancelled = true; return }

val minecraftMessage = ComponentDSV.textOfChildren(senderName, channelId, message.toComponentDSV(), simpleMessage)
chatty.plugin.server.sendPluginMessage(chatty.plugin, discordSrvChannel, gson.serialize(minecraftMessage).toByteArray())
Expand All @@ -50,7 +50,7 @@ class DiscordListener {
fun GameChatMessagePreProcessEvent.onChat() {
val channel = player.toGeary().get<ChannelData>()?.withChannelVerified()?.channel ?: return
val baseMessage = messageComponent.children().last().toComponent()
val filteredMessage = handleChatFilters(baseMessage, player, null)?.toComponentDSV()
val filteredMessage = handleChatFilters(baseMessage, player, null, true)?.toComponentDSV()
if (!channel.discordsrv || filteredMessage == null) isCancelled = true
else messageComponent = filteredMessage
}
Expand Down

0 comments on commit bbf42ca

Please sign in to comment.