Add Search addon by project id

Add `/jcf id` Command
This commit is contained in:
筱傑 2021-11-01 22:04:55 +08:00
parent 05a50b3b44
commit a7d1370c39
2 changed files with 28 additions and 0 deletions

View File

@ -50,4 +50,9 @@ object JcfCommand : CompositeCommand(
// MinecraftService.search(this, MinecraftService.SECTION_ID_WORLDS, filter)
// }
@SubCommand("id")
@Description("根据id查找")
suspend fun UserCommandSender.getAddonById(projectId: Int) {
MinecraftService.searchAddonByProjectId(this, projectId)
}
}

View File

@ -1,5 +1,7 @@
package me.jie65535.jcf
import io.ktor.client.features.*
import io.ktor.http.*
import kotlinx.coroutines.TimeoutCancellationException
import me.jie65535.jcf.model.addon.Addon
import me.jie65535.jcf.model.addon.AddonSortMethod
@ -77,6 +79,27 @@ object MinecraftService {
showAddon(sender, addon)
}
/**
* 根据项目ID搜索资源
*/
suspend fun searchAddonByProjectId(sender: UserCommandSender, projectId: Int) {
try {
val addon = curseClient.getAddon(projectId)
showAddon(sender, addon)
} catch (e: ClientRequestException) {
if (e.response.status == HttpStatusCode.NotFound) {
sender.sendMessage("未搜索到指定项目ID的资源请使用正确的项目ID")
} else {
sender.sendMessage("请求异常,错误代码:" + e.response.status)
throw e
}
} catch (e: Throwable) {
sender.sendMessage("内部发生异常,此次查询已取消,请重新查询。")
throw e
}
}
private suspend fun showAddon(sender: UserCommandSender, addon: Addon) {
val builder = MessageHandler.parseAddon(addon, sender.subject)
if (addon.latestFiles.isNotEmpty())