diff --git a/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt b/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt index a68718ad..799b61b3 100644 --- a/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt +++ b/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt @@ -53,6 +53,7 @@ object UrlReplacer : BaseReplacer { ::replaceSteamAppUrl, ::replaceYouTubeUrl, ::replaceYouTubePlaylistUrl, + ::replaceGoogleSearchUrl, ::replaceUrlToTitle, ::replaceUrl, ) @@ -559,6 +560,24 @@ object UrlReplacer : BaseReplacer { replacedText.replace(matchResult.value, replaceTo) } + /** + * Google検索のURLを置換します。 + */ + private suspend fun replaceGoogleSearchUrl(text: String, guildId: Snowflake) = + urlRegex.replaceAll(text) { replacedText, matchResult -> + val url = matchResult.value + + val expectedUrlStarts = "https://www.google.com/search" + if (!url.startsWith(expectedUrlStarts)) return@replaceAll replacedText + + val urlParams = Url(url).parameters + val query = urlParams["q"] ?: return@replaceAll replacedText + + val replaceTo = "Google検索「$query」へのリンク" + + replacedText.replace(matchResult.value, replaceTo) + } + /** * URLをページのタイトルに置換します。 */