chlwjrghk rnlcksgdk

This commit is contained in:
noobnuby 2024-10-21 00:01:13 +09:00
parent 8f3681e004
commit 0ce15945f4
8 changed files with 114 additions and 20 deletions

1
.gitignore vendored
View file

@ -5,6 +5,7 @@ build/
!**/src/test/**/build/ !**/src/test/**/build/
### IntelliJ IDEA ### ### IntelliJ IDEA ###
.idea
.idea/modules.xml .idea/modules.xml
.idea/jarRepositories.xml .idea/jarRepositories.xml
.idea/compiler.xml .idea/compiler.xml

View file

@ -9,17 +9,16 @@ repositories {
mavenCentral() mavenCentral()
} }
//tasks { tasks {
// shadowJar { shadowJar {
// archiveBaseName.set(rootProject.name) doLast {
// archiveClassifier.set("") copy {
// archiveVersion.set("") from(archiveFile)
// into(File("C:\\Users\\user\\Desktop\\px32\\plugins"))
// manifest { }
// attributes["Main-Class"] = "com.noobnuby.plugin.MainKt" }
// } }
// } }
//}
kotlin { kotlin {
jvmToolchain(21) jvmToolchain(21)

View file

@ -1,6 +1,7 @@
package com.noobnuby.plugin package com.noobnuby.plugin
import com.noobnuby.plugin.command.Steam 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.Plugin
import net.projecttl.p.x32.api.command.commandHandler import net.projecttl.p.x32.api.command.commandHandler
@ -9,7 +10,7 @@ class Main : Plugin() {
logger.info("Enable Steam plugin!") logger.info("Enable Steam plugin!")
// addHandler(Ready) addHandler(ButtonClick)
addHandler(commandHandler { handler -> addHandler(commandHandler { handler ->
handler.addCommand(Steam) handler.addCommand(Steam)
}) })

View file

@ -1,14 +1,19 @@
package com.noobnuby.plugin.command package com.noobnuby.plugin.command
import com.noobnuby.plugin.data.ButtonState
import com.noobnuby.plugin.data.SteamSaleData import com.noobnuby.plugin.data.SteamSaleData
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.runBlocking import kotlinx.coroutines.runBlocking
import net.dv8tion.jda.api.EmbedBuilder import net.dv8tion.jda.api.EmbedBuilder
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.events.interaction.command.SlashCommandInteractionEvent 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.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
object Steam : GlobalCommand { object Steam : GlobalCommand {
override val data = useCommand { override val data = useCommand {
@ -35,18 +40,26 @@ object Steam : GlobalCommand {
SteamApiService.getHotSales().list SteamApiService.getHotSales().list
} }
when (ev.subcommandName) { 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<SteamSaleData>, hot: Boolean): MessageEmbed { fun steamSaleEmbed(list: List<SteamSaleData>, hot: Boolean,user: User): MessageEmbed {
val embed = EmbedBuilder() val embed = EmbedBuilder()
if (hot) embed.setTitle("스팀 인기 할인목록") if (hot) embed.setTitle("스팀 인기 할인목록")
else embed.setTitle("스팀 할인목록") else embed.setTitle("스팀 할인목록")
@ -54,10 +67,12 @@ object Steam : GlobalCommand {
list.forEach { list.forEach {
val saleInfo = """ val saleInfo = """
~~${it.full_price_va.formatWon()}~~ ${it.sale_price_va.formatWon()} ~~${it.full_price_va.formatWon()}~~ ${it.sale_price_va.formatWon()}
[link](${it.store_lk})
""".trimIndent() """.trimIndent()
embed.addField("${it.title_nm} (${(it.discount_rt * 100).toInt()}%)", saleInfo, false) embed.addField("${it.title_nm} (${(it.discount_rt * 100).toInt()}%)", saleInfo, false)
} }
embed.footer(user)
return embed.build() return embed.build()
} }

View file

@ -0,0 +1,3 @@
package com.noobnuby.plugin.data
data class ButtonState(val page: Int,val hot: Boolean)

View file

@ -15,5 +15,6 @@ data class SteamSaleData(
val start_dt: String, val start_dt: String,
val end_dt: String, val end_dt: String,
val total_cn: Int, val total_cn: Int,
val list: List<SteamSaleData> val list: List<SteamSaleData>,
val more_fl: Boolean
) )

View file

@ -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<String, ButtonState>()
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()
}
}
}

View file

@ -17,14 +17,14 @@ object SteamApiService {
BrowserUserAgent() BrowserUserAgent()
} }
suspend fun getSales(): SteamSaleData { suspend fun getSales(page: Int = 1): SteamSaleData {
val url = "https://steamsale.windbell.co.kr/api/v1/sales/currents?keyword=&page=1&size=5" val url = "https://steamsale.windbell.co.kr/api/v1/sales/currents?keyword=&page=${page}&size=5"
return client.get(url).body() return client.get(url).body()
} }
suspend fun getHotSales(): SteamSaleData { suspend fun getHotSales(page: Int = 1): SteamSaleData {
val url = "https://steamsale.windbell.co.kr/api/v1/sales/tops?page=1&size=5" val url = "https://steamsale.windbell.co.kr/api/v1/sales/tops?page=${page}&size=5"
return client.get(url).body() return client.get(url).body()
} }