-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send and receive CQCode support (not tested)
- Loading branch information
Showing
4 changed files
with
140 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cn.evolvefield.onebot.sdk.util | ||
|
||
import com.google.gson.JsonArray | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonPrimitive | ||
|
||
object CQCode { | ||
fun fromJson(json: JsonArray): String { | ||
return buildString { | ||
for (ele in json) { | ||
val obj = ele.asJsonObject | ||
val type = obj["type"].asString | ||
val data = obj["data"] | ||
if (data is JsonObject) { | ||
// 纯文本 | ||
if (type == "text") { | ||
append(data["text"].asString | ||
.replace("&", "&") | ||
.replace("[", "[") | ||
.replace("]", "]")) | ||
continue | ||
} | ||
append("[CQ:$type") | ||
for ((key, value) in data.asMap()) { | ||
// 约定: key 不会出现需要转义的字符 | ||
append(",$key=") | ||
append( | ||
if (value is JsonPrimitive) { | ||
value.asString | ||
} else { | ||
value.toString() | ||
}.replace("&", "&") | ||
.replace("[", "[") | ||
.replace("]", "]") | ||
.replace(",", ",") | ||
) | ||
} | ||
} | ||
append("]") | ||
} | ||
} | ||
} | ||
|
||
fun toJson(s: String): JsonArray { | ||
val array = JsonArray() | ||
fun add(type: String, data: JsonObject.() -> Unit) { | ||
array.add(JsonObject().also { | ||
it.addProperty("type", type) | ||
it.add("data", JsonObject().apply(data)) | ||
}) | ||
} | ||
fun String.decode(): String { | ||
return replace("&", "&") | ||
.replace("[", "[") | ||
.replace("]", "]") | ||
} | ||
fun String.decodeValue(): String { | ||
return decode() | ||
.replace(",", ",") | ||
} | ||
fun StringBuilder.decode(): String { | ||
return toString() | ||
.decode() | ||
.also { clear() } | ||
} | ||
val temp = StringBuilder() | ||
var flag = false | ||
for (c in s) { | ||
if (c == '[' && temp.isNotEmpty()) { | ||
add("text") { | ||
addProperty("text", temp.decode()) | ||
} | ||
} | ||
temp.append(c) | ||
if (!flag && temp.startsWith("[CQ:")) flag = true | ||
if (flag && c == ']') { | ||
val cq = temp.substring(4, temp.length - 1) | ||
if (cq.contains(',')) { | ||
val split = cq.split(',') | ||
add(split[0]) { | ||
for (s1 in split.drop(1)) { | ||
if (!s1.contains('=')) continue | ||
val (key, value) = s1 | ||
.split('=', limit = 2) | ||
.run { this[0] to this[1].decodeValue() } | ||
addProperty(key, value) | ||
} | ||
} | ||
} else add(cq) { } | ||
temp.clear() | ||
flag = false | ||
} | ||
} | ||
if (temp.isNotEmpty()) { | ||
add("text") { | ||
addProperty("text", temp.decode()) | ||
} | ||
} | ||
return array | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters