Skip to content

Commit

Permalink
feat: Guild Emoji Replacer 🔢
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuahp committed Oct 1, 2023
1 parent a2bdaca commit 6ec9333
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/main/kotlin/com/jaoafa/vcspeaker/voicetext/Preprocessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ object Preprocessor {
::replaceChannelMention,
::replaceRoleMention,
::replaceUserMention,
::replaceMessageMention
::replaceMessageMention,
::replaceGuildEmoji
).replaceEmojiToName()

return replacedText.let { if (it.length > 180) it.substring(0, 180) else it }
Expand All @@ -56,8 +57,8 @@ object Preprocessor {
speed = parameterMap["speed"]?.toIntOrNull()
)

val newText = parameters.fold(text) { partText, parameterText ->
partText.replace(parameterText, "")
val newText = parameters.fold(text) { replacedText, parameterText ->
replacedText.replace(parameterText, "")
}.trim()

return newText to newVoice
Expand Down Expand Up @@ -114,14 +115,31 @@ object Preprocessor {
displayName ?: "不明なユーザー"
}

private suspend fun replaceMentionable(
text: String,
regex: Regex,
nameSupplier: suspend (Kord, Snowflake) -> String
): String {
val matches = regex.findAll(text)

val replacedText = matches.fold(text) { replacedText, match ->
val id = Snowflake(match.groupValues[1]) // 0 is for whole match
val name = nameSupplier(VCSpeaker.kord, id)

replacedText.replace(match.value, name)
}

return replacedText
}

private suspend fun replaceMessageMention(text: String, guildId: Snowflake): String {
val matches = Regex("https://(\\w+\\.)*discord.com/channels/(\\d+)/(\\d+)/(\\d+)").findAll(text)

val replacedText = matches.fold(text) { replacedText, match ->
val channelId = Snowflake(match.groupValues[3])
val messageId = Snowflake(match.groupValues[4])

val channel = VCSpeaker.instance.kordRef.getChannelOf<TextChannel>(channelId)
val channel = VCSpeaker.kord.getChannelOf<TextChannel>(channelId)
val message = channel?.getMessageOrNull(messageId) ?: return@fold replacedText

val read = "${message.author?.username ?: "システム"}${channel.name} で送信したメッセージへのリンク"
Expand All @@ -132,18 +150,13 @@ object Preprocessor {
return replacedText
}

private suspend fun replaceMentionable(
text: String,
regex: Regex,
nameSupplier: suspend (Kord, Snowflake) -> String
): String {
val matches = regex.findAll(text)
private fun replaceGuildEmoji(text: String, guildId: Snowflake): String {
val matches = Regex("<a?:(\\w+):(\\d+)>").findAll(text)

val replacedText = matches.fold(text) { replacedText, match ->
val id = Snowflake(match.groupValues[1]) // 0 is for whole match
val name = nameSupplier(VCSpeaker.instance.kordRef, id)
val emojiName = match.groupValues[1]

replacedText.replace(match.value, name)
replacedText.replace(match.value, emojiName)
}

return replacedText
Expand Down

0 comments on commit 6ec9333

Please sign in to comment.