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.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<InteractionHook, Int>()
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<SteamSaleData>, hot: Boolean,user: User): MessageEmbed {
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.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<String, ButtonState>()
@ -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,35 +40,8 @@ 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) "previousSteamHotSale" else "previousSteamSale", "")
.withDisabled(buttonState[ev.message.id]!!.page <= 1),
Button.secondary(if (isHotSale) "nextSteamHotSale" else "nextSteamSale", "").withDisabled(moreFl)
).queue()
}