feat: add exception catch

This commit is contained in:
2024-09-20 11:43:40 +08:00
parent 55af38931e
commit b37d8eb37d
1 file changed
+34 -27
@@ -34,32 +34,39 @@ class WikipediaCommand : BaseCommand() {
message.reply(msg) message.reply(msg)
} }
val title = args.first() val title = args.first()
val response = Http.get<WikipediaResponse>( try {
wikipediaAPI, val response = Http.get<WikipediaResponse>(
mapOf( wikipediaAPI,
"action" to "query", mapOf(
"list" to "search", "action" to "query",
"srsearch" to title, "list" to "search",
"format" to "json" "srsearch" to title,
) "format" to "json"
).query.search.first() )
val fullUrl = Http.get<PageInfoResponse>( ).query.search.first()
wikipediaAPI, val fullUrl = Http.get<PageInfoResponse>(
mapOf( wikipediaAPI,
"action" to "query", mapOf(
"prop" to "info", "action" to "query",
"pageids" to response.pageId, "prop" to "info",
"inprop" to "url", "pageids" to response.pageId,
"format" to "json" "inprop" to "url",
) "format" to "json"
).query.pages.values.first().fullUrl )
val msg = MessageChain.Builder() ).query.pages.values.first().fullUrl
.addText("标题: ${response.title} 内容片段如下") val msg = MessageChain.Builder()
.addNewLine() .addText("标题: ${response.title} 内容片段如下")
.addText(response.snippet.extractPlainTextFromHtml()) .addNewLine()
.addNewLine(2) .addText(response.snippet.extractPlainTextFromHtml())
.addText(fullUrl) .addNewLine(2)
.build() .addText(fullUrl)
message.reply(msg) .build()
message.reply(msg)
} catch (_: NoSuchElementException) {
val msg = MessageChain.Builder()
.addText("没有查询到指定的wiki页面呢 >>> $title")
.build()
message.reply(msg)
}
} }
} }