Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Fortnite related commands to Slash Commands #2474

Open
wants to merge 1 commit into
base: cinnamon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions discord/commands/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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!" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<String>) {
// 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<ChangeFortniteBattleRoyaleNewsPageData>(data)

val r = neoTiltedClient.getFortniteBattleRoyaleNewsByHash(hash)

val selectedPage = values.first().toInt()

context.updateMessage(FortniteNewsExecutor.createMessage(user, r, selectedPage))
}
}
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion microservices/broker-tickers-updater/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object BrokerTickersUpdaterLauncher {
@JvmStatic
fun main(args: Array<String>) {
val rootConfig = ConfigUtils.loadAndParseConfigOrCopyFromJarAndExit<RootConfig>(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
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ include(":discord:web-server")
include(":discord:gateway")

// ===[ MICROSERVICES ]===
include("microservices:broker-tickers-updater")
include("microservices:broker-tickers-updater")
include("microservices:fortnite-api-server")