Added a simple cooldown system that shouldn't work but does work

idk tbh
This commit is contained in:
Julius de Jeu 2018-10-30 20:59:07 +01:00
parent c3f183f43c
commit e1a9341b2e
3 changed files with 40 additions and 38 deletions

View file

@ -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()

View file

@ -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!")
*//*
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)
}
}
} else
event.client.applyCooldown(key, cooldown)
}*/
// run
try {

View file

@ -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<Long, GuildSettings>()
@ -57,9 +58,35 @@ data class UnityCommandClient(
private val prefix: String,
private val commands: MutableList<UnityCommand> = mutableListOf()
) : ListenerAdapter() {
private val cooldownMap = mutableMapOf<String, OffsetDateTime>()
private val cooldownMap = mutableMapOf<String, LocalDateTime>()
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()