From adb83e47d1140c25947f724007b4a64578b33410 Mon Sep 17 00:00:00 2001 From: yuuaHP Date: Sun, 5 Nov 2023 23:36:00 +0900 Subject: [PATCH] feat: Ignore Type (#49) --- .../jaoafa/vcspeaker/commands/AliasCommand.kt | 13 +++---- .../vcspeaker/commands/IgnoreCommand.kt | 37 ++++++++++++++----- .../jaoafa/vcspeaker/stores/IgnoreStore.kt | 10 +++++ .../vcspeaker/voicetext/TextProcessor.kt | 6 ++- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/com/jaoafa/vcspeaker/commands/AliasCommand.kt b/src/main/kotlin/com/jaoafa/vcspeaker/commands/AliasCommand.kt index a511e0a0..6bba4b8b 100644 --- a/src/main/kotlin/com/jaoafa/vcspeaker/commands/AliasCommand.kt +++ b/src/main/kotlin/com/jaoafa/vcspeaker/commands/AliasCommand.kt @@ -7,11 +7,11 @@ import com.jaoafa.vcspeaker.stores.AliasStore import com.jaoafa.vcspeaker.stores.AliasType import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.authorOf import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.errorColor -import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand -import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSubCommand import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respondEmbed import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.successColor import com.jaoafa.vcspeaker.tools.discord.Options +import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand +import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSubCommand import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.optionalStringChoice import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString @@ -196,12 +196,11 @@ class AliasCommand : Extension() { page { authorOf(user) - for (alias in chunkedAliases) { - val (_, _, type, from, to) = alias + title = ":information_source: Aliases" - field("${type.emoji} ${type.displayName}", false) { - "${if (type == AliasType.Regex) "`$from`" else from} → $to" - } + description = chunkedAliases.joinToString("\n") { (_, userId, type, from, to) -> + val fromDisplay = if (type == AliasType.Regex) "`$from`" else from + "${type.emoji} ${type.displayName} | 「$fromDisplay → $to」 | <@${userId}>" } successColor() diff --git a/src/main/kotlin/com/jaoafa/vcspeaker/commands/IgnoreCommand.kt b/src/main/kotlin/com/jaoafa/vcspeaker/commands/IgnoreCommand.kt index ea134eaf..3d7b2d4b 100644 --- a/src/main/kotlin/com/jaoafa/vcspeaker/commands/IgnoreCommand.kt +++ b/src/main/kotlin/com/jaoafa/vcspeaker/commands/IgnoreCommand.kt @@ -2,13 +2,15 @@ package com.jaoafa.vcspeaker.commands import com.jaoafa.vcspeaker.stores.IgnoreData import com.jaoafa.vcspeaker.stores.IgnoreStore +import com.jaoafa.vcspeaker.stores.IgnoreType import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.authorOf import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.errorColor -import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand -import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSubCommand import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respondEmbed import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.successColor import com.jaoafa.vcspeaker.tools.discord.Options +import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand +import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSubCommand +import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice import com.kotlindiscord.kord.extensions.commands.converters.impl.string import com.kotlindiscord.kord.extensions.extensions.Extension import com.kotlindiscord.kord.extensions.types.respondingPaginator @@ -19,6 +21,13 @@ class IgnoreCommand : Extension() { override val name = this::class.simpleName!! inner class CreateOptions : Options() { + val type by stringChoice { + name = "type" + description = "無視判定の種類" + for (ignoreType in IgnoreType.entries) + choice(ignoreType.displayName, ignoreType.name) + } + val text by string { name = "text" description = "無視する文字列" @@ -45,15 +54,18 @@ class IgnoreCommand : Extension() { publicSlashCommand("ignore", "無視機能を設定します。") { publicSubCommand("create", "無視する文字列を作成します。", ::CreateOptions) { action { + val type = IgnoreType.valueOf(arguments.type) val text = arguments.text val duplicateExists = IgnoreStore.find(guild!!.id, text) != null if (!duplicateExists) - IgnoreStore.create(IgnoreData(guild!!.id, user.id, text)) + IgnoreStore.create(IgnoreData(guild!!.id, user.id, type, text)) + + val typeText = if (type == IgnoreType.Contains) "を含む" else "と一致する" respondEmbed( ":face_with_symbols_over_mouth: Ignore Created", - "今後「$text」を含むメッセージは読み上げられません。" + "今後「$text」${typeText}メッセージは読み上げられません。" ) { authorOf(user) successColor() @@ -69,9 +81,11 @@ class IgnoreCommand : Extension() { if (target != null) { IgnoreStore.remove(target) + val typeText = if (target.type == IgnoreType.Contains) "が含まれて" else "と一致して" + respondEmbed( ":wastebasket: Ignore Deleted", - "「$text」が含まれていても読み上げます。" + "「$text」${typeText}いても読み上げます。" ) { authorOf(user) successColor() @@ -79,7 +93,10 @@ class IgnoreCommand : Extension() { } else { respondEmbed( ":question: Ignore Not Found", - "「$text」を含むメッセージは無視されていません。" + """ + 「$text」に一致する設定が見つかりませんでした。 + `/ignore list` で一覧を確認できます。 + """.trimIndent() ) } } @@ -92,7 +109,7 @@ class IgnoreCommand : Extension() { if (ignores.isEmpty()) { respondEmbed( ":grey_question: Ignores Not Found", - "無視機能が設定されていないようです。`/ignore create` で作成してみましょう!" + "設定されていないようです。`/ignore create` で作成してみましょう!" ) { authorOf(user) errorColor() @@ -105,8 +122,10 @@ class IgnoreCommand : Extension() { page { authorOf(user) - description = chunkedIgnores.joinToString("\n") { - "「${it.text}」<@${it.userId}>" + title = ":information_source: Ignores" + + description = chunkedIgnores.joinToString("\n") { (_, userId, type, text) -> + "${type.emoji} ${type.displayName} | 「$text」 | <@${userId}>" } successColor() diff --git a/src/main/kotlin/com/jaoafa/vcspeaker/stores/IgnoreStore.kt b/src/main/kotlin/com/jaoafa/vcspeaker/stores/IgnoreStore.kt index f1aa8bf9..fc298e5a 100644 --- a/src/main/kotlin/com/jaoafa/vcspeaker/stores/IgnoreStore.kt +++ b/src/main/kotlin/com/jaoafa/vcspeaker/stores/IgnoreStore.kt @@ -6,10 +6,20 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +@Serializable +enum class IgnoreType( + val displayName: String, + val emoji: String +) { + Exact("完全一致", ":asterisk:"), + Contains("部分一致", ":record_button:") +} + @Serializable data class IgnoreData( val guildId: Snowflake, val userId: Snowflake, + val type: IgnoreType, val text: String ) diff --git a/src/main/kotlin/com/jaoafa/vcspeaker/voicetext/TextProcessor.kt b/src/main/kotlin/com/jaoafa/vcspeaker/voicetext/TextProcessor.kt index 85d39fa1..ae4eabf8 100644 --- a/src/main/kotlin/com/jaoafa/vcspeaker/voicetext/TextProcessor.kt +++ b/src/main/kotlin/com/jaoafa/vcspeaker/voicetext/TextProcessor.kt @@ -1,6 +1,7 @@ package com.jaoafa.vcspeaker.voicetext import com.jaoafa.vcspeaker.stores.IgnoreStore +import com.jaoafa.vcspeaker.stores.IgnoreType import com.jaoafa.vcspeaker.tools.Emoji.replaceEmojiToName import com.jaoafa.vcspeaker.voicetext.api.Emotion import com.jaoafa.vcspeaker.voicetext.api.Speaker @@ -57,6 +58,9 @@ object TextProcessor { private fun shouldIgnore(text: String, guildId: Snowflake) = IgnoreStore.filter(guildId).any { - text.contains(it.text) + when (it.type) { + IgnoreType.Exact -> text == it.text + IgnoreType.Contains -> text.contains(it.text) + } } } \ No newline at end of file