mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-18 09:32:23 +08:00
parent
6667d94277
commit
32e01f4a60
@ -58,12 +58,6 @@ public abstract class Bot internal constructor(
|
|||||||
@Suppress("ObjectPropertyName")
|
@Suppress("ObjectPropertyName")
|
||||||
internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList()
|
internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList()
|
||||||
|
|
||||||
@PlannedRemoval("2.0.0")
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@JvmStatic
|
|
||||||
public val instances: List<WeakRef<Bot>>
|
|
||||||
get() = _instances.toList()
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 复制一份此时的 [Bot] 实例列表.
|
* 复制一份此时的 [Bot] 实例列表.
|
||||||
*/
|
*/
|
||||||
|
@ -1,245 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
|
||||||
*
|
|
||||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
|
||||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
|
||||||
*
|
|
||||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
|
||||||
*/
|
|
||||||
|
|
||||||
@file:JvmMultifileClass
|
|
||||||
@file:JvmName("SubscribeMessagesKt")
|
|
||||||
@file:Suppress(
|
|
||||||
"EXPERIMENTAL_API_USAGE",
|
|
||||||
"MemberVisibilityCanBePrivate",
|
|
||||||
"unused",
|
|
||||||
"INVISIBLE_MEMBER",
|
|
||||||
"INVISIBLE_REFERENCE"
|
|
||||||
)
|
|
||||||
|
|
||||||
package net.mamoe.mirai.event
|
|
||||||
|
|
||||||
import kotlinx.coroutines.CancellationException
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.channels.ReceiveChannel
|
|
||||||
import net.mamoe.mirai.Bot
|
|
||||||
import net.mamoe.mirai.event.events.BotEvent
|
|
||||||
import net.mamoe.mirai.utils.PlannedRemoval
|
|
||||||
import kotlin.contracts.InvocationKind
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
import kotlin.coroutines.CoroutineContext
|
|
||||||
import kotlin.coroutines.EmptyCoroutineContext
|
|
||||||
import kotlin.jvm.JvmMultifileClass
|
|
||||||
import kotlin.jvm.JvmName
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
////
|
|
||||||
//// 此文件存放所有 `subscribeMessages` 已弃用的函数.
|
|
||||||
////
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
@PlannedRemoval("1.3.0")
|
|
||||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
|
||||||
@Deprecated(
|
|
||||||
"Deprecated for better Coroutine life cycle management. Please filter bot instance on your own.",
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
public fun <R> Bot.subscribeMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
priority: Listener.EventPriority = EventPriority.MONITOR,
|
|
||||||
listeners: MessagePacketSubscribersBuilder.() -> R
|
|
||||||
): R {
|
|
||||||
contract {
|
|
||||||
callsInPlace(listeners, InvocationKind.EXACTLY_ONCE)
|
|
||||||
}
|
|
||||||
return MessagePacketSubscribersBuilder(Unit) { filter, listener ->
|
|
||||||
this.subscribeAlways(coroutineContext, concurrencyKind, priority) {
|
|
||||||
val toString = this.message.contentToString()
|
|
||||||
if (filter(this, toString))
|
|
||||||
listener(this, toString)
|
|
||||||
}
|
|
||||||
}.run(listeners)
|
|
||||||
}
|
|
||||||
|
|
||||||
@PlannedRemoval("1.3.0")
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
|
||||||
@Deprecated(
|
|
||||||
"Deprecated for better Coroutine life cycle management. Please filter bot instance on your own.",
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
public fun <R> Bot.subscribeGroupMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
priority: Listener.EventPriority = EventPriority.MONITOR,
|
|
||||||
listeners: GroupMessageSubscribersBuilder.() -> R
|
|
||||||
): R {
|
|
||||||
contract {
|
|
||||||
callsInPlace(listeners, InvocationKind.EXACTLY_ONCE)
|
|
||||||
}
|
|
||||||
return GroupMessageSubscribersBuilder(Unit) { filter, listener ->
|
|
||||||
this.subscribeAlways(coroutineContext, concurrencyKind, priority) {
|
|
||||||
val toString = this.message.contentToString()
|
|
||||||
if (filter(this, toString))
|
|
||||||
listener(this, toString)
|
|
||||||
}
|
|
||||||
}.run(listeners)
|
|
||||||
}
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@PlannedRemoval("1.3.0")
|
|
||||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
|
||||||
@Deprecated(
|
|
||||||
"Deprecated for better Coroutine life cycle management. Please filter bot instance on your own.",
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
public fun <R> Bot.subscribeFriendMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
priority: Listener.EventPriority = EventPriority.MONITOR,
|
|
||||||
listeners: FriendMessageSubscribersBuilder.() -> R
|
|
||||||
): R {
|
|
||||||
contract {
|
|
||||||
callsInPlace(listeners, InvocationKind.EXACTLY_ONCE)
|
|
||||||
}
|
|
||||||
return FriendMessageSubscribersBuilder(Unit) { filter, listener ->
|
|
||||||
this.subscribeAlways(coroutineContext, concurrencyKind, priority) {
|
|
||||||
val toString = this.message.contentToString()
|
|
||||||
if (filter(this, toString))
|
|
||||||
listener(this, toString)
|
|
||||||
}
|
|
||||||
}.run(listeners)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@PlannedRemoval("1.3.0")
|
|
||||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
|
||||||
@Deprecated(
|
|
||||||
"Deprecated for better Coroutine life cycle management. Please filter bot instance on your own.",
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
public fun <R> Bot.subscribeTempMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
priority: Listener.EventPriority = EventPriority.MONITOR,
|
|
||||||
listeners: TempMessageSubscribersBuilder.() -> R
|
|
||||||
): R {
|
|
||||||
contract {
|
|
||||||
callsInPlace(listeners, InvocationKind.EXACTLY_ONCE)
|
|
||||||
}
|
|
||||||
return TempMessageSubscribersBuilder(Unit) { filter, listener ->
|
|
||||||
this.subscribeAlways(coroutineContext, concurrencyKind, priority) {
|
|
||||||
val toString = this.message.contentToString()
|
|
||||||
if (filter(this, toString))
|
|
||||||
listener(this, toString)
|
|
||||||
}
|
|
||||||
}.run(listeners)
|
|
||||||
}
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@PlannedRemoval("1.3.0")
|
|
||||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
|
||||||
@Deprecated(
|
|
||||||
"Deprecated for better Coroutine life cycle management. Please filter bot instance on your own.",
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
public inline fun <reified E : BotEvent> Bot.incoming(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
priority: Listener.EventPriority = EventPriority.MONITOR,
|
|
||||||
capacity: Int = Channel.UNLIMITED
|
|
||||||
): ReceiveChannel<E> {
|
|
||||||
return Channel<E>(capacity).apply {
|
|
||||||
val listener = this@incoming.subscribeAlways<E>(coroutineContext, concurrencyKind, priority) {
|
|
||||||
send(this)
|
|
||||||
}
|
|
||||||
this.invokeOnClose {
|
|
||||||
listener.cancel(CancellationException("ReceiveChannel closed", it))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> CoroutineScope.subscribeMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: MessagePacketSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> CoroutineScope.subscribeGroupMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: GroupMessageSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeGroupMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> CoroutineScope.subscribeFriendMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: FriendMessageSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeFriendMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> CoroutineScope.subscribeTempMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: TempMessageSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeTempMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> Bot.subscribeMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: MessagePacketSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> Bot.subscribeGroupMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: GroupMessageSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeGroupMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> Bot.subscribeFriendMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: FriendMessageSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeFriendMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
||||||
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
public fun <R> Bot.subscribeTempMessages(
|
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
|
||||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
|
||||||
listeners: TempMessageSubscribersBuilder.() -> R
|
|
||||||
): R = this.subscribeTempMessages(coroutineContext, concurrencyKind, EventPriority.MONITOR, listeners)
|
|
@ -12,14 +12,16 @@
|
|||||||
package net.mamoe.mirai.message
|
package net.mamoe.mirai.message
|
||||||
|
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
|
import net.mamoe.mirai.contact.Contact
|
||||||
import net.mamoe.mirai.contact.Friend
|
import net.mamoe.mirai.contact.Friend
|
||||||
|
import net.mamoe.mirai.contact.User
|
||||||
|
import net.mamoe.mirai.event.AbstractEvent
|
||||||
import net.mamoe.mirai.event.BroadcastControllable
|
import net.mamoe.mirai.event.BroadcastControllable
|
||||||
import net.mamoe.mirai.event.events.FriendEvent
|
import net.mamoe.mirai.event.events.FriendEvent
|
||||||
import net.mamoe.mirai.message.data.MessageChain
|
import net.mamoe.mirai.message.data.MessageChain
|
||||||
import net.mamoe.mirai.message.data.MessageSource
|
import net.mamoe.mirai.message.data.MessageSource
|
||||||
import net.mamoe.mirai.message.data.OnlineMessageSource
|
import net.mamoe.mirai.message.data.OnlineMessageSource
|
||||||
import net.mamoe.mirai.message.data.source
|
import net.mamoe.mirai.message.data.source
|
||||||
import net.mamoe.mirai.utils.PlannedRemoval
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 机器人收到的好友消息的事件
|
* 机器人收到的好友消息的事件
|
||||||
@ -30,15 +32,14 @@ public class FriendMessageEvent constructor(
|
|||||||
public override val sender: Friend,
|
public override val sender: Friend,
|
||||||
public override val message: MessageChain,
|
public override val message: MessageChain,
|
||||||
public override val time: Int
|
public override val time: Int
|
||||||
) : @PlannedRemoval("1.2.0") FriendMessage(), BroadcastControllable, FriendEvent {
|
) : AbstractEvent(), MessageEvent, MessageEventExtensions<User, Contact>, BroadcastControllable, FriendEvent {
|
||||||
init {
|
init {
|
||||||
val source =
|
val source =
|
||||||
message[MessageSource] ?: throw IllegalArgumentException("Cannot find MessageSource from message")
|
message[MessageSource] ?: throw IllegalArgumentException("Cannot find MessageSource from message")
|
||||||
check(source is OnlineMessageSource.Incoming.FromFriend) { "source provided to a FriendMessage must be an instance of OnlineMessageSource.Incoming.FromFriend" }
|
check(source is OnlineMessageSource.Incoming.FromFriend) { "source provided to a FriendMessage must be an instance of OnlineMessageSource.Incoming.FromFriend" }
|
||||||
}
|
}
|
||||||
|
|
||||||
public override val friend: Friend
|
public override val friend: Friend get() = sender
|
||||||
get() = sender
|
|
||||||
public override val bot: Bot get() = super.bot
|
public override val bot: Bot get() = super.bot
|
||||||
public override val subject: Friend get() = sender
|
public override val subject: Friend get() = sender
|
||||||
public override val senderName: String get() = sender.nick
|
public override val senderName: String get() = sender.nick
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Mamoe Technologies and contributors.
|
||||||
|
*
|
||||||
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
|
*
|
||||||
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.mamoe.mirai.message
|
||||||
|
|
||||||
|
import net.mamoe.mirai.contact.Group
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 来自一个可以知道其 [Group] 的用户消息
|
||||||
|
*
|
||||||
|
* @see FriendMessageEvent
|
||||||
|
* @see TempMessageEvent
|
||||||
|
*/
|
||||||
|
public interface GroupAwareMessageEvent : MessageEvent {
|
||||||
|
public val group: Group
|
||||||
|
}
|
@ -15,10 +15,10 @@ import net.mamoe.mirai.Bot
|
|||||||
import net.mamoe.mirai.contact.Group
|
import net.mamoe.mirai.contact.Group
|
||||||
import net.mamoe.mirai.contact.Member
|
import net.mamoe.mirai.contact.Member
|
||||||
import net.mamoe.mirai.contact.MemberPermission
|
import net.mamoe.mirai.contact.MemberPermission
|
||||||
|
import net.mamoe.mirai.event.AbstractEvent
|
||||||
import net.mamoe.mirai.event.Event
|
import net.mamoe.mirai.event.Event
|
||||||
import net.mamoe.mirai.event.events.GroupEvent
|
import net.mamoe.mirai.event.events.GroupEvent
|
||||||
import net.mamoe.mirai.message.data.*
|
import net.mamoe.mirai.message.data.*
|
||||||
import net.mamoe.mirai.utils.PlannedRemoval
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 机器人收到的群消息的事件
|
* 机器人收到的群消息的事件
|
||||||
@ -34,7 +34,7 @@ public class GroupMessageEvent(
|
|||||||
public override val sender: Member,
|
public override val sender: Member,
|
||||||
public override val message: MessageChain,
|
public override val message: MessageChain,
|
||||||
public override val time: Int
|
public override val time: Int
|
||||||
) : @PlannedRemoval("1.2.0") GroupMessage(), Event, GroupEvent {
|
) : AbstractEvent(), GroupAwareMessageEvent, MessageEvent, Event, GroupEvent {
|
||||||
init {
|
init {
|
||||||
val source = message[MessageSource] ?: error("Cannot find MessageSource from message")
|
val source = message[MessageSource] ?: error("Cannot find MessageSource from message")
|
||||||
check(source is OnlineMessageSource.Incoming.FromGroup) { "source provided to a GroupMessage must be an instance of OnlineMessageSource.Incoming.FromGroup" }
|
check(source is OnlineMessageSource.Incoming.FromGroup) { "source provided to a GroupMessage must be an instance of OnlineMessageSource.Incoming.FromGroup" }
|
||||||
@ -42,9 +42,7 @@ public class GroupMessageEvent(
|
|||||||
|
|
||||||
public override val group: Group get() = sender.group
|
public override val group: Group get() = sender.group
|
||||||
public override val bot: Bot get() = sender.bot
|
public override val bot: Bot get() = sender.bot
|
||||||
|
|
||||||
public override val subject: Group get() = group
|
public override val subject: Group get() = group
|
||||||
|
|
||||||
public override val source: OnlineMessageSource.Incoming.FromGroup get() = message.source as OnlineMessageSource.Incoming.FromGroup
|
public override val source: OnlineMessageSource.Incoming.FromGroup get() = message.source as OnlineMessageSource.Incoming.FromGroup
|
||||||
|
|
||||||
public inline fun At.asMember(): Member = group[this.target]
|
public inline fun At.asMember(): Member = group[this.target]
|
||||||
|
@ -21,8 +21,10 @@ package net.mamoe.mirai.message
|
|||||||
import event.*
|
import event.*
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
import net.mamoe.mirai.contact.*
|
import net.mamoe.mirai.contact.*
|
||||||
|
import net.mamoe.mirai.event.Event
|
||||||
import net.mamoe.mirai.event.events.BotEvent
|
import net.mamoe.mirai.event.events.BotEvent
|
||||||
import net.mamoe.mirai.event.subscribe
|
import net.mamoe.mirai.event.subscribe
|
||||||
|
import net.mamoe.mirai.internal.network.Packet
|
||||||
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.ExternalImage
|
import net.mamoe.mirai.utils.ExternalImage
|
||||||
@ -45,13 +47,12 @@ import java.io.InputStream
|
|||||||
* @see isContextIdenticalWith 判断语境是否相同
|
* @see isContextIdenticalWith 判断语境是否相同
|
||||||
*/
|
*/
|
||||||
@Suppress("DEPRECATION_ERROR")
|
@Suppress("DEPRECATION_ERROR")
|
||||||
public abstract class MessageEvent : ContactMessage(),
|
public interface MessageEvent : Event, Packet, BotEvent, MessageEventExtensions<User, Contact> {
|
||||||
BotEvent, MessageEventExtensions<User, Contact> {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 与这个消息事件相关的 [Bot]
|
* 与这个消息事件相关的 [Bot]
|
||||||
*/
|
*/
|
||||||
public abstract override val bot: Bot
|
public override val bot: Bot
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息事件主体.
|
* 消息事件主体.
|
||||||
@ -62,19 +63,19 @@ public abstract class MessageEvent : ContactMessage(),
|
|||||||
*
|
*
|
||||||
* 在回复消息时, 可通过 [subject] 作为回复对象
|
* 在回复消息时, 可通过 [subject] 作为回复对象
|
||||||
*/
|
*/
|
||||||
public abstract override val subject: Contact
|
public override val subject: Contact
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送人.
|
* 发送人.
|
||||||
*
|
*
|
||||||
* 在好友消息时为 [Friend] 的实例, 在群消息时为 [Member] 的实例
|
* 在好友消息时为 [Friend] 的实例, 在群消息时为 [Member] 的实例
|
||||||
*/
|
*/
|
||||||
public abstract override val sender: User
|
public override val sender: User
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送人名称
|
* 发送人名称
|
||||||
*/
|
*/
|
||||||
public abstract override val senderName: String
|
public val senderName: String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息内容.
|
* 消息内容.
|
||||||
@ -82,19 +83,18 @@ public abstract class MessageEvent : ContactMessage(),
|
|||||||
* 第一个元素一定为 [MessageSource], 存储此消息的发送人, 发送时间, 收信人, 消息 ids 等数据.
|
* 第一个元素一定为 [MessageSource], 存储此消息的发送人, 发送时间, 收信人, 消息 ids 等数据.
|
||||||
* 随后的元素为拥有顺序的真实消息内容.
|
* 随后的元素为拥有顺序的真实消息内容.
|
||||||
*/
|
*/
|
||||||
public abstract override val message: MessageChain
|
public override val message: MessageChain
|
||||||
|
|
||||||
/** 消息发送时间 (由服务器提供, 可能与本地有时差) */
|
/** 消息发送时间 (由服务器提供, 可能与本地有时差) */
|
||||||
public abstract override val time: Int
|
public val time: Int
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息源. 来自 [message] 的第一个元素,
|
* 消息源. 来自 [message] 的第一个元素,
|
||||||
*/
|
*/
|
||||||
public override val source: OnlineMessageSource.Incoming get() = message.source as OnlineMessageSource.Incoming
|
public val source: OnlineMessageSource.Incoming get() = message.source as OnlineMessageSource.Incoming
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 消息事件的扩展函数 */
|
/** 消息事件的扩展函数 */
|
||||||
@Suppress("EXPOSED_SUPER_INTERFACE") // Functions are visible
|
|
||||||
public interface MessageEventExtensions<out TSender : User, out TSubject : Contact> :
|
public interface MessageEventExtensions<out TSender : User, out TSubject : Contact> :
|
||||||
MessageEventPlatformExtensions<TSender, TSubject> {
|
MessageEventPlatformExtensions<TSender, TSubject> {
|
||||||
|
|
||||||
@ -165,55 +165,55 @@ public interface MessageEventExtensions<out TSender : User, out TSubject : Conta
|
|||||||
* 消息事件在 JVM 平台的扩展
|
* 消息事件在 JVM 平台的扩展
|
||||||
* @see MessageEventExtensions
|
* @see MessageEventExtensions
|
||||||
*/
|
*/
|
||||||
internal interface MessageEventPlatformExtensions<out TSender : User, out TSubject : Contact> {
|
public interface MessageEventPlatformExtensions<out TSender : User, out TSubject : Contact> {
|
||||||
val subject: TSubject
|
public val subject: TSubject
|
||||||
val sender: TSender
|
public val sender: TSender
|
||||||
val message: MessageChain
|
public val message: MessageChain
|
||||||
val bot: Bot
|
public val bot: Bot
|
||||||
|
|
||||||
// region 上传图片
|
// region 上传图片
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun uploadImage(image: BufferedImage): Image = subject.uploadImage(image)
|
public suspend fun uploadImage(image: BufferedImage): Image = subject.uploadImage(image)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun uploadImage(image: InputStream): Image = subject.uploadImage(image)
|
public suspend fun uploadImage(image: InputStream): Image = subject.uploadImage(image)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun uploadImage(image: File): Image = subject.uploadImage(image)
|
public suspend fun uploadImage(image: File): Image = subject.uploadImage(image)
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region 发送图片
|
// region 发送图片
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun sendImage(image: BufferedImage): MessageReceipt<TSubject> = subject.sendImage(image)
|
public suspend fun sendImage(image: BufferedImage): MessageReceipt<TSubject> = subject.sendImage(image)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun sendImage(image: InputStream): MessageReceipt<TSubject> = subject.sendImage(image)
|
public suspend fun sendImage(image: InputStream): MessageReceipt<TSubject> = subject.sendImage(image)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun sendImage(image: File): MessageReceipt<TSubject> = subject.sendImage(image)
|
public suspend fun sendImage(image: File): MessageReceipt<TSubject> = subject.sendImage(image)
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region 上传图片 (扩展)
|
// region 上传图片 (扩展)
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun BufferedImage.upload(): Image = upload(subject)
|
public suspend fun BufferedImage.upload(): Image = upload(subject)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun InputStream.uploadAsImage(): Image = uploadAsImage(subject)
|
public suspend fun InputStream.uploadAsImage(): Image = uploadAsImage(subject)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun File.uploadAsImage(): Image = uploadAsImage(subject)
|
public suspend fun File.uploadAsImage(): Image = uploadAsImage(subject)
|
||||||
// endregion 上传图片 (扩展)
|
// endregion 上传图片 (扩展)
|
||||||
|
|
||||||
// region 发送图片 (扩展)
|
// region 发送图片 (扩展)
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun BufferedImage.send(): MessageReceipt<TSubject> = sendTo(subject)
|
public suspend fun BufferedImage.send(): MessageReceipt<TSubject> = sendTo(subject)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun InputStream.sendAsImage(): MessageReceipt<TSubject> = sendAsImageTo(subject)
|
public suspend fun InputStream.sendAsImage(): MessageReceipt<TSubject> = sendAsImageTo(subject)
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend fun File.sendAsImage(): MessageReceipt<TSubject> = sendAsImageTo(subject)
|
public suspend fun File.sendAsImage(): MessageReceipt<TSubject> = sendAsImageTo(subject)
|
||||||
// endregion 发送图片 (扩展)
|
// endregion 发送图片 (扩展)
|
||||||
|
|
||||||
}
|
}
|
@ -15,6 +15,7 @@ import net.mamoe.mirai.Bot
|
|||||||
import net.mamoe.mirai.contact.Group
|
import net.mamoe.mirai.contact.Group
|
||||||
import net.mamoe.mirai.contact.Member
|
import net.mamoe.mirai.contact.Member
|
||||||
import net.mamoe.mirai.contact.nameCardOrNick
|
import net.mamoe.mirai.contact.nameCardOrNick
|
||||||
|
import net.mamoe.mirai.event.AbstractEvent
|
||||||
import net.mamoe.mirai.event.BroadcastControllable
|
import net.mamoe.mirai.event.BroadcastControllable
|
||||||
import net.mamoe.mirai.message.data.MessageChain
|
import net.mamoe.mirai.message.data.MessageChain
|
||||||
import net.mamoe.mirai.message.data.MessageSource
|
import net.mamoe.mirai.message.data.MessageSource
|
||||||
@ -30,7 +31,7 @@ public class TempMessageEvent(
|
|||||||
public override val sender: Member,
|
public override val sender: Member,
|
||||||
public override val message: MessageChain,
|
public override val message: MessageChain,
|
||||||
public override val time: Int
|
public override val time: Int
|
||||||
) : TempMessage(), BroadcastControllable {
|
) : AbstractEvent(), GroupAwareMessageEvent, MessageEvent, BroadcastControllable {
|
||||||
init {
|
init {
|
||||||
val source = message[MessageSource] ?: error("Cannot find MessageSource from message")
|
val source = message[MessageSource] ?: error("Cannot find MessageSource from message")
|
||||||
check(source is OnlineMessageSource.Incoming.FromTemp) { "source provided to a TempMessage must be an instance of OnlineMessageSource.Incoming.FromTemp" }
|
check(source is OnlineMessageSource.Incoming.FromTemp) { "source provided to a TempMessage must be an instance of OnlineMessageSource.Incoming.FromTemp" }
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Mamoe Technologies and contributors.
|
||||||
|
*
|
||||||
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
|
*
|
||||||
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.mamoe.mirai.message
|
||||||
|
|
||||||
|
import net.mamoe.mirai.contact.User
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 来自 [User] 的消息
|
||||||
|
*
|
||||||
|
* @see FriendMessageEvent
|
||||||
|
* @see TempMessageEvent
|
||||||
|
*/
|
||||||
|
public interface UserMessageEvent : MessageEvent {
|
||||||
|
public override val subject: User
|
||||||
|
}
|
@ -12,11 +12,6 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.message.data
|
package net.mamoe.mirai.message.data
|
||||||
|
|
||||||
import net.mamoe.mirai.utils.PlannedRemoval
|
|
||||||
import kotlin.jvm.JvmField
|
|
||||||
import kotlin.jvm.JvmMultifileClass
|
|
||||||
import kotlin.jvm.JvmName
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速链接的两个消息 (避免构造新的 list).
|
* 快速链接的两个消息 (避免构造新的 list).
|
||||||
*
|
*
|
||||||
@ -33,16 +28,6 @@ internal constructor(
|
|||||||
@JvmField internal val tail: Message
|
@JvmField internal val tail: Message
|
||||||
) : Message, MessageChain, List<SingleMessage> by (left.flatten() + tail.flatten()).toList() {
|
) : Message, MessageChain, List<SingleMessage> by (left.flatten() + tail.flatten()).toList() {
|
||||||
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
@Deprecated(
|
|
||||||
"use asSequence from stdlib",
|
|
||||||
ReplaceWith("(this as List<SingleMessage>).asSequence()"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
|
||||||
@kotlin.internal.LowPriorityInOverloadResolution // resolve to extension from stdlib
|
|
||||||
fun asSequence(): Sequence<SingleMessage> = (this as List<SingleMessage>).asSequence()
|
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (other == null) return false
|
if (other == null) return false
|
||||||
if (other::class != CombinedMessage::class) return false
|
if (other::class != CombinedMessage::class) return false
|
||||||
|
@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
|
||||||
*
|
|
||||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
|
||||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
|
||||||
*
|
|
||||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
|
||||||
*/
|
|
||||||
|
|
||||||
@file:Suppress(
|
|
||||||
"EXPERIMENTAL_UNSIGNED_LITERALS",
|
|
||||||
"EXPERIMENTAL_API_USAGE",
|
|
||||||
"unused",
|
|
||||||
"DECLARATION_CANT_BE_INLINED", "UNCHECKED_CAST", "NOTHING_TO_INLINE"
|
|
||||||
)
|
|
||||||
|
|
||||||
@file:JvmMultifileClass
|
|
||||||
@file:JvmName("MessageEventKt")
|
|
||||||
|
|
||||||
package net.mamoe.mirai.message
|
|
||||||
|
|
||||||
import net.mamoe.mirai.Bot
|
|
||||||
import net.mamoe.mirai.contact.*
|
|
||||||
import net.mamoe.mirai.event.AbstractEvent
|
|
||||||
import net.mamoe.mirai.event.events.BotEvent
|
|
||||||
import net.mamoe.mirai.internal.network.Packet
|
|
||||||
import net.mamoe.mirai.message.data.MessageChain
|
|
||||||
import net.mamoe.mirai.message.data.OnlineMessageSource
|
|
||||||
import kotlin.jvm.JvmMultifileClass
|
|
||||||
import kotlin.jvm.JvmName
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 已废弃, 请使用 [MessageEvent]
|
|
||||||
*/
|
|
||||||
@Deprecated(
|
|
||||||
message = "use MessageEvent",
|
|
||||||
replaceWith = ReplaceWith("MessageEvent", "MessageEvent"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
public abstract class MessagePacketBase<out TSender : User, out TSubject : Contact> : Packet, BotEvent,
|
|
||||||
AbstractEvent() {
|
|
||||||
abstract override val bot: Bot
|
|
||||||
public abstract val sender: User
|
|
||||||
public abstract val subject: Contact
|
|
||||||
public abstract val message: MessageChain
|
|
||||||
public abstract val time: Int
|
|
||||||
public abstract val source: OnlineMessageSource.Incoming
|
|
||||||
public abstract val senderName: String
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
message = "Ambiguous name. Use MessageEvent instead",
|
|
||||||
replaceWith = ReplaceWith("MessageEvent", "MessageEvent"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
@Suppress("DEPRECATION_ERROR")
|
|
||||||
public abstract class MessagePacket : MessagePacketBase<User, Contact>(),
|
|
||||||
BotEvent, MessageEventExtensions<User, Contact> {
|
|
||||||
abstract override val bot: Bot
|
|
||||||
abstract override val sender: User
|
|
||||||
abstract override val subject: Contact
|
|
||||||
abstract override val message: MessageChain
|
|
||||||
abstract override val time: Int
|
|
||||||
abstract override val source: OnlineMessageSource.Incoming
|
|
||||||
abstract override val senderName: String
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
message = "Ambiguous name. Use MessageEvent instead",
|
|
||||||
replaceWith = ReplaceWith("MessageEvent", "MessageEvent"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
@Suppress("DEPRECATION_ERROR")
|
|
||||||
public abstract class ContactMessage : MessagePacket(),
|
|
||||||
BotEvent, MessageEventExtensions<User, Contact> {
|
|
||||||
abstract override val bot: Bot
|
|
||||||
abstract override val sender: User
|
|
||||||
abstract override val subject: Contact
|
|
||||||
abstract override val message: MessageChain
|
|
||||||
abstract override val time: Int
|
|
||||||
abstract override val source: OnlineMessageSource.Incoming
|
|
||||||
abstract override val senderName: String
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
message = "Ambiguous name. Use FriendMessageEvent instead",
|
|
||||||
replaceWith = ReplaceWith("FriendMessageEvent", "FriendMessageEvent"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
@Suppress("DEPRECATION_ERROR")
|
|
||||||
public abstract class FriendMessage : MessageEvent() {
|
|
||||||
abstract override val bot: Bot
|
|
||||||
abstract override val sender: Friend
|
|
||||||
abstract override val subject: Friend
|
|
||||||
abstract override val message: MessageChain
|
|
||||||
abstract override val time: Int
|
|
||||||
abstract override val source: OnlineMessageSource.Incoming.FromFriend
|
|
||||||
abstract override val senderName: String
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
message = "Ambiguous name. Use GroupMessageEvent instead",
|
|
||||||
replaceWith = ReplaceWith("GroupMessageEvent", "GroupMessageEvent"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
@Suppress("DEPRECATION_ERROR")
|
|
||||||
public abstract class GroupMessage : MessageEvent() {
|
|
||||||
public abstract val group: Group
|
|
||||||
abstract override val bot: Bot
|
|
||||||
abstract override val sender: Member
|
|
||||||
abstract override val subject: Group
|
|
||||||
abstract override val message: MessageChain
|
|
||||||
abstract override val time: Int
|
|
||||||
abstract override val source: OnlineMessageSource.Incoming.FromGroup
|
|
||||||
abstract override val senderName: String
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
message = "Ambiguous name. Use TempMessageEvent instead",
|
|
||||||
replaceWith = ReplaceWith("TempMessageEvent", "TempMessageEvent"),
|
|
||||||
level = DeprecationLevel.HIDDEN
|
|
||||||
)
|
|
||||||
public abstract class TempMessage : MessageEvent() {
|
|
||||||
abstract override val bot: Bot
|
|
||||||
abstract override val sender: Member
|
|
||||||
abstract override val subject: Member
|
|
||||||
abstract override val message: MessageChain
|
|
||||||
abstract override val time: Int
|
|
||||||
abstract override val source: OnlineMessageSource.Incoming.FromTemp
|
|
||||||
public abstract val group: Group
|
|
||||||
abstract override val senderName: String
|
|
||||||
}
|
|
@ -20,12 +20,8 @@ import net.mamoe.mirai.event.Listener
|
|||||||
import net.mamoe.mirai.event.syncFromEvent
|
import net.mamoe.mirai.event.syncFromEvent
|
||||||
import net.mamoe.mirai.event.syncFromEventOrNull
|
import net.mamoe.mirai.event.syncFromEventOrNull
|
||||||
import net.mamoe.mirai.message.data.MessageChain
|
import net.mamoe.mirai.message.data.MessageChain
|
||||||
import net.mamoe.mirai.utils.PlannedRemoval
|
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.coroutines.EmptyCoroutineContext
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
import kotlin.jvm.JvmMultifileClass
|
|
||||||
import kotlin.jvm.JvmName
|
|
||||||
import kotlin.jvm.JvmSynthetic
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,13 +107,4 @@ public inline fun <reified P : MessageEvent> P.nextMessageOrNullAsync(
|
|||||||
return this.bot.async(coroutineContext) {
|
return this.bot.async(coroutineContext) {
|
||||||
nextMessageOrNull(timeoutMillis, priority, filter)
|
nextMessageOrNull(timeoutMillis, priority, filter)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@PlannedRemoval("1.2.0")
|
|
||||||
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
|
|
||||||
@Suppress("DEPRECATION_ERROR")
|
|
||||||
@JvmSynthetic
|
|
||||||
public fun ContactMessage.isContextIdenticalWith(another: ContactMessage): Boolean {
|
|
||||||
return this.sender == another.sender && this.subject == another.subject && this.bot == another.bot
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user