Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
improve: cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Dec 23, 2023
1 parent 802bf23 commit c3fe6af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/main/kotlin/ltd/guimc/lgzbot/command/ACGCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package ltd.guimc.lgzbot.command

import kotlinx.coroutines.launch
import ltd.guimc.lgzbot.PluginMain
import ltd.guimc.lgzbot.utils.CooldownUtils
import ltd.guimc.lgzbot.utils.ImageUtils
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.SimpleCommand
Expand All @@ -20,14 +21,23 @@ object ACGCommand: SimpleCommand (
primaryName = "acg",
description = "二次元图片"
) {
val cooldown = CooldownUtils(10000)

@Handler
fun CommandSender.onHandler() = ltd_guimc_command_acg()

fun CommandSender.ltd_guimc_command_acg() = launch {
requireNotNull(user) { "请在聊天环境中使用该指令" }
if (cooldown.isTimePassed(user!!)) {
if (cooldown.shouldSendCooldownNotice(user!!)) sendMessage("你可以在 ${ACGCommand.cooldown.getLeftTime(user!!) / 1000} 秒后继续使用该指令")
return@launch
}
cooldown.flag(user!!)
try {
sendMessage(ImageUtils.url2imageMessage("https://www.dmoe.cc/random.php", bot!!, subject!!))
} catch (ignore: Throwable) {
sendMessage("Oops, something went wrong.")
cooldown.addLeftTime(user!!, -10000L)
}
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/ltd/guimc/lgzbot/command/FbCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package ltd.guimc.lgzbot.command
import kotlinx.coroutines.launch
import ltd.guimc.lgzbot.PluginMain
import ltd.guimc.lgzbot.PluginMain.fbValue
import ltd.guimc.lgzbot.utils.CooldownUtils
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.message.data.At
Expand All @@ -23,11 +24,19 @@ object FbCommand: SimpleCommand(
primaryName = "fb",
description = "嘿嘿...❤"
) {
val cooldown = CooldownUtils(30000)

@Handler
fun CommandSender.onHandler() = ltd_guimc_lgzbot_fb()

fun CommandSender.ltd_guimc_lgzbot_fb() = launch {
requireNotNull(user) { "我不能对着控制台发情" }
requireNotNull(user) { "请在聊天环境中使用该指令" }
if (cooldown.isTimePassed(user!!)) {
if (cooldown.shouldSendCooldownNotice(user!!)) sendMessage("你可以在 ${cooldown.getLeftTime(user!!) / 1000} 秒后继续使用该指令")
return@launch
}
cooldown.flag(user!!)

var msg: Message = PlainText("")

val context = fbValue.random().replace("\\n","\n")
Expand Down
17 changes: 8 additions & 9 deletions src/main/kotlin/ltd/guimc/lgzbot/command/HypixelCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package ltd.guimc.lgzbot.command

import kotlinx.coroutines.launch
import ltd.guimc.lgzbot.PluginMain
import ltd.guimc.lgzbot.utils.CooldownUtils
import ltd.guimc.lgzbot.utils.hypixel.HypixelApiUtils
import ltd.guimc.lgzbot.utils.hypixel.ExpCalculator
import ltd.guimc.lgzbot.utils.MojangAPIUtils
Expand All @@ -32,7 +33,7 @@ object HypixelCommand: SimpleCommand(
secondaryNames = arrayOf("hyp"),
description = "Hypixel Player Information"
) {
val cooldownMember = mutableMapOf<User, MSTimer>()
val cooldown = CooldownUtils(30000)

@Handler
fun CommandSender.onHandler(name: String) = ltd_guimc_command_hypixel(name)
Expand All @@ -42,17 +43,15 @@ object HypixelCommand: SimpleCommand(
requireNotNull(bot) { "Must have bot to use it" }
requireNotNull(user) { "Must have user to use it" }

val uuid = MojangAPIUtils.getUUIDByName(name)

if (user!! !in cooldownMember.keys) {
cooldownMember[user!!] = MSTimer()
} else if (!cooldownMember[user!!]!!.isTimePressed(60000)) {
sendMessage("你的冷却时间未过, 还需要等待 ${(60000 - cooldownMember[user!!]!!.hasTimePassed()) / 1000}")
if (cooldown.isTimePassed(user!!)) {
if (cooldown.shouldSendCooldownNotice(user!!)) sendMessage("你可以在 ${cooldown.getLeftTime(user!!) / 1000} 秒后继续使用该指令")
return@launch
} else {
cooldownMember[user!!]!!.reset()
}

val uuid = MojangAPIUtils.getUUIDByName(name)

cooldown.flag(user!!)

val outputMessage = ForwardMessageBuilder(bot!!.asFriend)

val playerInfo = HypixelApiUtils.request("/player?uuid=${uuid}").getJSONObject("player")
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/ltd/guimc/lgzbot/utils/CooldownUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ltd.guimc.lgzbot.utils

class CooldownUtils(val cooldown: Long) {
var cooldownMap: MutableMap<Any, Long> = mutableMapOf()
var cooldownNoticeMap: MutableMap<Any, Long> = mutableMapOf()

fun flag(target: Any) {
cooldownMap[target] = System.currentTimeMillis()
}

fun isTimePassed(target: Any): Boolean {
return isTimePassed(target, cooldown)
}

fun isTimePassed(target: Any, time: Long): Boolean {
if (cooldownMap.keys.indexOf(target) == -1) return true
if (System.currentTimeMillis() - cooldownMap[target]!! >= time) return true
return false
}

fun shouldSendCooldownNotice(target: Any): Boolean {
if (cooldownNoticeMap.keys.indexOf(target) == -1) return true
if (System.currentTimeMillis() - cooldownNoticeMap[target]!! >= 3000) {
cooldownNoticeMap[target] = System.currentTimeMillis()
return true
}
return false
}

fun getLeftTime(target: Any): Long {
return getLeftTime(target, cooldown)
}

fun getLeftTime(target: Any, time: Long): Long {
if (cooldownMap.keys.indexOf(target) == -1) return -1
return System.currentTimeMillis() - cooldownMap[target]!! - time
}

fun addLeftTime(target: Any, time: Long) {
if (cooldownMap.keys.indexOf(target) == -1) return
cooldownMap[target] = cooldownMap[target]!! + time
}
}

0 comments on commit c3fe6af

Please sign in to comment.