mirror of
https://github.com/DreamExposure/DisCal-Discord-Bot.git
synced 2026-02-09 04:49:22 -06:00
This should fix wizards not auto clearing after timeout
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
package org.dreamexposure.discal.core.`object`
|
||||
|
||||
import discord4j.common.util.Snowflake
|
||||
import reactor.core.publisher.Flux
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
import java.time.temporal.ChronoUnit
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class Wizard<T: Pre> {
|
||||
private val active = ConcurrentHashMap<Snowflake, T>()
|
||||
|
||||
init {
|
||||
Flux.interval(Duration.ofMinutes(30))
|
||||
.map { removeOld() }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
fun get(id: Snowflake): T? {
|
||||
val p = active[id]
|
||||
if (p != null) p.lastEdit = Instant.now()
|
||||
@@ -16,4 +25,15 @@ class Wizard<T: Pre> {
|
||||
fun start(pre: T): T? = active.put(pre.guildId, pre)
|
||||
|
||||
fun remove(id: Snowflake): T? = active.remove(id)
|
||||
|
||||
private fun removeOld() {
|
||||
val toRemove = mutableListOf<Snowflake>()
|
||||
|
||||
active.forEach {
|
||||
if (Instant.now().isAfter(it.value.lastEdit.plus(30, ChronoUnit.MINUTES))) {
|
||||
toRemove.add(it.key)
|
||||
}
|
||||
}
|
||||
toRemove.forEach { active.remove(it) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user