forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.kt
123 lines (111 loc) · 4.34 KB
/
utils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* 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
*/
@file:JvmMultifileClass
@file:JvmName("MessageEventKt")
@file:Suppress("unused")
package net.mamoe.mirai.message
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import net.mamoe.mirai.event.EventPriority
import net.mamoe.mirai.event.Listener
import net.mamoe.mirai.event.syncFromEvent
import net.mamoe.mirai.event.syncFromEventOrNull
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.utils.PlannedRemoval
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
/**
* 判断两个 [MessageEvent] 的 [MessageEvent.sender] 和 [MessageEvent.subject] 是否相同
*/
fun MessageEvent.isContextIdenticalWith(another: MessageEvent): Boolean {
return this.sender == another.sender && this.subject == another.subject
}
/**
* 挂起当前协程, 等待下一条 [MessageEvent.sender] 和 [MessageEvent.subject] 与 [this] 相同且通过 [筛选][filter] 的 [MessageEvent]
*
* 若 [filter] 抛出了一个异常, 本函数会立即抛出这个异常.
*
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param filter 过滤器. 返回非 null 则代表得到了需要的值. [syncFromEvent] 会返回这个值
*
* @see syncFromEvent 实现原理
*/
@JvmSynthetic
suspend inline fun <reified P : MessageEvent> P.nextMessage(
timeoutMillis: Long = -1,
priority: Listener.EventPriority = EventPriority.MONITOR,
noinline filter: suspend P.(P) -> Boolean = { true }
): MessageChain {
return syncFromEvent<P, P>(timeoutMillis, priority) {
takeIf { this.isContextIdenticalWith(this@nextMessage) }?.takeIf { filter(it, it) }
}.message
}
/**
* 挂起当前协程, 等待下一条 [MessageEvent.sender] 和 [MessageEvent.subject] 与 [this] 相同且通过 [筛选][filter] 的 [MessageEvent]
*
* 若 [filter] 抛出了一个异常, 本函数会立即抛出这个异常.
*
* @param timeoutMillis 超时. 单位为毫秒.
* @param filter 过滤器. 返回非 null 则代表得到了需要的值. [syncFromEvent] 会返回这个值
* @return 消息链. 超时时返回 `null`
*
* @see syncFromEventOrNull 实现原理
*/
@JvmSynthetic
suspend inline fun <reified P : MessageEvent> P.nextMessageOrNull(
timeoutMillis: Long,
priority: Listener.EventPriority = EventPriority.MONITOR,
noinline filter: suspend P.(P) -> Boolean = { true }
): MessageChain? {
require(timeoutMillis > 0) { "timeoutMillis must be > 0" }
return syncFromEventOrNull<P, P>(timeoutMillis, priority) {
takeIf { this.isContextIdenticalWith(this@nextMessageOrNull) }?.takeIf { filter(it, it) }
}?.message
}
/**
* @see nextMessage
*/
@JvmSynthetic
inline fun <reified P : MessageEvent> P.nextMessageAsync(
timeoutMillis: Long = -1,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
priority: Listener.EventPriority = EventPriority.MONITOR,
noinline filter: suspend P.(P) -> Boolean = { true }
): Deferred<MessageChain> {
return this.bot.async(coroutineContext) {
nextMessage(timeoutMillis, priority, filter)
}
}
/**
* [nextMessageOrNull] 的异步版本
*
* @see nextMessageOrNull
*/
@JvmSynthetic
inline fun <reified P : MessageEvent> P.nextMessageOrNullAsync(
timeoutMillis: Long,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
priority: Listener.EventPriority = EventPriority.MONITOR,
noinline filter: suspend P.(P) -> Boolean = { true }
): Deferred<MessageChain?> {
require(timeoutMillis > 0) { "timeoutMillis must be > 0" }
return this.bot.async(coroutineContext) {
nextMessageOrNull(timeoutMillis, priority, filter)
}
}
@PlannedRemoval("1.2.0")
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
@Suppress("DEPRECATION_ERROR")
@JvmSynthetic
fun ContactMessage.isContextIdenticalWith(another: ContactMessage): Boolean {
return this.sender == another.sender && this.subject == another.subject && this.bot == another.bot
}