From 60f4e17c3797b25d1b09d9a6503c225ed7aca912 Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Sun, 19 Dec 2021 14:04:30 -0300 Subject: [PATCH] Add "/fortnite news" --- buildSrc/src/main/kotlin/Versions.kt | 3 +- common/build.gradle.kts | 2 +- .../loritta/cinnamon/common/emotes/Emotes.kt | 1 + discord/commands/build.gradle.kts | 2 + .../platform/commands/CommandManager.kt | 16 +++++ .../platform/commands/ComponentExecutorIds.kt | 1 + .../ChangeFortniteBattleRoyaleNewsPageData.kt | 11 +++ ...ngeFortniteBattleRoyaleNewsPageExecutor.kt | 28 ++++++++ .../commands/fortnite/FortniteNewsExecutor.kt | 71 +++++++++++++++++++ .../fortnite/declarations/FortniteCommand.kt | 14 ++++ .../platform/commands/CommandRegistry.kt | 4 +- .../broker-tickers-updater/build.gradle.kts | 2 +- .../BrokerTickersUpdaterLauncher.kt | 2 +- settings.gradle.kts | 3 +- 14 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageData.kt create mode 100644 discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageExecutor.kt create mode 100644 discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/FortniteNewsExecutor.kt create mode 100644 discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/declarations/FortniteCommand.kt diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index a7c562e016..9bc751014a 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -7,7 +7,8 @@ object Versions { const val KOTLINX_SERIALIZATION = "1.2.2" const val EXPOSED = "0.36.1" const val PROMETHEUS = "0.12.0" - const val KTOR = "1.6.3" + // 1.6.6+ requires Kotlin 1.6.0 + const val KTOR = "1.6.5" const val DISCORD_INTERAKTIONS = "0.0.10-SNAPSHOT" const val I18N_HELPER = "0.0.2-SNAPSHOT" } \ No newline at end of file diff --git a/common/build.gradle.kts b/common/build.gradle.kts index bca39e779a..1bdf3c160c 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -95,7 +95,7 @@ kotlin { api("org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.KOTLINX_SERIALIZATION}") api("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.KOTLINX_SERIALIZATION}") api("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:${Versions.KOTLINX_SERIALIZATION}") - api("io.ktor:ktor-client-core:1.6.0") + api("io.ktor:ktor-client-core:${Versions.KTOR}") api("net.perfectdreams.i18nhelper:core:${Versions.I18N_HELPER}") api("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1") diff --git a/common/src/commonMain/kotlin/net/perfectdreams/loritta/cinnamon/common/emotes/Emotes.kt b/common/src/commonMain/kotlin/net/perfectdreams/loritta/cinnamon/common/emotes/Emotes.kt index fb380cf789..79f9c3633a 100644 --- a/common/src/commonMain/kotlin/net/perfectdreams/loritta/cinnamon/common/emotes/Emotes.kt +++ b/common/src/commonMain/kotlin/net/perfectdreams/loritta/cinnamon/common/emotes/Emotes.kt @@ -34,6 +34,7 @@ object Emotes { val CoinHeads = DiscordEmote(412586256409559041, "cara", false) val CoinTails = DiscordEmote(412586257114464259, "coroa", false) + val DefaultDance = DiscordEmote(607213397313847298L, "default_dance", true) val Tada = UnicodeEmote("\uD83C\uDF89") val WhiteFlag = UnicodeEmote("\uD83C\uDFF3️") val BlackFlag = UnicodeEmote("\uD83C\uDFF4") diff --git a/discord/commands/build.gradle.kts b/discord/commands/build.gradle.kts index 19906caa3d..e4311e7238 100644 --- a/discord/commands/build.gradle.kts +++ b/discord/commands/build.gradle.kts @@ -24,6 +24,8 @@ kotlin { // Kord REST api("dev.kord:kord-rest:0.8.x-SNAPSHOT") + + implementation("net.perfectdreams.neotilted:client:0.0.1-SNAPSHOT") } } } diff --git a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandManager.kt b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandManager.kt index 46450d3333..7d5bc17ca7 100644 --- a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandManager.kt +++ b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandManager.kt @@ -63,6 +63,9 @@ import net.perfectdreams.loritta.cinnamon.platform.commands.economy.BrokerStockI import net.perfectdreams.loritta.cinnamon.platform.commands.economy.SonhosExecutor import net.perfectdreams.loritta.cinnamon.platform.commands.economy.declarations.BrokerCommand import net.perfectdreams.loritta.cinnamon.platform.commands.economy.declarations.SonhosCommand +import net.perfectdreams.loritta.cinnamon.platform.commands.fortnite.ChangeFortniteBattleRoyaleNewsPageExecutor +import net.perfectdreams.loritta.cinnamon.platform.commands.fortnite.FortniteNewsExecutor +import net.perfectdreams.loritta.cinnamon.platform.commands.fortnite.declarations.FortniteCommand import net.perfectdreams.loritta.cinnamon.platform.commands.images.ArtExecutor import net.perfectdreams.loritta.cinnamon.platform.commands.images.BobBurningPaperExecutor import net.perfectdreams.loritta.cinnamon.platform.commands.images.BolsoDrakeExecutor @@ -170,6 +173,7 @@ import net.perfectdreams.loritta.cinnamon.platform.commands.videos.declarations. import net.perfectdreams.loritta.cinnamon.platform.commands.videos.declarations.CarlyAaahCommand import net.perfectdreams.loritta.cinnamon.platform.commands.videos.declarations.FansExplainingCommand import net.perfectdreams.minecraftmojangapi.MinecraftMojangAPI +import net.perfectdreams.neotilted.client.NeoTiltedClient import kotlin.system.exitProcess class CommandManager( @@ -185,6 +189,7 @@ class CommandManager( private val rest = loritta.rest private val gabrielaImageServerClient = net.perfectdreams.gabrielaimageserver.client.GabrielaImageServerClient(loritta.servicesConfig.gabrielaImageServer.url, loritta.http) + private val neoTiltedClient = NeoTiltedClient("http://127.0.0.1:8000", "a", loritta.http) private val random = loritta.random private val http = loritta.http @@ -428,6 +433,17 @@ class CommandManager( ChangeColorPortraitTypeButtonClickExecutor(loritta, gabrielaImageServerClient) ) + // ===[ FORTNITE ]=== + commandManager.register( + FortniteCommand, + FortniteNewsExecutor(neoTiltedClient) + ) + + commandManager.register( + ChangeFortniteBattleRoyaleNewsPageExecutor, + ChangeFortniteBattleRoyaleNewsPageExecutor(neoTiltedClient) + ) + // Validate if we don't have more commands than Discord allows if (commandManager.declarations.size > 100) { logger.error { "Currently there are ${commandManager.declarations.size} root commands registered, however Discord has a 100 root command limit! You need to remove some of the commands!" } diff --git a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/ComponentExecutorIds.kt b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/ComponentExecutorIds.kt index b292e4dc1c..5f55393ba8 100644 --- a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/ComponentExecutorIds.kt +++ b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/ComponentExecutorIds.kt @@ -16,6 +16,7 @@ object ComponentExecutorIds { val CHANGE_UNIVERSE_SELECT_MENU_EXECUTOR = register("0004") val CONFIRM_DIALOG_BOX_BUTTON_EXECUTOR = register("0005") val CHANGE_COLOR_PORTRAIT_TYPE_BUTTON_EXECUTOR = register("0006") + val CHANGE_FORTNITE_BR_NEWS_PAGE_EXECUTOR = register("0007") /** * Verifies if the [id] matches our constraints diff --git a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageData.kt b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageData.kt new file mode 100644 index 0000000000..4617f5fcd9 --- /dev/null +++ b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageData.kt @@ -0,0 +1,11 @@ +package net.perfectdreams.loritta.cinnamon.platform.commands.fortnite + +import dev.kord.common.entity.Snowflake +import kotlinx.serialization.Serializable +import net.perfectdreams.loritta.cinnamon.platform.components.data.SingleUserComponentData + +@Serializable +data class ChangeFortniteBattleRoyaleNewsPageData( + override val userId: Snowflake, + val hash: String +) : SingleUserComponentData \ No newline at end of file diff --git a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageExecutor.kt b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageExecutor.kt new file mode 100644 index 0000000000..a351d9c3ac --- /dev/null +++ b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/ChangeFortniteBattleRoyaleNewsPageExecutor.kt @@ -0,0 +1,28 @@ +package net.perfectdreams.loritta.cinnamon.platform.commands.fortnite + +import net.perfectdreams.discordinteraktions.api.entities.User +import net.perfectdreams.loritta.cinnamon.platform.commands.ComponentExecutorIds +import net.perfectdreams.loritta.cinnamon.platform.components.ComponentContext +import net.perfectdreams.loritta.cinnamon.platform.components.selects.SelectMenuExecutorDeclaration +import net.perfectdreams.loritta.cinnamon.platform.components.selects.SelectMenuWithDataExecutor +import net.perfectdreams.neotilted.client.NeoTiltedClient + +class ChangeFortniteBattleRoyaleNewsPageExecutor( + val neoTiltedClient: NeoTiltedClient +) : SelectMenuWithDataExecutor { + companion object : SelectMenuExecutorDeclaration(ChangeFortniteBattleRoyaleNewsPageExecutor::class, ComponentExecutorIds.CHANGE_FORTNITE_BR_NEWS_PAGE_EXECUTOR) + + override suspend fun onSelect(user: User, context: ComponentContext, data: String, values: List) { + // We will already defer to avoid issues + // Also because we want to edit the message with a file... later! + context.deferUpdateMessage() + + val (_, hash) = context.decodeViaComponentDataUtilsAndRequireUserToMatch(data) + + val r = neoTiltedClient.getFortniteBattleRoyaleNewsByHash(hash) + + val selectedPage = values.first().toInt() + + context.updateMessage(FortniteNewsExecutor.createMessage(user, r, selectedPage)) + } +} \ No newline at end of file diff --git a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/FortniteNewsExecutor.kt b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/FortniteNewsExecutor.kt new file mode 100644 index 0000000000..0622817225 --- /dev/null +++ b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/FortniteNewsExecutor.kt @@ -0,0 +1,71 @@ +package net.perfectdreams.loritta.cinnamon.platform.commands.fortnite + +import dev.kord.common.Color +import net.perfectdreams.discordinteraktions.api.entities.User +import net.perfectdreams.discordinteraktions.common.builder.message.MessageBuilder +import net.perfectdreams.discordinteraktions.common.builder.message.actionRow +import net.perfectdreams.discordinteraktions.common.builder.message.embed +import net.perfectdreams.loritta.cinnamon.common.emotes.Emotes +import net.perfectdreams.loritta.cinnamon.platform.commands.ApplicationCommandContext +import net.perfectdreams.loritta.cinnamon.platform.commands.CommandArguments +import net.perfectdreams.loritta.cinnamon.platform.commands.CommandExecutor +import net.perfectdreams.loritta.cinnamon.platform.commands.declarations.CommandExecutorDeclaration +import net.perfectdreams.loritta.cinnamon.platform.components.loriEmoji +import net.perfectdreams.loritta.cinnamon.platform.components.selectMenu +import net.perfectdreams.loritta.cinnamon.platform.utils.ComponentDataUtils +import net.perfectdreams.neotilted.client.NeoTiltedClient +import net.perfectdreams.neotilted.data.FortniteBattleRoyaleNewsResponse + +class FortniteNewsExecutor(val neoTiltedClient: NeoTiltedClient) : CommandExecutor() { + companion object : CommandExecutorDeclaration(FortniteNewsExecutor::class) { + fun createMessage( + user: User, + newsData: FortniteBattleRoyaleNewsResponse.NewsData, + currentPage: Int + ) : MessageBuilder.() -> (Unit) { + return { + val currentMotd = newsData.motds[currentPage] + + embed { + title = "${Emotes.DefaultDance} ${currentMotd.title}" + description = currentMotd.body + image = currentMotd.image + color = Color(0, 125, 187) + } + + val encodedComponent = ComponentDataUtils.encode( + ChangeFortniteBattleRoyaleNewsPageData( + user.id, + newsData.hash + ) + ) + + actionRow { + selectMenu( + ChangeFortniteBattleRoyaleNewsPageExecutor, + encodedComponent + ) { + for ((pageId, motd) in newsData.motds.withIndex()) { + this.option(motd.title, pageId.toString()) { + loriEmoji = Emotes.DefaultDance + + if (motd == currentMotd) { + default = true + } + } + } + } + } + } + } + } + + override suspend fun execute(context: ApplicationCommandContext, args: CommandArguments) { + context.deferChannelMessage() + + // TODO: language + val r = neoTiltedClient.getFortniteBattleRoyaleNews("pt-BR") + + context.sendMessage(createMessage(context.user, r, 0)) + } +} \ No newline at end of file diff --git a/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/declarations/FortniteCommand.kt b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/declarations/FortniteCommand.kt new file mode 100644 index 0000000000..8535ca4b78 --- /dev/null +++ b/discord/commands/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/fortnite/declarations/FortniteCommand.kt @@ -0,0 +1,14 @@ +package net.perfectdreams.loritta.cinnamon.platform.commands.fortnite.declarations + +import net.perfectdreams.loritta.cinnamon.common.utils.TodoFixThisData +import net.perfectdreams.loritta.cinnamon.platform.commands.CommandCategory +import net.perfectdreams.loritta.cinnamon.platform.commands.declarations.CommandDeclaration +import net.perfectdreams.loritta.cinnamon.platform.commands.fortnite.FortniteNewsExecutor + +object FortniteCommand : CommandDeclaration { + override fun declaration() = command(listOf("fortnite"), CommandCategory.FORTNITE, TodoFixThisData) { + subcommand(listOf("news"), TodoFixThisData) { + executor = FortniteNewsExecutor + } + } +} \ No newline at end of file diff --git a/discord/discord-common/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandRegistry.kt b/discord/discord-common/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandRegistry.kt index 7a8b5cb4e2..4151b3c6fd 100644 --- a/discord/discord-common/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandRegistry.kt +++ b/discord/discord-common/src/jvmMain/kotlin/net/perfectdreams/loritta/cinnamon/platform/commands/CommandRegistry.kt @@ -272,7 +272,7 @@ class CommandRegistry( val emoji = when (declaration.category) { CommandCategory.FUN -> "\uD83D\uDE02" CommandCategory.IMAGES -> "\uD83D\uDDBC️" - CommandCategory.MINECRAFT -> "⛏️" + CommandCategory.MINECRAFT -> "\uD83D\uDC8E" CommandCategory.POKEMON -> TODO() CommandCategory.UNDERTALE -> "❤️" CommandCategory.ROBLOX -> TODO() @@ -285,7 +285,7 @@ class CommandRegistry( CommandCategory.ACTION -> TODO() CommandCategory.ECONOMY -> "\uD83D\uDCB8" CommandCategory.VIDEOS -> "\uD83C\uDFAC" - CommandCategory.FORTNITE -> TODO() + CommandCategory.FORTNITE -> "⛏️" CommandCategory.MAGIC -> TODO() } append(emoji) diff --git a/microservices/broker-tickers-updater/build.gradle.kts b/microservices/broker-tickers-updater/build.gradle.kts index 889cea806e..2e496ae513 100644 --- a/microservices/broker-tickers-updater/build.gradle.kts +++ b/microservices/broker-tickers-updater/build.gradle.kts @@ -15,7 +15,7 @@ dependencies { implementation("org.jetbrains.exposed:exposed-jdbc:${Versions.EXPOSED}") implementation("org.jetbrains.exposed:exposed-java-time:${Versions.EXPOSED}") implementation("pw.forst", "exposed-upsert", "1.1.0") - implementation("io.ktor:ktor-client-cio:1.6.0") + implementation("io.ktor:ktor-client-cio:${Versions.KTOR}") api("org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.KOTLINX_SERIALIZATION}") api("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.KOTLINX_SERIALIZATION}") diff --git a/microservices/broker-tickers-updater/src/main/kotlin/net/perfectdreams/loritta/cinnamon/microservices/brokertickersupdater/BrokerTickersUpdaterLauncher.kt b/microservices/broker-tickers-updater/src/main/kotlin/net/perfectdreams/loritta/cinnamon/microservices/brokertickersupdater/BrokerTickersUpdaterLauncher.kt index 8d11ed57a6..205c35413d 100644 --- a/microservices/broker-tickers-updater/src/main/kotlin/net/perfectdreams/loritta/cinnamon/microservices/brokertickersupdater/BrokerTickersUpdaterLauncher.kt +++ b/microservices/broker-tickers-updater/src/main/kotlin/net/perfectdreams/loritta/cinnamon/microservices/brokertickersupdater/BrokerTickersUpdaterLauncher.kt @@ -13,7 +13,7 @@ object BrokerTickersUpdaterLauncher { @JvmStatic fun main(args: Array) { val rootConfig = ConfigUtils.loadAndParseConfigOrCopyFromJarAndExit(BrokerTickersUpdaterLauncher::class, System.getProperty("brokertickersupdater.config", "broker-tickers-updater.conf")) - logger.info { "Loaded Loritta's configuration file" } + logger.info { "Loaded Broker Tickers Updater's configuration file" } val http = HttpClient { expectSuccess = false diff --git a/settings.gradle.kts b/settings.gradle.kts index 7a1bfc98fa..0dfc387d37 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -24,4 +24,5 @@ include(":discord:web-server") include(":discord:gateway") // ===[ MICROSERVICES ]=== -include("microservices:broker-tickers-updater") \ No newline at end of file +include("microservices:broker-tickers-updater") +include("microservices:fortnite-api-server") \ No newline at end of file