Skip to content

Commit

Permalink
records: truncate embed values possibly exceeding field limit
Browse files Browse the repository at this point in the history
  • Loading branch information
lokka30 committed Oct 23, 2024
1 parent b3ec817 commit 528e116
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.bson.BsonDateTime
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.types.ObjectId
import space.lachy.lachsbot.util.StringExtensions.truncate
import java.awt.Color
import java.util.*

Expand Down Expand Up @@ -56,7 +57,7 @@ data class RecordMessageDelete(
return EmbedBuilder()
.setTitle("Message Deleted")
.setDescription("Discord's API does not advise who caused this, use Audit Log to check.")
.addField("Last Known Message Raw", messageRawLastKnown ?: "N/A", false)
.addField("Last Known Message Raw", messageRawLastKnown?.truncate() ?: "N/A", false)
.addField("Message ID", "$messageId", true)
.addField("Timestamp Epoch", "${timestampEpoch.value}", true)
.addField("Timestamp FMT", "${Date(timestampEpoch.value)}", true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package space.lachy.lachsbot.database.mongo.collection

import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.entities.MessageEmbed.VALUE_MAX_LENGTH
import org.bson.BsonDateTime
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.types.ObjectId
import space.lachy.lachsbot.listener.record.RecordListenerManager
import space.lachy.lachsbot.util.StringExtensions.truncate
import java.awt.Color
import java.util.*

Expand Down Expand Up @@ -59,7 +61,7 @@ data class RecordMessageReceived(
fun toEmbedBuilder(): EmbedBuilder {
val builder = EmbedBuilder()
.setTitle("Message Received")
.addField("Message", messageRaw, false)
.addField("Message", messageRaw.truncate(VALUE_MAX_LENGTH), false)
.addField("Message ID", "$messageId", true)
.addField("Timestamp Epoch", "${timestampEpoch.value}", true)
.addField("Timestamp FMT", "${Date(timestampEpoch.value)}", true)
Expand All @@ -72,7 +74,7 @@ data class RecordMessageReceived(
.addField("Author Username", authorUsername, true)
.addField("Author Effective Name", authorEffectiveName, true)
.addField("Author Tag", "<@${authorId}>", true)
.addField("Jump URL", jumpUrl, true)
.addField("Jump URL", jumpUrl.truncate(VALUE_MAX_LENGTH), true)
.setFooter("${javaClass.simpleName} - ID#$id")
.setColor(Color.LIGHT_GRAY)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.types.ObjectId
import space.lachy.lachsbot.listener.record.RecordListenerManager
import space.lachy.lachsbot.util.StringExtensions.truncate
import java.awt.Color
import java.util.*

Expand Down Expand Up @@ -65,8 +66,8 @@ data class RecordMessageUpdate(
fun toEmbedBuilder(): EmbedBuilder {
val builder = EmbedBuilder()
.setTitle("Message Updated")
.addField("New Message Raw", messageRawNew, false)
.addField("Old Message Raw Last Known", messageRawLastKnown ?: "N/A", false)
.addField("New Message Raw", messageRawNew.truncate(), false)
.addField("Old Message Raw Last Known", messageRawLastKnown?.truncate() ?: "N/A", false)
.addField("Message ID", "$messageId", true)
.addField("Timestamp Epoch", "${timestampEpoch.value}", true)
.addField("Timestamp FMT", "${Date(timestampEpoch.value)}", true)
Expand All @@ -81,7 +82,7 @@ data class RecordMessageUpdate(
.addField("Author Username", authorUsername, true)
.addField("Author Effective Name", authorEffectiveName, true)
.addField("Author Tag", "<@${authorId}>", true)
.addField("Jump URL", jumpUrl, true)
.addField("Jump URL", jumpUrl.truncate(), true)
.setFooter("${javaClass.simpleName} - ID#$id")
.setColor(Color.LIGHT_GRAY)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import space.lachy.lachsbot.listener.record.delegate.MessageReceivedRecordListen
import space.lachy.lachsbot.listener.record.delegate.MessageUpdateRecordListener
import space.lachy.lachsbot.listener.record.delegate.StatusChangeRecordListener
import space.lachy.lachsbot.listener.record.delegate.UserUpdateOnlineStatusRecordListener
import space.lachy.lachsbot.util.StringExtensions.truncate

object RecordListenerManager {

Expand Down Expand Up @@ -82,7 +83,7 @@ object RecordListenerManager {
for(att in attachments) {
builder.addField(
"Attachment ${i}",
"Name: '${att.name}'; Extension: '${att.extension}'; URL: '${att.url}'",
"Name: '${att.name}'; Extension: '${att.extension}'; URL: '${att.url}'".truncate(),
false
)

Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/space/lachy/lachsbot/util/StringExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package space.lachy.lachsbot.util

import kotlin.math.min

object StringExtensions {

private val humps = "(?<=.)(?=\\p{Upper})".toRegex()

fun String.toSnakeCase() = replace(humps, "_").lowercase()

fun String.truncate(newLength: Int): String {
if(isEmpty() || newLength <= 3 || length <= newLength) {
return this
}

return take(min(length, newLength) - 3) + "..."
}

}

0 comments on commit 528e116

Please sign in to comment.