-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Goksi/cooldowns
Implementing button cooldowns
- Loading branch information
Showing
6 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/kotlin/tech/goksi/pterobot/util/cooldown/CooldownManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package tech.goksi.pterobot.util.cooldown | ||
|
||
import dev.minn.jda.ktx.events.onButton | ||
import dev.minn.jda.ktx.interactions.components.button | ||
import dev.minn.jda.ktx.jdabuilder.scope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import net.dv8tion.jda.api.JDA | ||
import net.dv8tion.jda.api.entities.User | ||
import net.dv8tion.jda.api.entities.emoji.Emoji | ||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent | ||
import net.dv8tion.jda.api.interactions.components.buttons.Button | ||
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle | ||
import tech.goksi.pterobot.manager.ConfigManager | ||
import tech.goksi.pterobot.manager.EmbedManager | ||
import tech.goksi.pterobot.manager.EmbedManager.replace | ||
import tech.goksi.pterobot.manager.EmbedManager.toEmbed | ||
import java.util.concurrent.ThreadLocalRandom | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.math.max | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
object CooldownManager { | ||
private val cooldownMapping: MutableMap<CooldownNamespace, Long> = HashMap() | ||
|
||
private fun applyCooldown(event: ButtonInteractionEvent) { | ||
val cooldownType = CooldownType.fromEvent(event) | ||
cooldownMapping[CooldownNamespace(event.user.idLong, cooldownType)] = | ||
System.currentTimeMillis() + cooldownType.millis | ||
} | ||
|
||
private fun canInteract(event: ButtonInteractionEvent): Boolean = getRemaining(event) == 0L | ||
|
||
private fun getRemaining(event: ButtonInteractionEvent): Long = max( | ||
0, | ||
( | ||
cooldownMapping[CooldownNamespace(event.user.idLong, CooldownType.fromEvent(event))] ?: 0 | ||
) - System.currentTimeMillis() | ||
) | ||
|
||
private fun getRemainingSeconds(event: ButtonInteractionEvent): Long = | ||
TimeUnit.MILLISECONDS.toSeconds(getRemaining(event)) | ||
|
||
fun JDA.cooldownButton( | ||
style: ButtonStyle, | ||
label: String? = null, | ||
emoji: Emoji?, | ||
disabled: Boolean = false, | ||
expiration: Duration = 15.minutes, | ||
user: User? = null, | ||
type: CooldownType = CooldownType.UNDEFINED, | ||
listener: suspend (ButtonInteractionEvent) -> Unit | ||
): Button { | ||
val id = type.name + ":${ThreadLocalRandom.current().nextLong()}" | ||
val button = button(id, label, emoji, style, disabled) | ||
val task = onButton(id, expiration) { | ||
if (user == null || user == it.user) { | ||
if (!canInteract(it)) { | ||
it.replyEmbeds( | ||
EmbedManager.getGenericFailure( | ||
ConfigManager.config.getString("Messages.OnCooldown") | ||
.replace("%time" to "${getRemainingSeconds(it)}") | ||
).toEmbed(it.jda) | ||
).setEphemeral(true).queue() | ||
return@onButton | ||
} | ||
listener(it) | ||
applyCooldown(it) | ||
} | ||
if (!it.isAcknowledged) it.deferEdit().queue() | ||
} | ||
if (expiration.isPositive() && expiration.isFinite()) { | ||
scope.launch { | ||
delay(expiration) | ||
removeEventListener(task) | ||
} | ||
} | ||
return button | ||
} | ||
} | ||
|
||
private data class CooldownNamespace(val id: Long, val type: CooldownType) |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/tech/goksi/pterobot/util/cooldown/CooldownType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tech.goksi.pterobot.util.cooldown | ||
|
||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent | ||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent | ||
import tech.goksi.pterobot.manager.ConfigManager | ||
import java.util.concurrent.TimeUnit | ||
|
||
private const val BUTTON_CONFIG = "Cooldown.Button." | ||
|
||
enum class CooldownType(coolDownConfig: String) { | ||
STATUS_BTN("StatusChange"), | ||
RESTART_BTN("RestartServer"), | ||
COMMAND_BTN("SendCommand"), | ||
REFRESH_BTN("RefreshEmbed"), | ||
LOGS_BTN("RequestLogs"), | ||
UNDEFINED(""); | ||
|
||
private val seconds: Long = ConfigManager.config.getLong("$BUTTON_CONFIG$coolDownConfig") | ||
val millis | ||
get() = TimeUnit.SECONDS.toMillis(seconds) | ||
|
||
companion object { | ||
fun fromEvent(event: ButtonInteractionEvent): CooldownType = valueOf(event.button.id!!.split(":")[0]) | ||
|
||
fun fromEvent(event: SlashCommandInteractionEvent): CooldownType { | ||
TODO("Not impl") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters