From 35742632b32bee4e8fc7fc8af5c6863eee7159f6 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Tue, 4 Feb 2020 15:56:43 +0800 Subject: [PATCH] Group Card --- .../net/mamoe/mirai/qqandroid/ContactImpl.kt | 1 + .../network/QQAndroidBotNetworkHandler.kt | 9 ++++++-- .../kotlin/net.mamoe.mirai/contact/Member.kt | 6 +++++ .../main/kotlin/demo/gentleman/GentleImage.kt | 23 +++++++++---------- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt index e5e7bd4a4..04b491851 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt @@ -61,6 +61,7 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin internal class MemberImpl( qq: QQImpl, + override var groupCard: String, group: GroupImpl, override val coroutineContext: CoroutineContext, override val permission: MemberPermission diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt index 6a6e9a1b7..57fcd0205 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt @@ -174,6 +174,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler group.owner = MemberImpl( qq = bot.QQ(it.dwGroupOwnerUin) as QQImpl, + groupCard = "",//unknown now group = group, coroutineContext = group.coroutineContext, permission = MemberPermission.OWNER @@ -253,6 +254,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler list.delegate.addLast( MemberImpl( qq = bot.QQ(it.memberUin) as QQImpl, + groupCard = it.sShowName ?: it.sName ?: it.nick, group = group, coroutineContext = group.coroutineContext, permission = when { @@ -262,8 +264,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler } ) ) - } else if (it.dwFlag == 1L) { - group.botPermission = MemberPermission.ADMINISTRATOR + } else { + group.owner.groupCard = it.sShowName ?: it.sName ?: it.nick + if (it.dwFlag == 1L) { + group.botPermission = MemberPermission.ADMINISTRATOR + } } } size += data.members.size diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Member.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Member.kt index 43e6bdeb1..b522a910e 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Member.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Member.kt @@ -21,6 +21,11 @@ interface Member : QQ, Contact { */ val permission: MemberPermission + /** + * + */ + var groupCard: String + /** * 禁言 * @@ -37,6 +42,7 @@ interface Member : QQ, Contact { * 解除禁言 */ suspend fun unmute(): Boolean + } @ExperimentalTime diff --git a/mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt b/mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt index f47ad9ae7..a661934cf 100644 --- a/mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt +++ b/mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt @@ -2,7 +2,6 @@ package demo.gentleman import com.alibaba.fastjson.JSON import kotlinx.coroutines.* -import kotlinx.serialization.json.JsonObject import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.uploadAsImage @@ -18,21 +17,24 @@ class GentleImage { val seImage: Deferred by lazy { getImage(1) } fun getImage(r18: Int): Deferred { - GlobalScope.async { + return GlobalScope.async { withTimeoutOrNull(5 * 1000) { withContext(Dispatchers.IO) { - val result = JSON.parseObject( - Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18").ignoreContentType(true).timeout(10_0000).get().body().text(), + val result = + JSON.parseObject( + Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18").ignoreContentType(true).timeout( + 10_0000 + ).get().body().text() ) - var url = ""; - var pid = ""; + val url: String + val pid: String with(result.getJSONArray("data").getJSONObject(0)) { url = this.getString("url") pid = this.getString("pid") } - val image = Jsoup + Jsoup .connect(url) .followRedirects(true) .timeout(180_000) @@ -41,12 +43,9 @@ class GentleImage { .referrer("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=$pid") .ignoreHttpErrors(true) .maxBodySize(10000000) - .execute() - - if (image.statusCode() != 200) error("Failed to download image") + .execute().also { check(it.statusCode() == 200) { "Failed to download image" } } } - image.bodyStream().uploadAsImage(contact) ?: error("Unable to upload image") - } + }?.bodyStream()?.uploadAsImage(contact) ?: error("Unable to download image") } } }