From 0ce15945f46718cea5e3e52904c0c0d02286a785 Mon Sep 17 00:00:00 2001 From: noobnuby Date: Mon, 21 Oct 2024 00:01:13 +0900 Subject: [PATCH] chlwjrghk rnlcksgdk --- .gitignore | 1 + steam/build.gradle.kts | 21 +++--- .../main/kotlin/com/noobnuby/plugin/Main.kt | 3 +- .../com/noobnuby/plugin/command/Steam.kt | 21 +++++- .../com/noobnuby/plugin/data/ButtonState.kt | 3 + .../com/noobnuby/plugin/data/SteamSaleData.kt | 3 +- .../noobnuby/plugin/handler/ButtonClick.kt | 74 +++++++++++++++++++ .../plugin/service/SteamApiService.kt | 8 +- 8 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 steam/src/main/kotlin/com/noobnuby/plugin/data/ButtonState.kt create mode 100644 steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt diff --git a/.gitignore b/.gitignore index b1dff0d..a4f5738 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ build/ !**/src/test/**/build/ ### IntelliJ IDEA ### +.idea .idea/modules.xml .idea/jarRepositories.xml .idea/compiler.xml diff --git a/steam/build.gradle.kts b/steam/build.gradle.kts index 9d7157b..db7f6ef 100644 --- a/steam/build.gradle.kts +++ b/steam/build.gradle.kts @@ -9,17 +9,16 @@ repositories { mavenCentral() } -//tasks { -// shadowJar { -// archiveBaseName.set(rootProject.name) -// archiveClassifier.set("") -// archiveVersion.set("") -// -// manifest { -// attributes["Main-Class"] = "com.noobnuby.plugin.MainKt" -// } -// } -//} +tasks { + shadowJar { + doLast { + copy { + from(archiveFile) + into(File("C:\\Users\\user\\Desktop\\px32\\plugins")) + } + } + } +} kotlin { jvmToolchain(21) diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/Main.kt b/steam/src/main/kotlin/com/noobnuby/plugin/Main.kt index 02cbecc..28932b3 100644 --- a/steam/src/main/kotlin/com/noobnuby/plugin/Main.kt +++ b/steam/src/main/kotlin/com/noobnuby/plugin/Main.kt @@ -1,6 +1,7 @@ package com.noobnuby.plugin import com.noobnuby.plugin.command.Steam +import com.noobnuby.plugin.handler.ButtonClick import net.projecttl.p.x32.api.Plugin import net.projecttl.p.x32.api.command.commandHandler @@ -9,7 +10,7 @@ class Main : Plugin() { logger.info("Enable Steam plugin!") -// addHandler(Ready) + addHandler(ButtonClick) addHandler(commandHandler { handler -> handler.addCommand(Steam) }) diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/command/Steam.kt b/steam/src/main/kotlin/com/noobnuby/plugin/command/Steam.kt index 32be01d..6732d27 100644 --- a/steam/src/main/kotlin/com/noobnuby/plugin/command/Steam.kt +++ b/steam/src/main/kotlin/com/noobnuby/plugin/command/Steam.kt @@ -1,14 +1,19 @@ package com.noobnuby.plugin.command +import com.noobnuby.plugin.data.ButtonState import com.noobnuby.plugin.data.SteamSaleData +import com.noobnuby.plugin.handler.ButtonClick import com.noobnuby.plugin.service.SteamApiService import com.noobnuby.plugin.util.formatWon import kotlinx.coroutines.runBlocking import net.dv8tion.jda.api.EmbedBuilder import net.dv8tion.jda.api.entities.MessageEmbed +import net.dv8tion.jda.api.entities.User import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent +import net.dv8tion.jda.api.interactions.components.buttons.Button import net.projecttl.p.x32.api.command.GlobalCommand import net.projecttl.p.x32.api.command.useCommand +import net.projecttl.p.x32.api.util.footer object Steam : GlobalCommand { override val data = useCommand { @@ -35,18 +40,26 @@ object Steam : GlobalCommand { SteamApiService.getHotSales().list } + when (ev.subcommandName) { "할인목록" -> { - ev.replyEmbeds(steamSaleEmbed(sale, false)).queue() + ev.replyEmbeds(steamSaleEmbed(sale, false, ev.user)).addActionRow( + Button.secondary("previousSteamSale", "◀").asDisabled(), + Button.secondary("nextSteamSale", "▶") + ).queue() + } "인기할인" -> { - ev.replyEmbeds(steamSaleEmbed(hotSale, true)).queue() + ev.replyEmbeds(steamSaleEmbed(hotSale, true, ev.user)).addActionRow( + Button.secondary("previousSteamHotSale", "◀").asDisabled(), + Button.secondary("nextSteamHotSale", "▶") + ).queue() } } } - private fun steamSaleEmbed(list: List, hot: Boolean): MessageEmbed { + fun steamSaleEmbed(list: List, hot: Boolean,user: User): MessageEmbed { val embed = EmbedBuilder() if (hot) embed.setTitle("스팀 인기 할인목록") else embed.setTitle("스팀 할인목록") @@ -54,10 +67,12 @@ object Steam : GlobalCommand { list.forEach { val saleInfo = """ ~~₩${it.full_price_va.formatWon()}~~ → ₩${it.sale_price_va.formatWon()} + [link](${it.store_lk}) """.trimIndent() embed.addField("${it.title_nm} (${(it.discount_rt * 100).toInt()}%)", saleInfo, false) } + embed.footer(user) return embed.build() } diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/data/ButtonState.kt b/steam/src/main/kotlin/com/noobnuby/plugin/data/ButtonState.kt new file mode 100644 index 0000000..77e937f --- /dev/null +++ b/steam/src/main/kotlin/com/noobnuby/plugin/data/ButtonState.kt @@ -0,0 +1,3 @@ +package com.noobnuby.plugin.data + +data class ButtonState(val page: Int,val hot: Boolean) \ No newline at end of file diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/data/SteamSaleData.kt b/steam/src/main/kotlin/com/noobnuby/plugin/data/SteamSaleData.kt index 173a4e9..3bc428f 100644 --- a/steam/src/main/kotlin/com/noobnuby/plugin/data/SteamSaleData.kt +++ b/steam/src/main/kotlin/com/noobnuby/plugin/data/SteamSaleData.kt @@ -15,5 +15,6 @@ data class SteamSaleData( val start_dt: String, val end_dt: String, val total_cn: Int, - val list: List + val list: List, + val more_fl: Boolean ) \ No newline at end of file diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt b/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt new file mode 100644 index 0000000..410f551 --- /dev/null +++ b/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt @@ -0,0 +1,74 @@ +package com.noobnuby.plugin.handler + +import com.noobnuby.plugin.command.Steam +import com.noobnuby.plugin.data.ButtonState +import com.noobnuby.plugin.service.SteamApiService +import kotlinx.coroutines.runBlocking +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent +import net.dv8tion.jda.api.hooks.ListenerAdapter +import net.dv8tion.jda.api.interactions.components.buttons.Button + +object ButtonClick : ListenerAdapter() { + private val buttonState = mutableMapOf() + + override fun onButtonInteraction(ev: ButtonInteractionEvent) { + val buttonId = ev.componentId + val currentState = buttonState.getOrDefault(ev.message.id, ButtonState(1, false)) + + if (buttonId == "nextSteamSale" || buttonId == "nextSteamHotSale") { + val isHotSale = buttonId == "nextSteamHotSale" + + buttonState[ev.message.id] = currentState.copy(page = currentState.page + 1) + + println("next " + buttonState[ev.message.id]) + + val sale = runBlocking { + if (isHotSale) { + SteamApiService.getHotSales(buttonState[ev.message.id]!!.page).list + } else { + SteamApiService.getSales(buttonState[ev.message.id]!!.page).list + } + } + val moreFl = runBlocking { + if (isHotSale) { + !SteamApiService.getHotSales(buttonState[ev.message.id]!!.page).more_fl + } else { + !SteamApiService.getSales(buttonState[ev.message.id]!!.page).more_fl + } + } + + ev.editMessageEmbeds(Steam.steamSaleEmbed(sale, isHotSale, ev.user)).setActionRow( + Button.secondary(if(isHotSale) "previousSteamHotSale" else "previousSteamSale", "◀"), + Button.secondary("nextSteamSale", "▶").withDisabled(moreFl) + ).queue() + } + + if (buttonId == "previousSteamSale" || buttonId == "previousSteamHotSale") { + val isHotSale = buttonId == "previousSteamHotSale" + + buttonState[ev.message.id] = currentState.copy(page = currentState.page - 1) + + println("previous " + buttonState[ev.message.id]) + + val sale = runBlocking { + if (isHotSale) { + SteamApiService.getHotSales(buttonState[ev.message.id]!!.page).list + } else { + SteamApiService.getSales(buttonState[ev.message.id]!!.page).list + } + } + val moreFl = runBlocking { + if (isHotSale) { + !SteamApiService.getHotSales(buttonState[ev.message.id]!!.page).more_fl + } else { + !SteamApiService.getSales(buttonState[ev.message.id]!!.page).more_fl + } + } + + ev.editMessageEmbeds(Steam.steamSaleEmbed(sale, isHotSale, ev.user)).setActionRow( + Button.secondary(buttonId, "◀").withDisabled(buttonState[ev.message.id]!!.page <= 1), + Button.secondary(if(isHotSale) "nextSteamHotSale" else "nextSteamSale", "▶").withDisabled(moreFl) + ).queue() + } + } +} diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/service/SteamApiService.kt b/steam/src/main/kotlin/com/noobnuby/plugin/service/SteamApiService.kt index 154fe38..de5eb8d 100644 --- a/steam/src/main/kotlin/com/noobnuby/plugin/service/SteamApiService.kt +++ b/steam/src/main/kotlin/com/noobnuby/plugin/service/SteamApiService.kt @@ -17,14 +17,14 @@ object SteamApiService { BrowserUserAgent() } - suspend fun getSales(): SteamSaleData { - val url = "https://steamsale.windbell.co.kr/api/v1/sales/currents?keyword=&page=1&size=5" + suspend fun getSales(page: Int = 1): SteamSaleData { + val url = "https://steamsale.windbell.co.kr/api/v1/sales/currents?keyword=&page=${page}&size=5" return client.get(url).body() } - suspend fun getHotSales(): SteamSaleData { - val url = "https://steamsale.windbell.co.kr/api/v1/sales/tops?page=1&size=5" + suspend fun getHotSales(page: Int = 1): SteamSaleData { + val url = "https://steamsale.windbell.co.kr/api/v1/sales/tops?page=${page}&size=5" return client.get(url).body() }