This commit is contained in:
noobnuby 2024-10-24 02:42:49 +09:00
parent 0ce15945f4
commit e3aa1b97b2
2 changed files with 31 additions and 38 deletions

View file

@ -5,17 +5,32 @@ import com.noobnuby.plugin.data.SteamSaleData
import com.noobnuby.plugin.handler.ButtonClick import com.noobnuby.plugin.handler.ButtonClick
import com.noobnuby.plugin.service.SteamApiService import com.noobnuby.plugin.service.SteamApiService
import com.noobnuby.plugin.util.formatWon import com.noobnuby.plugin.util.formatWon
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.serialization.builtins.serializer
import net.dv8tion.jda.api.EmbedBuilder 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.MessageEmbed
import net.dv8tion.jda.api.entities.User import net.dv8tion.jda.api.entities.User
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent 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.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.GlobalCommand
import net.projecttl.p.x32.api.command.useCommand import net.projecttl.p.x32.api.command.useCommand
import net.projecttl.p.x32.api.util.footer 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 { object Steam : GlobalCommand {
private val buttonTime = mutableMapOf<InteractionHook, Int>()
override val data = useCommand { override val data = useCommand {
name = "스팀" name = "스팀"
description = "스팀 관련 명령어 입니다." description = "스팀 관련 명령어 입니다."
@ -40,24 +55,27 @@ object Steam : GlobalCommand {
SteamApiService.getHotSales().list SteamApiService.getHotSales().list
} }
when (ev.subcommandName) { when (ev.subcommandName) {
"할인목록" -> { "할인목록" -> {
ev.replyEmbeds(steamSaleEmbed(sale, false, ev.user)).addActionRow( ev.replyEmbeds(steamSaleEmbed(sale, false, ev.user)).addActionRow(
Button.secondary("previousSteamSale", "").asDisabled(), Button.secondary("previousSteamSale", "").asDisabled(),
Button.secondary("nextSteamSale", "") Button.secondary("nextSteamSale", "")
).queue() ).queue {
it.deleteComponents().queueAfter(10, TimeUnit.SECONDS)
}
} }
"인기할인" -> { "인기할인" -> {
ev.replyEmbeds(steamSaleEmbed(hotSale, true, ev.user)).addActionRow( ev.replyEmbeds(steamSaleEmbed(hotSale, true, ev.user)).addActionRow(
Button.secondary("previousSteamHotSale", "").asDisabled(), Button.secondary("previousSteamHotSale", "").asDisabled(),
Button.secondary("nextSteamHotSale", "") Button.secondary("nextSteamHotSale", "")
).queue() ).queue {
it.deleteComponents().queueAfter(10, TimeUnit.SECONDS)
} }
} }
} }
}
private fun InteractionHook.deleteComponents() = editOriginalComponents()
fun steamSaleEmbed(list: List<SteamSaleData>, hot: Boolean,user: User): MessageEmbed { fun steamSaleEmbed(list: List<SteamSaleData>, hot: Boolean,user: User): MessageEmbed {
val embed = EmbedBuilder() val embed = EmbedBuilder()

View file

@ -7,6 +7,7 @@ import kotlinx.coroutines.runBlocking
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.dv8tion.jda.api.interactions.components.buttons.Button import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.interactions.components.buttons.ButtonInteraction
object ButtonClick : ListenerAdapter() { object ButtonClick : ListenerAdapter() {
private val buttonState = mutableMapOf<String, ButtonState>() private val buttonState = mutableMapOf<String, ButtonState>()
@ -15,12 +16,13 @@ object ButtonClick : ListenerAdapter() {
val buttonId = ev.componentId val buttonId = ev.componentId
val currentState = buttonState.getOrDefault(ev.message.id, ButtonState(1, false)) val currentState = buttonState.getOrDefault(ev.message.id, ButtonState(1, false))
if (buttonId == "nextSteamSale" || buttonId == "nextSteamHotSale") { ev.messageId
val isHotSale = buttonId == "nextSteamHotSale"
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 { val sale = runBlocking {
if (isHotSale) { if (isHotSale) {
@ -38,35 +40,8 @@ object ButtonClick : ListenerAdapter() {
} }
ev.editMessageEmbeds(Steam.steamSaleEmbed(sale, isHotSale, ev.user)).setActionRow( ev.editMessageEmbeds(Steam.steamSaleEmbed(sale, isHotSale, ev.user)).setActionRow(
Button.secondary(if(isHotSale) "previousSteamHotSale" else "previousSteamSale", ""), Button.secondary(if (isHotSale) "previousSteamHotSale" else "previousSteamSale", "")
Button.secondary("nextSteamSale", "").withDisabled(moreFl) .withDisabled(buttonState[ev.message.id]!!.page <= 1),
).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) "nextSteamHotSale" else "nextSteamSale", "").withDisabled(moreFl)
).queue() ).queue()
} }