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