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 6732d27..2156226 100644 --- a/steam/src/main/kotlin/com/noobnuby/plugin/command/Steam.kt +++ b/steam/src/main/kotlin/com/noobnuby/plugin/command/Steam.kt @@ -5,17 +5,32 @@ 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.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import kotlinx.serialization.builtins.serializer import net.dv8tion.jda.api.EmbedBuilder +import net.dv8tion.jda.api.entities.Message 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.events.interaction.component.ButtonInteractionEvent +import net.dv8tion.jda.api.interactions.InteractionHook +import net.dv8tion.jda.api.interactions.components.ActionRow +import net.dv8tion.jda.api.interactions.components.LayoutComponent import net.dv8tion.jda.api.interactions.components.buttons.Button +import net.dv8tion.jda.api.requests.RestAction import net.projecttl.p.x32.api.command.GlobalCommand import net.projecttl.p.x32.api.command.useCommand import net.projecttl.p.x32.api.util.footer +import java.time.Duration +import java.util.concurrent.TimeUnit +import java.util.function.Consumer object Steam : GlobalCommand { + private val buttonTime = mutableMapOf() + override val data = useCommand { name = "스팀" description = "스팀 관련 명령어 입니다." @@ -40,24 +55,27 @@ object Steam : GlobalCommand { SteamApiService.getHotSales().list } - when (ev.subcommandName) { "할인목록" -> { ev.replyEmbeds(steamSaleEmbed(sale, false, ev.user)).addActionRow( Button.secondary("previousSteamSale", "◀").asDisabled(), Button.secondary("nextSteamSale", "▶") - ).queue() - + ).queue { + it.deleteComponents().queueAfter(10, TimeUnit.SECONDS) + } } "인기할인" -> { ev.replyEmbeds(steamSaleEmbed(hotSale, true, ev.user)).addActionRow( Button.secondary("previousSteamHotSale", "◀").asDisabled(), Button.secondary("nextSteamHotSale", "▶") - ).queue() + ).queue { + it.deleteComponents().queueAfter(10, TimeUnit.SECONDS) + } } } } + private fun InteractionHook.deleteComponents() = editOriginalComponents() fun steamSaleEmbed(list: List, hot: Boolean,user: User): MessageEmbed { val embed = EmbedBuilder() diff --git a/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt b/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt index 410f551..b1ce371 100644 --- a/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt +++ b/steam/src/main/kotlin/com/noobnuby/plugin/handler/ButtonClick.kt @@ -7,6 +7,7 @@ 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 +import net.dv8tion.jda.api.interactions.components.buttons.ButtonInteraction object ButtonClick : ListenerAdapter() { private val buttonState = mutableMapOf() @@ -15,12 +16,13 @@ object ButtonClick : ListenerAdapter() { val buttonId = ev.componentId val currentState = buttonState.getOrDefault(ev.message.id, ButtonState(1, false)) - if (buttonId == "nextSteamSale" || buttonId == "nextSteamHotSale") { - val isHotSale = buttonId == "nextSteamHotSale" + ev.messageId - buttonState[ev.message.id] = currentState.copy(page = currentState.page + 1) + if (buttonId == "nextSteamSale" || buttonId == "nextSteamHotSale" || buttonId == "previousSteamSale" || buttonId == "previousSteamHotSale") { + val isHotSale = buttonId == "nextSteamHotSale" || buttonId == "previousSteamHotSale" + val isNext = buttonId.startsWith("next") - println("next " + buttonState[ev.message.id]) + buttonState[ev.message.id] = currentState.copy(page = currentState.page + if (isNext) 1 else -1) val sale = runBlocking { if (isHotSale) { @@ -38,36 +40,9 @@ object ButtonClick : ListenerAdapter() { } 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) + Button.secondary(if (isHotSale) "previousSteamHotSale" else "previousSteamSale", "◀") + .withDisabled(buttonState[ev.message.id]!!.page <= 1), + Button.secondary(if (isHotSale) "nextSteamHotSale" else "nextSteamSale", "▶").withDisabled(moreFl) ).queue() } }