diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt index 492afe7..9956405 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt @@ -22,7 +22,8 @@ val helloCommand = UnityCommand("hello", "Say hello to Andy!", aliases = *arrayO } -val pingCommand = UnityCommand("ping", help = "Check the bot's ping", aliases = *arrayOf("pong")) { event -> +val pingCommand = + UnityCommand("ping", help = "Check the bot's ping", aliases = *arrayOf("pong"), cooldown = 10) { event -> event.reply("Ping: ...") { m -> val ping = event.message.creationTime.until(m.creationTime, ChronoUnit.MILLIS) m.editMessage("Ping: " + ping + "ms | Websocket: " + event.jda.ping + "ms").queue() diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt index dacd625..19b35ea 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt @@ -30,21 +30,16 @@ open class UnityCommand( } - /*//cooldown check + //cooldown check if (cooldown > 0) { - val key = getCooldownKey(event) - val remaining = event.client.getRemainingCooldown(key) - if (remaining > 0) { - val error = getCooldownError(event, remaining) - if (error != null) { -*//* - event.reply("There still is a cooldown (`$error`) on this command!") -*//* - return - } - } else - event.client.applyCooldown(key, cooldown) - }*/ + + if (event.client.isOnCooldown(name)) { + event.reply("Sorry, this command is on cooldown for `${event.client.getRemainingCooldown(name)}` seconds!") + return + } else { + event.client.applyCooldown(name, cooldown) + } + } // run try { diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommandClient.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommandClient.kt index 304a449..fa77297 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommandClient.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommandClient.kt @@ -19,6 +19,7 @@ import java.time.LocalDateTime import java.time.OffsetDateTime import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.ScheduledThreadPoolExecutor +import kotlin.math.absoluteValue val gmap = mutableMapOf() @@ -57,9 +58,35 @@ data class UnityCommandClient( private val prefix: String, private val commands: MutableList = mutableListOf() ) : ListenerAdapter() { - private val cooldownMap = mutableMapOf() + + private val cooldownMap = mutableMapOf() + fun applyCooldown(name: String, seconds: Int) { - cooldownMap[name] = OffsetDateTime.now().plusSeconds(seconds.toLong()) + cooldownMap[name] = LocalDateTime.now().plusSeconds(seconds.toLong()) + } + + fun getRemainingCooldown(name: String): Int { + val cd = cooldownMap[name] + return if (cd == null) { + 0 + } else { + (LocalDateTime.now().second - cd.second).absoluteValue + } + } + + fun isOnCooldown(name: String): Boolean { + val cooldown: Boolean? = LocalDateTime.now().isBefore(cooldownMap[name] ?: LocalDateTime.MIN) + + return cooldown ?: false + } + + + fun cleanCooldowns() { + for ((k, v) in cooldownMap) { + if (v.isBefore(LocalDateTime.now())) { + cooldownMap.remove(k) + } + } } fun getError(): String = "❌" @@ -73,13 +100,6 @@ data class UnityCommandClient( fun getScheduleExecutor(): ScheduledExecutorService = ses - fun cleanCooldowns() { - for ((k, v) in cooldownMap) { - if (v.isBefore(OffsetDateTime.now())) { - cooldownMap.remove(k) - } - } - } fun getWarning(): String = "⚠️" @@ -101,7 +121,6 @@ data class UnityCommandClient( private fun getSettings(guild: Guild): GuildSettings = GSM.getSettings(guild) - private val start = OffsetDateTime.now() fun getStartTime(): OffsetDateTime = start @@ -123,9 +142,6 @@ data class UnityCommandClient( fun getCommandUses(name: String): Int = usageMap[name] ?: 0 - fun getCooldown(name: String): OffsetDateTime? = cooldownMap[name] - - fun shutdown() { ses.shutdown() GSM.shutdown() @@ -138,16 +154,6 @@ data class UnityCommandClient( fun getTotalGuilds(): Int = bot.guilds.size - fun getRemainingCooldown(name: String): Int { - val cd = cooldownMap[name] - return if (cd == null) { - 0 - } else { - (OffsetDateTime.now().toEpochSecond() - cd.toEpochSecond()).toInt() - } - - } - val ownerId = "168743656738521088" fun getOwnerIdLong(): Long = ownerId.toLong()