Skip to content

Commit

Permalink
fix: replace instances of C-style string formatting to string interpo…
Browse files Browse the repository at this point in the history
…lation, toId()
  • Loading branch information
mazziechai committed Jan 21, 2025
1 parent e89726f commit c9e6fa9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/main/kotlin/cafe/ferret/kosekata/_Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (c) 2023 mazziechai
*/

@file:OptIn(ExperimentalStdlibApi::class)

package cafe.ferret.kosekata

import cafe.ferret.kosekata.database.entities.Note
Expand All @@ -20,6 +22,11 @@ import dev.kordex.core.types.EphemeralInteractionContext
import kotlinx.datetime.Instant
import java.text.SimpleDateFormat

private val ID_FORMAT = HexFormat {
number.minLength = 6
number.removeLeadingZeros = true
}

/**
* Formats an Instant to a string.
*/
Expand All @@ -28,6 +35,7 @@ fun formatTime(instant: Instant): String {
return format.format(instant.epochSeconds * 1000L) + " UTC"
}

@OptIn(ExperimentalStdlibApi::class)
suspend fun FollowupMessageCreateBuilder.noteEmbed(kord: Kord, note: Note, verbose: Boolean) {
val noteUser = kord.getUser(note.author)
val noteMember = noteUser?.asMemberOrNull(note.guild)
Expand All @@ -52,7 +60,7 @@ suspend fun FollowupMessageCreateBuilder.noteEmbed(kord: Kord, note: Note, verbo

footer {
text = buildString {
append("#%06x ".format(note._id))
append(note._id.toId())

if (verbose) {
append("| Created on ${formatTime(note.timeCreated)} UTC ")
Expand Down Expand Up @@ -105,7 +113,7 @@ suspend fun EphemeralInteractionContext.guildNotes(
}

append("${user?.username ?: "Unknown user"}")
append("*${note.name}* | #%06x | ".format(note._id))
append("*${note.name}* | ${note._id.toId()} | ")
append("Created on ${note.timeCreated.toDiscord(TimestampType.ShortDate)} ")
appendLine("at ${note.timeCreated.toDiscord(TimestampType.ShortTime)}")
}
Expand All @@ -118,3 +126,6 @@ suspend fun EphemeralInteractionContext.guildNotes(
}
}.send()
}

fun Int.toId(): String = this.toHexString(ID_FORMAT).removePrefix("00")

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
* Copyright (c) 2023 mazziechai
*/

@file:OptIn(ExperimentalStdlibApi::class)

package cafe.ferret.kosekata.extensions

import cafe.ferret.kosekata.ByIdArgs
import cafe.ferret.kosekata.database.collections.NoteCollection
import cafe.ferret.kosekata.i18n.Translations
import cafe.ferret.kosekata.toId
import dev.kord.common.entity.Permission
import dev.kordex.core.checks.anyGuild
import dev.kordex.core.commands.Arguments
Expand Down Expand Up @@ -60,7 +63,10 @@ class AliasExtension : Extension() {

respond {
content =
Translations.Extensions.Alias.New.success.translate(arguments.alias, "%06x".format(noteId))
Translations.Extensions.Alias.New.success.translate(
arguments.alias,
note._id.toId()
)
}
}
}
Expand Down Expand Up @@ -103,7 +109,7 @@ class AliasExtension : Extension() {
respond {
content = Translations.Extensions.Alias.Remove.success.translate(
arguments.alias,
"%06x".format(noteId)
note._id.toId()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
* Copyright (c) 2023 mazziechai
*/

@file:OptIn(ExperimentalStdlibApi::class)

package cafe.ferret.kosekata.extensions

import cafe.ferret.kosekata.database.collections.NoteCollection
import cafe.ferret.kosekata.i18n.Translations
import cafe.ferret.kosekata.toId
import dev.kord.common.entity.Permission
import dev.kord.core.behavior.edit
import dev.kord.core.event.message.ReactionAddEvent
Expand Down Expand Up @@ -54,9 +57,10 @@ class ChatCommandsExtension : Extension() {
val references = referenceRegex.findAll(note.content).distinctBy { it.groupValues[1] }

message.respond {
content = "${note.content}\n\n`#%06x` `%s`".format(note._id, note.name)
content = "${note.content}\n\n`${note._id.toId()}` `${note.name}`"
if (references.any()) {
content += "\nThis note contains note references, which are only available in the slash command equivalents of this command."
content += "\nThis note contains note references, which are only available in the slash " +
"command equivalents of this command."
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
* Copyright (c) 2023 mazziechai
*/

@file:OptIn(ExperimentalStdlibApi::class)

package cafe.ferret.kosekata.extensions

import cafe.ferret.kosekata.database.collections.NoteCollection
import cafe.ferret.kosekata.i18n.Translations
import cafe.ferret.kosekata.noteEmbed
import cafe.ferret.kosekata.toId
import dev.kordex.core.checks.anyGuild
import dev.kordex.core.components.forms.ModalForm
import dev.kordex.core.extensions.Extension
Expand Down Expand Up @@ -42,7 +45,8 @@ class CreationExtension : Extension() {
)

respond {
content = Translations.Extensions.Creation.success.translate(noteName, "%06x".format(note._id))
content =
Translations.Extensions.Creation.success.translate(noteName, note._id.toId())
}
}
}
Expand All @@ -63,7 +67,8 @@ class CreationExtension : Extension() {
val note = noteCollection.new(user.id, guild!!.id, noteName, mutableListOf(noteName), noteContent)

respond {
content = Translations.Extensions.Creation.success.translate(noteName, "%06x".format(note._id))
content =
Translations.Extensions.Creation.success.translate(noteName, note._id.toId())
noteEmbed(this@publicSlashCommand.kord, note, false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
* Copyright (c) 2023 mazziechai
*/

@file:OptIn(ExperimentalStdlibApi::class)

package cafe.ferret.kosekata.extensions

import cafe.ferret.kosekata.ByIdArgs
import cafe.ferret.kosekata.UserNotesArgs
import cafe.ferret.kosekata.database.collections.NoteCollection
import cafe.ferret.kosekata.i18n.Translations
import cafe.ferret.kosekata.noteEmbed
import cafe.ferret.kosekata.toId
import dev.kord.common.entity.ButtonStyle
import dev.kord.common.entity.Permission
import dev.kord.core.behavior.interaction.response.createPublicFollowup
Expand Down Expand Up @@ -81,7 +84,7 @@ class ManagementExtension : Extension() {

edit {
content = Translations.Extensions.Management.Delete.success.translate(
"%06x".format(noteId)
note._id.toId()
)

components = mutableListOf()
Expand Down Expand Up @@ -320,7 +323,7 @@ class ManagementExtension : Extension() {
noteCollection.set(note)

result.createPublicFollowup {
content = Translations.Extensions.Management.Edit.success.translate("%06x".format(noteId))
content = Translations.Extensions.Management.Edit.success.translate(note._id.toId())

noteEmbed(this@unsafeSlashCommand.kord, note, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (c) 2023 mazziechai
*/

@file:OptIn(ExperimentalStdlibApi::class)

package cafe.ferret.kosekata.extensions

import cafe.ferret.kosekata.ByIdArgs
Expand All @@ -10,6 +12,7 @@ import cafe.ferret.kosekata.database.entities.Note
import cafe.ferret.kosekata.formatTime
import cafe.ferret.kosekata.i18n.Translations
import cafe.ferret.kosekata.noteEmbed
import cafe.ferret.kosekata.toId
import dev.kord.common.Color
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.GuildBehavior
Expand Down Expand Up @@ -163,7 +166,7 @@ class ViewingExtension : Extension() {
color = Color(note._id)

footer {
text = "#%06x".format(note._id)
text = note._id.toId()
}
})
}
Expand Down Expand Up @@ -205,7 +208,7 @@ class ViewingExtension : Extension() {
if (!text) {
noteEmbed(kord, note, false)
} else {
content = "${note.content}\n\n`#%06x` `%s`".format(note._id, note.name)
content = "${note.content}\n\n`${note._id.toId()}` `${note.name}`"
}
noteReferencesComponents(note, text, guild)
}
Expand Down

0 comments on commit c9e6fa9

Please sign in to comment.