-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
189 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ data/ | |
logs/ | ||
|
||
config.yml | ||
|
||
store-test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.jaoafa.vcspeaker.tts | ||
|
||
data class Token(val text: String, val replaced: Boolean = false) |
26 changes: 22 additions & 4 deletions
26
src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/AliasReplacer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
package com.jaoafa.vcspeaker.tts.replacers | ||
|
||
import com.jaoafa.vcspeaker.stores.AliasType | ||
import com.jaoafa.vcspeaker.tts.Token | ||
import dev.kord.common.entity.Snowflake | ||
|
||
/** | ||
* エイリアスを置換するクラス | ||
*/ | ||
object AliasReplacer : BaseReplacer { | ||
override val priority = ReplacerPriority.Normal | ||
override val priority = ReplacerPriority.Low | ||
|
||
override suspend fun replace(text: String, guildId: Snowflake) = | ||
replaceText(text, guildId, AliasType.Text) { alias, replacedText -> | ||
replacedText.replace(alias.search, alias.replace) | ||
override suspend fun replace(tokens: MutableList<Token>, guildId: Snowflake) = | ||
replaceText(tokens, guildId, AliasType.Text) { alias, replacedTokens -> | ||
buildList { | ||
for (replacedToken in replacedTokens) { | ||
val text = replacedToken.text | ||
|
||
if (replacedToken.replaced || !text.contains(alias.search)) { | ||
add(replacedToken) | ||
continue | ||
} | ||
|
||
val splitTexts = text.split(alias.search) | ||
|
||
val additions = splitTexts.mixin { | ||
Token(alias.replace, true) | ||
} | ||
|
||
addAll(additions) | ||
} | ||
}.toMutableList() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/ChannelMentionReplacer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package com.jaoafa.vcspeaker.tts.replacers | ||
|
||
import com.jaoafa.vcspeaker.tts.Token | ||
import dev.kord.common.entity.Snowflake | ||
|
||
/** | ||
* チャンネルメンションを置換するクラス | ||
*/ | ||
object ChannelMentionReplacer : BaseReplacer { | ||
override val priority = ReplacerPriority.High | ||
override val priority = ReplacerPriority.Normal | ||
|
||
override suspend fun replace(text: String, guildId: Snowflake) = | ||
replaceMentionable(text, Regex("<#(\\d+)>")) { kord, id -> | ||
override suspend fun replace(tokens: MutableList<Token>, guildId: Snowflake) = | ||
replaceMentionable(tokens, Regex("<#(\\d+)>")) { kord, id -> | ||
kord.getChannel(id)?.data?.name?.value ?: "不明なチャンネル" | ||
} | ||
} |
26 changes: 22 additions & 4 deletions
26
src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/EmojiReplacer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
package com.jaoafa.vcspeaker.tts.replacers | ||
|
||
import com.jaoafa.vcspeaker.stores.AliasType | ||
import com.jaoafa.vcspeaker.tts.Token | ||
import dev.kord.common.entity.Snowflake | ||
|
||
/** | ||
* 絵文字エイリアスを置換するクラス | ||
*/ | ||
object EmojiReplacer : BaseReplacer { | ||
override val priority = ReplacerPriority.High | ||
override val priority = ReplacerPriority.Normal | ||
|
||
override suspend fun replace(text: String, guildId: Snowflake) = | ||
replaceText(text, guildId, AliasType.Emoji) { alias, replacedText -> | ||
replacedText.replace(alias.search, alias.replace) | ||
override suspend fun replace(tokens: MutableList<Token>, guildId: Snowflake) = | ||
replaceText(tokens, guildId, AliasType.Emoji) { alias, replacedTokens -> | ||
buildList { | ||
for (token in replacedTokens) { | ||
val text = token.text | ||
|
||
if (token.replaced || !text.contains(alias.search)) { | ||
add(token) | ||
continue | ||
} | ||
|
||
val splitTexts = text.split(alias.search) | ||
|
||
val additions = splitTexts.mixin { | ||
Token(alias.replace, true) | ||
} | ||
|
||
addAll(additions) | ||
} | ||
}.toMutableList() | ||
} | ||
} |
32 changes: 23 additions & 9 deletions
32
src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/GuildEmojiReplacer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
package com.jaoafa.vcspeaker.tts.replacers | ||
|
||
import com.jaoafa.vcspeaker.tts.Token | ||
import dev.kord.common.entity.Snowflake | ||
|
||
/** | ||
* Guildの絵文字を置換するクラス | ||
*/ | ||
object GuildEmojiReplacer : BaseReplacer { | ||
override val priority = ReplacerPriority.High | ||
override val priority = ReplacerPriority.Normal | ||
|
||
override suspend fun replace(text: String, guildId: Snowflake): String { | ||
val matches = Regex("<a?:(\\w+):(\\d+)>").findAll(text) | ||
override suspend fun replace(tokens: MutableList<Token>, guildId: Snowflake) = buildList { | ||
val regex = Regex("<a?:(\\w+):(\\d+)>") | ||
for (token in tokens) { | ||
val text = token.text | ||
|
||
val replacedText = matches.fold(text) { replacedText, match -> | ||
val emojiName = match.groupValues[1] | ||
if (token.replaced || !text.partialMatch(regex)) { | ||
add(token) | ||
continue | ||
} | ||
|
||
replacedText.replace(match.value, emojiName) | ||
} | ||
val matches = regex.findAll(text).toList() | ||
|
||
val splitTexts = text.split(regex) | ||
|
||
val additions = splitTexts.mixin { index -> | ||
val match = matches[index] | ||
val emojiName = match.groupValues[1] | ||
|
||
return replacedText | ||
} | ||
Token(emojiName, true) | ||
} | ||
|
||
addAll(additions) | ||
} | ||
}.toMutableList() | ||
} |
28 changes: 24 additions & 4 deletions
28
src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/RegexReplacer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
package com.jaoafa.vcspeaker.tts.replacers | ||
|
||
import com.jaoafa.vcspeaker.stores.AliasType | ||
import com.jaoafa.vcspeaker.tts.Token | ||
import dev.kord.common.entity.Snowflake | ||
|
||
/** | ||
* 正規表現エイリアスを置換するクラス | ||
*/ | ||
object RegexReplacer : BaseReplacer { | ||
override val priority = ReplacerPriority.Normal | ||
override val priority = ReplacerPriority.Low | ||
|
||
override suspend fun replace(text: String, guildId: Snowflake) = | ||
replaceText(text, guildId, AliasType.Regex) { alias, replacedText -> | ||
replacedText.replace(Regex(alias.search), alias.replace) | ||
override suspend fun replace(tokens: MutableList<Token>, guildId: Snowflake) = | ||
replaceText(tokens, guildId, AliasType.Regex) { alias, replacedTokens -> | ||
buildList { | ||
val regex = Regex(alias.search) | ||
|
||
for (replacedToken in replacedTokens) { | ||
val text = replacedToken.text | ||
|
||
if (replacedToken.replaced || !text.partialMatch(regex)) { | ||
add(replacedToken) | ||
continue | ||
} | ||
|
||
val splitTexts = text.split(regex) | ||
|
||
val additions = splitTexts.mixin { | ||
Token(alias.replace, true) | ||
} | ||
|
||
addAll(additions) | ||
} | ||
}.toMutableList() | ||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/RoleMentionReplacer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package com.jaoafa.vcspeaker.tts.replacers | ||
|
||
import com.jaoafa.vcspeaker.tts.Token | ||
import dev.kord.common.entity.Snowflake | ||
|
||
/** | ||
* ロールメンションを置換するクラス | ||
*/ | ||
object RoleMentionReplacer : BaseReplacer { | ||
override val priority = ReplacerPriority.High | ||
override val priority = ReplacerPriority.Normal | ||
|
||
override suspend fun replace(text: String, guildId: Snowflake) = | ||
replaceMentionable(text, Regex("<@&(\\d+)>")) { kord, id -> | ||
override suspend fun replace(tokens: MutableList<Token>, guildId: Snowflake) = | ||
replaceMentionable(tokens, Regex("<@&(\\d+)>")) { kord, id -> | ||
kord.getGuildOrNull(guildId)?.getRole(id)?.data?.name ?: "不明なロール" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.