Skip to content

Commit

Permalink
fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Jul 6, 2024
1 parent 864a89f commit 776e270
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 22 additions & 17 deletions src/main/kotlin/ltd/guimc/lgzbot/command/HttpCatCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,41 @@ 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 ltd.guimc.lgzbot.utils.OverflowUtils
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.message.data.Image.Key.isUploaded
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import net.mamoe.mirai.console.command.getGroupOrNull
import top.mrxiaom.overflow.OverflowAPI

object HttpCatCommand: SimpleCommand (
owner = PluginMain,
primaryName = "httpcat",
description = "Funny Cat"
) {
val cooldown = CooldownUtils(10000)

@Handler
fun CommandSender.onHandler(code: Int) = ltd_guimc_command_httpcat(code)

fun CommandSender.ltd_guimc_command_httpcat(code: Int) = launch{
require(!(subject == null || bot == null))
val httpclients = HttpClients.createDefault()
val httpget = HttpGet("https://http.cat/$code")
val entity = httpclients.execute(httpget).entity
if (entity != null) {
val inputstream = entity.content
if (inputstream != null) {
val image = subject!!.uploadImage(inputstream.toExternalResource())
if (image.isUploaded(bot!!)) {
subject?.sendMessage(image)
return@launch
}
requireNotNull(user) { "请在聊天环境中使用该指令" }
val group = getGroupOrNull()
if (!cooldown.isTimePassed(user!!)) {
if (cooldown.shouldSendCooldownNotice(user!!)) sendMessage("你可以在 ${cooldown.getLeftTime(user!!) / 1000} 秒后继续使用该指令")
return@launch
}
cooldown.flag(user!!)
try {
if (!OverflowUtils.checkOverflowCore()) {
sendMessage(ImageUtils.url2imageMessage("https://http.cat/$code", bot!!, subject!!))
} else {
sendMessage(OverflowAPI.get().imageFromFile("https://http.cat/$code"))
}
} catch (ignore: Throwable) {
sendMessage("Oops, something went wrong.")
cooldown.addLeftTime(user!!, -10000L)
}
subject?.sendMessage("Oops, something went wrong.")
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/ltd/guimc/lgzbot/command/LGZBotCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ object LGZBotCommand : CompositeCommand(
var str = string
if (string == "reply") {
val quote = fromEvent.message.findIsInstance<QuoteReply>() ?: return
val raw = MiraiHibernateRecorder[quote.source] as MessageChain
val raw = MiraiHibernateRecorder[quote.source][0].toMessageChain()
str = raw.getPlainText()
}
if (LL4JUtils.predictAllResult(str).let { it[1] > it[0] }) {
Expand Down

0 comments on commit 776e270

Please sign in to comment.