diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt index 8137a402e..b712fa29e 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt @@ -39,7 +39,7 @@ interface Contact : CoroutineScope { suspend fun uploadImage(image: ExternalImage): ImageId } -suspend inline fun Contact.sendMessage(message: Message) = sendMessage(message.chain()) +suspend inline fun Contact.sendMessage(message: Message) = sendMessage(message.toChain()) suspend inline fun Contact.sendMessage(plain: String) = sendMessage(plain.singleChain()) 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 96ed916fc..d11c1666f 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 @@ -61,8 +61,6 @@ interface Group : Contact, CoroutineScope/*, Map*/ { // TODO: 2019 /** * 让机器人退出这个群. 机器人必须为非群主才能退出. 否则将会失败 - * - * @see QuitGroupResponse.isSuccess 判断是否成功 */ suspend fun quit(): Boolean diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt index 0387bb327..be7dc9acb 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt @@ -10,7 +10,6 @@ import net.mamoe.mirai.data.ImageLink import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.* -import net.mamoe.mirai.utils.coerceAtLeastOrFail import kotlin.jvm.JvmName /** @@ -50,7 +49,7 @@ abstract class MessagePacketBase(_bot: Bot) : */ suspend inline fun reply(message: MessageChain) = subject.sendMessage(message) - suspend inline fun reply(message: Message) = subject.sendMessage(message.chain()) + suspend inline fun reply(message: Message) = subject.sendMessage(message.toChain()) suspend inline fun reply(plain: String) = subject.sendMessage(plain.toMessage()) @JvmName("reply1") diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt index df7592cff..8b5b49be4 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt @@ -63,7 +63,7 @@ fun MessageChain(initialCapacity: Int): MessageChain = /** * 构造 [MessageChain] - * 若仅提供一个参数, 请考虑使用 [Message.chain] 以优化性能 + * 若仅提供一个参数, 请考虑使用 [Message.toChain] 以优化性能 */ @Suppress("FunctionName") fun MessageChain(vararg messages: Message): MessageChain = @@ -81,12 +81,12 @@ fun MessageChain(messages: Iterable): MessageChain = * 构造单元素的不可修改的 [MessageChain]. 内部类实现为 [SingleMessageChain] * * 参数 [delegate] 不能为 [MessageChain] 的实例, 否则将会抛出异常. - * 使用 [Message.chain] 将帮助提前处理这个问题. + * 使用 [Message.toChain] 将帮助提前处理这个问题. * * @param delegate 所构造的单元素 [MessageChain] 代表的 [Message] * @throws IllegalArgumentException 当 [delegate] 为 [MessageChain] 的实例时 * - * @see Message.chain receiver 模式 + * @see Message.toChain receiver 模式 */ @MiraiExperimentalAPI @UseExperimental(ExperimentalContracts::class) @@ -106,17 +106,13 @@ fun SingleMessageChain(delegate: Message): MessageChain { * 否则将调用 [MessageChain] 构造一个 [MessageChainImpl] */ @Suppress("NOTHING_TO_INLINE") -inline fun Message.chain(): MessageChain = if (this is MessageChain) this else MessageChain( - this -) +inline fun Message.toChain(): MessageChain = if (this is MessageChain) this else MessageChain(this) /** * 构造 [MessageChain] */ @Suppress("unused", "NOTHING_TO_INLINE") -inline fun List.messageChain(): MessageChain = - MessageChain(this) - +inline fun List.toMessageChain(): MessageChain = MessageChain(this) /** * 获取第一个 [M] 类型的 [Message] 实例 @@ -327,6 +323,7 @@ internal inline class MessageChainImpl constructor( * * 在连接时将会把它当做一个普通 [Message] 看待, 但它不能被 [plusAssign] */ +@PublishedApi internal inline class SingleMessageChainImpl( private val delegate: Message ) : Message, MutableList, diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt index 67b784862..72ae01ac0 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt @@ -23,4 +23,4 @@ inline fun String.toMessage(): PlainText = PlainText(this) * @see SingleMessageChainImpl */ @Suppress("NOTHING_TO_INLINE") -inline fun String.singleChain(): MessageChain = this.toMessage().chain() \ No newline at end of file +inline fun String.singleChain(): MessageChain = SingleMessageChainImpl(this.toMessage()) \ No newline at end of file diff --git a/mirai-japt/mirai-japt.postfixTemplates b/mirai-japt/mirai-japt.postfixTemplates index 9df0de9fd..030b8a3d7 100644 --- a/mirai-japt/mirai-japt.postfixTemplates +++ b/mirai-japt/mirai-japt.postfixTemplates @@ -1,4 +1,5 @@ -# Contact - -.blocking : 阻塞式包装 - net.mamoe.mirai.contact.QQ [net.mamoe.mirai.japt.BlockingContacts] -> BlockingQQ. BlockingContacts.createBlocking($var$) +## Contact ## +.blocking : Blocking wrap + net.mamoe.mirai.contact.QQ → net.mamoe.mirai.japt.BlockingContacts.createBlocking($expr$) + net.mamoe.mirai.contact.QQ [net.mamoe.mirai.japt.BlockingContacts] → net.mamoe.mirai.japt.BlockingContacts.createBlocking($expr$) + net.mamoe.mirai.timpc.network.QQImpl [net.mamoe.mirai.japt.BlockingContacts] → net.mamoe.mirai.japt.BlockingContacts.createBlocking($expr$) \ No newline at end of file diff --git a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingBot.java b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingBot.java new file mode 100644 index 000000000..b94d6b2b3 --- /dev/null +++ b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingBot.java @@ -0,0 +1,4 @@ +package net.mamoe.mirai.japt; + +public interface BlockingBot { +} diff --git a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingContact.java b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingContact.java index dcc1585cb..4ea7b98aa 100644 --- a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingContact.java +++ b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingContact.java @@ -1,7 +1,8 @@ package net.mamoe.mirai.japt; import net.mamoe.mirai.Bot; -import net.mamoe.mirai.message.MessageChain; +import net.mamoe.mirai.message.data.Message; +import net.mamoe.mirai.message.data.MessageChain; import org.jetbrains.annotations.NotNull; @SuppressWarnings("unused") @@ -19,8 +20,16 @@ public interface BlockingContact { /** * 向这个对象发送消息. - *

- * 速度太快会被服务器屏蔽(无响应). 在测试中不延迟地发送 6 条消息就会被屏蔽之后的数据包 1 秒左右. */ void sendMessage(@NotNull MessageChain messages); + + /** + * 向这个对象发送消息. + */ + void sendMessage(@NotNull String message); + + /** + * 向这个对象发送消息. + */ + void sendMessage(@NotNull Message message); } diff --git a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingGroup.java b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingGroup.java index 37fdc62b1..2805ad9f7 100644 --- a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingGroup.java +++ b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingGroup.java @@ -1,6 +1,6 @@ package net.mamoe.mirai.japt; -import net.mamoe.mirai.network.protocol.tim.packet.action.GroupInfo; +import net.mamoe.mirai.data.GroupInfo; import java.util.Map; @@ -9,7 +9,7 @@ public interface BlockingGroup extends BlockingContact { /** * 内部 ID. 内部 ID 为 [GroupId] 的映射 */ - Long getInternalId(); + long getInternalId(); /** * 群主 (同步事件更新) diff --git a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingQQ.java b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingQQ.java index e01aa246c..44145d585 100644 --- a/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingQQ.java +++ b/mirai-japt/src/main/java/net/mamoe/mirai/japt/BlockingQQ.java @@ -1,8 +1,8 @@ package net.mamoe.mirai.japt; -import net.mamoe.mirai.contact.data.Profile; -import net.mamoe.mirai.network.protocol.tim.packet.action.FriendNameRemark; -import net.mamoe.mirai.network.protocol.tim.packet.action.PreviousNameList; +import net.mamoe.mirai.data.FriendNameRemark; +import net.mamoe.mirai.data.PreviousNameList; +import net.mamoe.mirai.data.Profile; import org.jetbrains.annotations.NotNull; @SuppressWarnings("unused") diff --git a/mirai-japt/src/main/kotlin/net/mamoe/mirai/japt/BlockingContactsImpl.kt b/mirai-japt/src/main/kotlin/net/mamoe/mirai/japt/BlockingContactsImpl.kt index 550d5a807..b15929731 100644 --- a/mirai-japt/src/main/kotlin/net/mamoe/mirai/japt/BlockingContactsImpl.kt +++ b/mirai-japt/src/main/kotlin/net/mamoe/mirai/japt/BlockingContactsImpl.kt @@ -8,16 +8,24 @@ import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.contact.QQ -import net.mamoe.mirai.contact.data.Profile -import net.mamoe.mirai.message.MessageChain -import net.mamoe.mirai.network.protocol.tim.packet.action.FriendNameRemark -import net.mamoe.mirai.network.protocol.tim.packet.action.GroupInfo -import net.mamoe.mirai.network.protocol.tim.packet.action.PreviousNameList +import net.mamoe.mirai.data.FriendNameRemark +import net.mamoe.mirai.data.GroupInfo +import net.mamoe.mirai.data.PreviousNameList +import net.mamoe.mirai.data.Profile +import net.mamoe.mirai.message.data.Message +import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.toChain +import net.mamoe.mirai.message.data.toMessage +import net.mamoe.mirai.utils.MiraiInternalAPI +import net.mamoe.mirai.utils.toList internal class BlockingQQImpl(private val delegate: QQ) : BlockingQQ { override fun getBot(): Bot = delegate.bot - override fun getId(): Long = delegate.id.toLong() + override fun getId(): Long = delegate.id override fun sendMessage(messages: MessageChain) = runBlocking { delegate.sendMessage(messages) } + override fun sendMessage(message: String) = runBlocking { delegate.sendMessage(message.toMessage().toChain()) } + override fun sendMessage(message: Message) = runBlocking { delegate.sendMessage(message.toChain()) } + override fun queryProfile(): Profile = runBlocking { delegate.queryProfile() } override fun queryPreviousNameList(): PreviousNameList = runBlocking { delegate.queryPreviousNameList() } override fun queryRemark(): FriendNameRemark = runBlocking { delegate.queryRemark() } @@ -25,17 +33,22 @@ internal class BlockingQQImpl(private val delegate: QQ) : BlockingQQ { internal class BlockingGroupImpl(private val delegate: Group) : BlockingGroup { override fun sendMessage(messages: MessageChain) = runBlocking { delegate.sendMessage(messages) } + override fun sendMessage(message: String) = runBlocking { delegate.sendMessage(message.toMessage().toChain()) } + override fun sendMessage(message: Message) = runBlocking { delegate.sendMessage(message.toChain()) } override fun getOwner(): BlockingMember = delegate.owner.blocking() override fun getName(): String = delegate.name - override fun getId(): Long = delegate.id.toLong() + override fun getId(): Long = delegate.id override fun updateGroupInfo(): GroupInfo = runBlocking { delegate.updateGroupInfo() } override fun toFullString(): String = delegate.toFullString() - override fun getMember(id: Long): BlockingMember = delegate.getMember(id.toUInt()).blocking() + override fun getMember(id: Long): BlockingMember = delegate.getMember(id).blocking() override fun getBot(): Bot = delegate.bot override fun getAnnouncement(): String = delegate.announcement - override fun getMembers(): MutableMap = delegate.members.mapKeys { it.key.toLong() }.mapValues { it.value.blocking() }.toMutableMap() - override fun getInternalId(): Long = delegate.internalId.value.toLong() - override fun quit(): Boolean = runBlocking { delegate.quit().isSuccess } + @UseExperimental(MiraiInternalAPI::class) + override fun getMembers(): Map = + delegate.members.delegate.toList().associateBy { it.id }.mapValues { it.value.blocking() } + + override fun getInternalId(): Long = delegate.internalId.value + override fun quit(): Boolean = runBlocking { delegate.quit() } } internal class BlockingMemberImpl(private val delegate: Member) : BlockingMember {