Blocking bridges for contacts, MessageReceipt and Bot

This commit is contained in:
Him188 2020-12-05 16:55:57 +08:00
parent db100d336f
commit 970f51fbe1
9 changed files with 67 additions and 67 deletions

View File

@ -15,6 +15,7 @@
package net.mamoe.mirai package net.mamoe.mirai
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
import net.mamoe.mirai.message.action.BotNudge import net.mamoe.mirai.message.action.BotNudge
import net.mamoe.mirai.message.action.MemberNudge import net.mamoe.mirai.message.action.MemberNudge
@ -165,7 +166,7 @@ public abstract class Bot internal constructor(
* @throws LoginFailedException 正常登录失败时抛出 * @throws LoginFailedException 正常登录失败时抛出
* @see alsoLogin `.apply { login() }` 捷径 * @see alsoLogin `.apply { login() }` 捷径
*/ */
@JvmSynthetic @JvmBlockingBridge
public abstract suspend fun login() public abstract suspend fun login()
/** /**

View File

@ -13,12 +13,13 @@
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.* import net.mamoe.mirai.*
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageReceipt.Companion.quote
import net.mamoe.mirai.message.MessageReceipt.Companion.recall
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.quote
import net.mamoe.mirai.message.recall
import net.mamoe.mirai.utils.ExternalImage import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.OverFileSizeMaxException import net.mamoe.mirai.utils.OverFileSizeMaxException
import net.mamoe.mirai.utils.WeakRefProperty import net.mamoe.mirai.utils.WeakRefProperty
@ -57,7 +58,7 @@ public interface Contact : ContactOrBot, CoroutineScope {
* *
* @return 消息回执. [引用回复][MessageReceipt.quote]仅群聊 [撤回][MessageReceipt.recall] 这条消息. * @return 消息回执. [引用回复][MessageReceipt.quote]仅群聊 [撤回][MessageReceipt.recall] 这条消息.
*/ */
@JvmSynthetic @JvmBlockingBridge
public suspend fun sendMessage(message: Message): MessageReceipt<Contact> public suspend fun sendMessage(message: Message): MessageReceipt<Contact>
/** /**
@ -71,7 +72,7 @@ public interface Contact : ContactOrBot, CoroutineScope {
* @throws EventCancelledException 当发送消息事件被取消时抛出 * @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时抛出. (最大大小约为 20 MB, mirai 限制的大小为 30 MB) * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时抛出. (最大大小约为 20 MB, mirai 限制的大小为 30 MB)
*/ */
@JvmSynthetic @JvmBlockingBridge
public suspend fun uploadImage(image: ExternalImage): Image public suspend fun uploadImage(image: ExternalImage): Image
/** /**

View File

@ -12,16 +12,17 @@
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.events.EventCancelledException import net.mamoe.mirai.event.events.EventCancelledException
import net.mamoe.mirai.event.events.FriendMessagePostSendEvent import net.mamoe.mirai.event.events.FriendMessagePostSendEvent
import net.mamoe.mirai.event.events.FriendMessagePreSendEvent import net.mamoe.mirai.event.events.FriendMessagePreSendEvent
import net.mamoe.mirai.message.FriendMessageEvent import net.mamoe.mirai.message.FriendMessageEvent
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageReceipt.Companion.recall
import net.mamoe.mirai.message.action.FriendNudge import net.mamoe.mirai.message.action.FriendNudge
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.isContentEmpty import net.mamoe.mirai.message.data.isContentEmpty
import net.mamoe.mirai.message.recall
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
/** /**
@ -70,8 +71,8 @@ public interface Friend : User, CoroutineScope {
* *
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall]) * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
*/ */
@JvmSynthetic @JvmBlockingBridge
override suspend fun sendMessage(message: Message): MessageReceipt<Friend> public override suspend fun sendMessage(message: Message): MessageReceipt<Friend>
/** /**
* 创建一个 "戳一戳" 消息 * 创建一个 "戳一戳" 消息

View File

@ -18,11 +18,11 @@ import net.mamoe.mirai.LowLevelApi
import net.mamoe.mirai.data.MemberInfo import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageReceipt.Companion.recall
import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.Voice import net.mamoe.mirai.message.data.Voice
import net.mamoe.mirai.message.data.isContentEmpty import net.mamoe.mirai.message.data.isContentEmpty
import net.mamoe.mirai.message.recall
import net.mamoe.mirai.utils.ExternalImage import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.OverFileSizeMaxException import net.mamoe.mirai.utils.OverFileSizeMaxException
@ -121,7 +121,7 @@ public interface Group : Contact, CoroutineScope {
* @throws IllegalStateException 当机器人为群主时 * @throws IllegalStateException 当机器人为群主时
* @return 退出成功时 true; 已经退出时 false * @return 退出成功时 true; 已经退出时 false
*/ */
@JvmSynthetic @JvmBlockingBridge
public suspend fun quit(): Boolean public suspend fun quit(): Boolean
/** /**
@ -147,7 +147,7 @@ public interface Group : Contact, CoroutineScope {
* *
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall]) * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
*/ */
@JvmSynthetic @JvmBlockingBridge
public override suspend fun sendMessage(message: Message): MessageReceipt<Group> public override suspend fun sendMessage(message: Message): MessageReceipt<Group>
@ -162,7 +162,7 @@ public interface Group : Contact, CoroutineScope {
* @throws EventCancelledException 当发送消息事件被取消 * @throws EventCancelledException 当发送消息事件被取消
* @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 20 MB) * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 20 MB)
*/ */
@JvmSynthetic @JvmBlockingBridge
public override suspend fun uploadImage(image: ExternalImage): Image public override suspend fun uploadImage(image: ExternalImage): Image
/** /**

View File

@ -11,16 +11,17 @@
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.JavaFriendlyAPI import net.mamoe.mirai.JavaFriendlyAPI
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.getFriendOrNull import net.mamoe.mirai.getFriendOrNull
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageReceipt.Companion.recall
import net.mamoe.mirai.message.action.MemberNudge import net.mamoe.mirai.message.action.MemberNudge
import net.mamoe.mirai.message.action.Nudge import net.mamoe.mirai.message.action.Nudge
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.isContentEmpty import net.mamoe.mirai.message.data.isContentEmpty
import net.mamoe.mirai.message.recall
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.WeakRefProperty import net.mamoe.mirai.utils.WeakRefProperty
import kotlin.time.Duration import kotlin.time.Duration
@ -108,7 +109,7 @@ public interface Member : User {
* *
* @throws PermissionDeniedException 无权限修改时抛出 * @throws PermissionDeniedException 无权限修改时抛出
*/ */
@JvmSynthetic @JvmBlockingBridge
public suspend fun mute(durationSeconds: Int) public suspend fun mute(durationSeconds: Int)
/** /**
@ -122,7 +123,7 @@ public interface Member : User {
* *
* @throws PermissionDeniedException 无权限修改时抛出 * @throws PermissionDeniedException 无权限修改时抛出
*/ */
@JvmSynthetic @JvmBlockingBridge
public suspend fun unmute() public suspend fun unmute()
/** /**
@ -133,7 +134,7 @@ public interface Member : User {
* @see MemberLeaveEvent.Kick 成员被踢出事件. * @see MemberLeaveEvent.Kick 成员被踢出事件.
* @throws PermissionDeniedException 无权限修改时 * @throws PermissionDeniedException 无权限修改时
*/ */
@JvmSynthetic @JvmBlockingBridge
public suspend fun kick(message: String = "") public suspend fun kick(message: String = "")
/** /**
@ -155,7 +156,7 @@ public interface Member : User {
* *
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall]) * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
*/ */
@JvmSynthetic @JvmBlockingBridge
public override suspend fun sendMessage(message: Message): MessageReceipt<Member> public override suspend fun sendMessage(message: Message): MessageReceipt<Member>
/** /**

View File

@ -12,15 +12,16 @@
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageReceipt.Companion.recall
import net.mamoe.mirai.message.action.FriendNudge import net.mamoe.mirai.message.action.FriendNudge
import net.mamoe.mirai.message.action.Nudge import net.mamoe.mirai.message.action.Nudge
import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.isContentEmpty import net.mamoe.mirai.message.data.isContentEmpty
import net.mamoe.mirai.message.recall
import net.mamoe.mirai.utils.ExternalImage import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.OverFileSizeMaxException import net.mamoe.mirai.utils.OverFileSizeMaxException
@ -78,7 +79,7 @@ public interface User : Contact, CoroutineScope {
* *
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall]) * @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
*/ */
@JvmSynthetic @JvmBlockingBridge
public override suspend fun sendMessage(message: Message): MessageReceipt<User> public override suspend fun sendMessage(message: Message): MessageReceipt<User>
/** /**
@ -100,7 +101,7 @@ public interface User : Contact, CoroutineScope {
* @throws EventCancelledException 当发送消息事件被取消 * @throws EventCancelledException 当发送消息事件被取消
* @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 20 MB) * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 20 MB)
*/ */
@JvmSynthetic @JvmBlockingBridge
public override suspend fun uploadImage(image: ExternalImage): Image public override suspend fun uploadImage(image: ExternalImage): Image
} }

