io.github.freya022.botcommands.internal.commands.ratelimit.CancellableRateLimitImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.internal.commands.ratelimit
import io.github.bucket4j.Bucket
import io.github.freya022.botcommands.api.commands.ratelimit.CancellableRateLimit
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
internal class CancellableRateLimitImpl internal constructor(private val bucket: Bucket) : CancellableRateLimit {
private val lock = ReentrantLock()
override var isRateLimitCancelled: Boolean = false
override fun cancelRateLimit() = lock.withLock {
if (!isRateLimitCancelled) {
isRateLimitCancelled = true
bucket.addTokens(1)
}
}
}