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 dfadae4fe..637273e35 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 @@ -21,6 +21,7 @@ import kotlinx.serialization.MissingFieldException import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration import kotlinx.serialization.json.int +import kotlinx.serialization.json.long import net.mamoe.mirai.LowLevelAPI import net.mamoe.mirai.contact.* import net.mamoe.mirai.data.* @@ -600,9 +601,9 @@ internal class GroupImpl( @MiraiExperimentalAPI - override suspend fun sendAnnouncement(announcement: GroupAnnouncement) { + override suspend fun sendAnnouncement(announcement: GroupAnnouncement): String { val json = Json(JsonConfiguration.Stable) - bot.network.launch { + val rep = bot.network.async { HttpClient().post { url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice") body = MultiPartFormDataContent(formData { @@ -610,7 +611,13 @@ internal class GroupImpl( append("bkn", getBkn()) append("text", announcement.msg.text) append("pinned", announcement.pinned) - append("settings", json.stringify(GroupAnnouncementSettings.serializer(), announcement.settings?:GroupAnnouncementSettings())) + append( + "settings", + json.stringify( + GroupAnnouncementSettings.serializer(), + announcement.settings ?: GroupAnnouncementSettings() + ) + ) append("format", "json") }) headers { @@ -622,13 +629,41 @@ internal class GroupImpl( " p_skey=${bot.client.wLoginSigInfo.psKeyMap["qun.qq.com"]?.data?.encodeToString()}; " ) } - }.also { - val jsonObj = json.parseJson(it) - if (jsonObj.jsonObject["ec"]?.int ?:1 != 0){ - throw IllegalStateException("Send Announcement fail group:$id msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}") + } + } + val jsonObj = json.parseJson(rep.await()) + return jsonObj.jsonObject["new_fid"]?.primitive?.content + ?: throw throw IllegalStateException("Send Announcement fail group:$id msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}") + } + + @MiraiExperimentalAPI + override suspend fun deleteAnnouncement(fid: String) { + val json = Json(JsonConfiguration.Stable) + val rep = bot.network.async { + HttpClient().post { + url("https://web.qun.qq.com/cgi-bin/announce/del_feed") + body = MultiPartFormDataContent(formData { + append("qid", id) + append("bkn", getBkn()) + append("fid", fid) + append("format", "json") + }) + headers { + append( + "cookie", + "uin=o${bot.selfQQ.id};" + + " skey=${bot.client.wLoginSigInfo.sKey.data.encodeToString()};" + + " p_uin=o${bot.selfQQ.id};" + + " p_skey=${bot.client.wLoginSigInfo.psKeyMap["qun.qq.com"]?.data?.encodeToString()}; " + ) } } } + val data = rep.await() + val jsonObj = json.parseJson(data) + if (jsonObj.jsonObject["ec"]?.int ?: 1 != 0){ + throw throw IllegalStateException("delete Announcement fail group:$id msg:${jsonObj.jsonObject["em"]} fid:$fid") + } } @OptIn(MiraiExperimentalAPI::class) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt index dc6005238..ff7c690de 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt @@ -158,14 +158,23 @@ expect abstract class Group() : Contact, CoroutineScope { * * */ @MiraiExperimentalAPI - abstract suspend fun getAnnouncements(page: Int = 1, amount: Int = 10):GroupAnnouncementList? + abstract suspend fun getAnnouncements(page: Int = 1, amount: Int = 10): GroupAnnouncementList? /** * 发送群公告 * * */ @MiraiExperimentalAPI - abstract suspend fun sendAnnouncement(announcement: GroupAnnouncement) + abstract suspend fun sendAnnouncement(announcement: GroupAnnouncement): String + + + /** + * 删除群公告 + * fid可以通过发送公告的返回值得到或者获取列表得到 + * */ + + @MiraiExperimentalAPI + abstract suspend fun deleteAnnouncement(fid: String) /** * 让机器人退出这个群. 机器人必须为非群主才能退出. 否则将会失败 diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/data/GroupAnnouncement.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/data/GroupAnnouncement.kt index 2dfadf8c5..ac9cc6311 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/data/GroupAnnouncement.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/data/GroupAnnouncement.kt @@ -26,7 +26,8 @@ data class GroupAnnouncement( @SerialName("pubt") val time: Long = 0, @SerialName("read_num") val readNum: Int = 0, @SerialName("is_read") val isRead: Int = 0, - val pinned: Int = 0 + val pinned: Int = 0, + val fid:String? = null //公告的id ) @Serializable diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt index 2f065bab5..8efa42be9 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt @@ -156,12 +156,18 @@ actual abstract class Group : Contact(), CoroutineScope { @MiraiExperimentalAPI actual suspend abstract fun getAnnouncements(page: Int, amount: Int): GroupAnnouncementList? + /** + * 删除群公告 + * fid可以通过发送公告的返回值得到或者获取列表得到 + * */ + @MiraiExperimentalAPI + actual abstract suspend fun deleteAnnouncement(fid: String) /** * 发送群公告 * */ @MiraiExperimentalAPI - actual suspend abstract fun sendAnnouncement(announcement: GroupAnnouncement) + actual suspend abstract fun sendAnnouncement(announcement: GroupAnnouncement):String /** * 检查此 id 的群成员是否存在