Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
fix: fix message empty, #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Nambers committed Jun 21, 2022
1 parent f77ae9e commit ebb8c73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal val errCode = mapOf(
"4300" to "批量操作超过限制",
"4400" to "没有上传合法userid",
"4500" to "userid申请个数超过限制",
"4600" to "输入内容为空",
// "4600" to "输入内容为空",
"4602" to "输入文本内容超长(上限150)",
"7002" to "上传信息失败",
"8008" to "服务器错误"
Expand Down
15 changes: 8 additions & 7 deletions src/main/kotlin/PluginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.SingleMessage
import net.mamoe.mirai.message.data.content
import org.json.JSONObject
import tech.eritquearcus.tuling.TuringConfig.apikey
import tech.eritquearcus.tuling.TuringConfig.debug
Expand All @@ -38,12 +37,12 @@ import tech.eritquearcus.tuling.TuringConfig.overLimitReply

object PluginMain : KotlinPlugin(
JvmPluginDescription(
id = "tech.eritquearcus.TuLingBot", name = "TuLingBot", version = "1.6.0"
id = "tech.eritquearcus.TuLingBot", name = "TuLingBot", version = "1.7.0"
)
) {

private suspend fun SingleMessage.getResult(uinfo: TulingRequest.UserInfo): Pair<String, Int> {
if (content.isBlank() || content.isEmpty()) return Pair("", 1)
// if (content.isBlank() || content.isEmpty()) return Pair("", 1)
val text = Gson().toJson(toRequest(uinfo).apply {
if (this == null) {
logger.warning("遇到不能处理的消息类型: ${javaClass.name}")
Expand All @@ -58,7 +57,7 @@ object PluginMain : KotlinPlugin(
return Pair("", 2) // change apikey
}
logger.error("图灵服务返回异常: code: $code, msg: ${errCode[code.toString()]}, apikey: ${uinfo.apiKey}")
val re = if (overLimitReply.isNotEmpty()) overLimitReply.random() else errCode[code.toString()]!!
val re = if (overLimitReply.isNotEmpty()) overLimitReply.random() else "Err: " + errCode[code.toString()]!!
return Pair(re, 1) // equal to `break`
} else {
val re = (JSONObject(j).getJSONArray("results")[0] as JSONObject).getJSONObject("values").getString("text")
Expand All @@ -69,11 +68,13 @@ object PluginMain : KotlinPlugin(
private suspend fun MessageEvent.getResult(keyWords: List<String>) {
var reS = ""
run out@{
(if (keyWords.isEmpty()) this.message.toList()
var msgs = (if (keyWords.isEmpty()) this.message.toList()
else this.message.containKey(keyWords, this.bot).let {
if (it.isEmpty()) return
if (it == null) return
else return@let it
}).forEach foreach1@{ msg ->
})
if (msgs.isEmpty()) msgs = listOf(PlainText(""))
msgs.forEach foreach1@{ msg ->
var backup = ""
apikey.forEach { key ->
val uinfo = TulingRequest.UserInfo(
Expand Down
23 changes: 11 additions & 12 deletions src/main/kotlin/Uilts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.StandardCharsets

internal fun TulingRequest.Perception?.toRequest(uinfo: TulingRequest.UserInfo): TulingRequest? =
if (this == null) null
else TulingRequest(
this, when {
this.inputText != null -> 0
this.inputImage != null -> 1
this.inputMedia != null -> 2
else -> throw IllegalArgumentException("Unreachable")
}, uinfo
)
internal fun TulingRequest.Perception?.toRequest(uinfo: TulingRequest.UserInfo): TulingRequest? = if (this == null) null
else TulingRequest(
this, when {
this.inputText != null -> 0
this.inputImage != null -> 1
this.inputMedia != null -> 2
else -> throw IllegalArgumentException("Unreachable")
}, uinfo
)

internal suspend fun SingleMessage.toRequest(uinfo: TulingRequest.UserInfo): TulingRequest? = when (this) {
is PlainText -> TulingRequest.Perception(inputText = TulingRequest.Perception.InputText(this.content))
Expand All @@ -60,7 +59,7 @@ internal fun sendJson(out: String, debug: Boolean?): String {
return re
}

internal fun MessageChain.containKey(l: List<String>, bot: Bot): List<SingleMessage> {
internal fun MessageChain.containKey(l: List<String>, bot: Bot): List<SingleMessage>? {
l.forEach {
if (it == "@bot" && this.contains(At(bot))) return this.toMutableList().apply {
this.remove(At(bot))
Expand All @@ -71,5 +70,5 @@ internal fun MessageChain.containKey(l: List<String>, bot: Bot): List<SingleMess
this[0] = PlainText(this[0].contentToString().replace(it, ""))
}
}
return emptyList()
return null
}

0 comments on commit ebb8c73

Please sign in to comment.