View File

@ -11,9 +11,12 @@
package net.mamoe.mirai.message package net.mamoe.mirai.message
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.IMirai import net.mamoe.mirai.IMirai
import net.mamoe.mirai.Mirai import net.mamoe.mirai.Mirai
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
import net.mamoe.mirai.message.MessageReceipt.Companion.quote
import net.mamoe.mirai.message.MessageReceipt.Companion.quoteReply
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
@ -55,61 +58,59 @@ public open class MessageReceipt<out C : Contact> @MiraiExperimentalApi("The con
* 是否为发送给群的消息的回执 * 是否为发送给群的消息的回执
*/ */
public val isToGroup: Boolean get() = target is Group public val isToGroup: Boolean get() = target is Group
public companion object {
/**
* 撤回这条消息. [recallMessage] [recallIn] 只能被调用一次.
*
* @see IMirai.recallMessage
* @throws IllegalStateException 当此消息已经被撤回或正计划撤回时
*/
@JvmBlockingBridge
public suspend inline fun MessageReceipt<*>.recall() {
return Mirai.recallMessage(target.bot, source)
}
/**
* 引用这条消息.
* @see MessageChain.quote 引用一条消息
*/
public inline fun MessageReceipt<*>.quote(): QuoteReply = this.source.quote()
/**
* 引用这条消息并回复.
* @see MessageChain.quote 引用一条消息
*/
@JvmBlockingBridge
public suspend inline fun <C : Contact> MessageReceipt<C>.quoteReply(message: Message): MessageReceipt<C> {
@Suppress("UNCHECKED_CAST")
return target.sendMessage(this.quote() + message) as MessageReceipt<C>
}
/**
* 引用这条消息并回复.
* @see MessageChain.quote 引用一条消息
*/
@JvmBlockingBridge
public suspend inline fun <C : Contact> MessageReceipt<C>.quoteReply(message: String): MessageReceipt<C> {
return this.quoteReply(PlainText(message))
}
}
} }
/**
* 撤回这条消息. [recallMessage] [recallIn] 只能被调用一次.
*
* @see IMirai.recallMessage
* @throws IllegalStateException 当此消息已经被撤回或正计划撤回时
*/
public suspend inline fun MessageReceipt<*>.recall() {
return Mirai.recallMessage(target.bot, source)
}
/**
* 引用这条消息.
* @see MessageChain.quote 引用一条消息
*/
@JvmSynthetic
public inline fun MessageReceipt<*>.quote(): QuoteReply = this.source.quote()
/**
* 引用这条消息并回复.
* @see MessageChain.quote 引用一条消息
*/
@JvmSynthetic
public suspend inline fun <C : Contact> MessageReceipt<C>.quoteReply(message: Message): MessageReceipt<C> {
@Suppress("UNCHECKED_CAST")
return target.sendMessage(this.quote() + message) as MessageReceipt<C>
}
/**
* 引用这条消息并回复.
* @see MessageChain.quote 引用一条消息
*/
@JvmSynthetic
public suspend inline fun <C : Contact> MessageReceipt<C>.quoteReply(message: String): MessageReceipt<C> {
return this.quoteReply(PlainText(message))
}
/** /**
* 获取源消息 [MessageSource.ids] * 获取源消息 [MessageSource.ids]
* *
* @see MessageSource.ids * @see MessageSource.ids
*/ */
@get:JvmSynthetic
public inline val MessageReceipt<*>.sourceIds: IntArray public inline val MessageReceipt<*>.sourceIds: IntArray
get() = this.source.ids get() = this.source.ids
/** /**
* 获取源消息 [MessageSource.internalIds] * 获取源消息 [MessageSource.internalIds]
* *
* @see MessageSource.ids * @see MessageSource.ids
*/ */
@get:JvmSynthetic
public inline val MessageReceipt<*>.sourceInternalIds: IntArray public inline val MessageReceipt<*>.sourceInternalIds: IntArray
get() = this.source.internalIds get() = this.source.internalIds
@ -118,7 +119,5 @@ public inline val MessageReceipt<*>.sourceInternalIds: IntArray
* *
* @see MessageSource.time * @see MessageSource.time
*/ */
@get:JvmSynthetic
public inline val MessageReceipt<*>.sourceTime: Int public inline val MessageReceipt<*>.sourceTime: Int
get() = this.source.time get() = this.source.time

View File

@ -93,7 +93,6 @@ internal class FriendImpl(
override val remark: String override val remark: String
get() = friendInfo.remark get() = friendInfo.remark
@JvmSynthetic
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
override suspend fun sendMessage(message: Message): MessageReceipt<Friend> { override suspend fun sendMessage(message: Message): MessageReceipt<Friend> {
require(message.isContentNotEmpty()) { "message is empty" } require(message.isContentNotEmpty()) { "message is empty" }
@ -108,8 +107,7 @@ internal class FriendImpl(
override fun toString(): String = "Friend($id)" override fun toString(): String = "Friend($id)"
@JvmSynthetic @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
override suspend fun uploadImage(image: ExternalImage): Image = try { override suspend fun uploadImage(image: ExternalImage): Image = try {
if (image.input is net.mamoe.mirai.utils.internal.DeferredReusableInput) { if (image.input is net.mamoe.mirai.utils.internal.DeferredReusableInput) {
image.input.init(bot.configuration.fileCacheStrategy) image.input.init(bot.configuration.fileCacheStrategy)

View File

@ -268,7 +268,6 @@ internal class GroupImpl(
return members.firstOrNull { it.id == id } return members.firstOrNull { it.id == id }
} }
@JvmSynthetic
override suspend fun sendMessage(message: Message): MessageReceipt<Group> { override suspend fun sendMessage(message: Message): MessageReceipt<Group> {
require(message.isContentNotEmpty()) { "message is empty" } require(message.isContentNotEmpty()) { "message is empty" }
check(!isBotMuted) { throw BotIsBeingMutedException(this) } check(!isBotMuted) { throw BotIsBeingMutedException(this) }
@ -381,7 +380,6 @@ internal class GroupImpl(
@Suppress("DEPRECATION", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") @Suppress("DEPRECATION", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@OptIn(ExperimentalTime::class) @OptIn(ExperimentalTime::class)
@JvmSynthetic
override suspend fun uploadImage(image: ExternalImage): Image = try { override suspend fun uploadImage(image: ExternalImage): Image = try {
if (image.input is net.mamoe.mirai.utils.internal.DeferredReusableInput) { if (image.input is net.mamoe.mirai.utils.internal.DeferredReusableInput) {
image.input.init(bot.configuration.fileCacheStrategy) image.input.init(bot.configuration.fileCacheStrategy)