Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
9aa32c49
Commit
9aa32c49
authored
Apr 07, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Compact MessageSubscribersBuilder
parent
b72589de
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
233 deletions
+100
-233
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt
...monMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt
+100
-233
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt
View file @
9aa32c49
...
@@ -260,7 +260,10 @@ typealias MessageListener<T, R> = @MessageDsl suspend T.(String) -> R
...
@@ -260,7 +260,10 @@ typealias MessageListener<T, R> = @MessageDsl suspend T.(String) -> R
*
*
* @see subscribeFriendMessages
* @see subscribeFriendMessages
*/
*/
@Suppress
(
"unused"
,
"DSL_SCOPE_VIOLATION_WARNING"
)
@Suppress
(
"unused"
,
"DSL_SCOPE_VIOLATION_WARNING"
,
"INAPPLICABLE_JVM_NAME"
,
"INVALID_CHARACTERS"
,
"NAME_CONTAINS_ILLEGAL_CHARS"
,
"FunctionName"
)
@MessageDsl
@MessageDsl
open
class
MessageSubscribersBuilder
<
M
:
ContactMessage
,
out
Ret
,
R
:
RR
,
RR
>(
open
class
MessageSubscribersBuilder
<
M
:
ContactMessage
,
out
Ret
,
R
:
RR
,
RR
>(
val
stub
:
RR
,
val
stub
:
RR
,
...
@@ -282,87 +285,88 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -282,87 +285,88 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
)
constructor
(
)
constructor
(
val
filter
:
M
.(
String
)
->
Boolean
val
filter
:
M
.(
String
)
->
Boolean
)
{
)
{
/**
/** 进行逻辑 `or`. */
* 进行逻辑 `or`.
*/
infix
fun
or
(
another
:
ListeningFilter
):
ListeningFilter
=
infix
fun
or
(
another
:
ListeningFilter
):
ListeningFilter
=
newListeningFilter
{
filter
.
invoke
(
this
,
it
)
||
another
.
filter
.
invoke
(
this
,
it
)
}
newListeningFilter
{
filter
.
invoke
(
this
,
it
)
||
another
.
filter
.
invoke
(
this
,
it
)
}
/**
/** 进行逻辑 `and`. */
* 进行逻辑 `and`.
*/
infix
fun
and
(
another
:
ListeningFilter
):
ListeningFilter
=
infix
fun
and
(
another
:
ListeningFilter
):
ListeningFilter
=
newListeningFilter
{
filter
.
invoke
(
this
,
it
)
&&
another
.
filter
.
invoke
(
this
,
it
)
}
newListeningFilter
{
filter
.
invoke
(
this
,
it
)
&&
another
.
filter
.
invoke
(
this
,
it
)
}
/**
/** 进行逻辑 `xor`. */
* 进行逻辑 `xor`.
*/
infix
fun
xor
(
another
:
ListeningFilter
):
ListeningFilter
=
infix
fun
xor
(
another
:
ListeningFilter
):
ListeningFilter
=
newListeningFilter
{
filter
.
invoke
(
this
,
it
)
xor
another
.
filter
.
invoke
(
this
,
it
)
}
newListeningFilter
{
filter
.
invoke
(
this
,
it
)
xor
another
.
filter
.
invoke
(
this
,
it
)
}
/**
/** 进行逻辑 `nand`, 即 `not and`. */
* 进行逻辑 `nand`, 即 `not and`.
*/
infix
fun
nand
(
another
:
ListeningFilter
):
ListeningFilter
=
infix
fun
nand
(
another
:
ListeningFilter
):
ListeningFilter
=
newListeningFilter
{
!
filter
.
invoke
(
this
,
it
)
||
!
another
.
filter
.
invoke
(
this
,
it
)
}
newListeningFilter
{
!
filter
.
invoke
(
this
,
it
)
||
!
another
.
filter
.
invoke
(
this
,
it
)
}
/**
/** 进行逻辑 `not` */
* 进行逻辑 `not`
fun
not
():
ListeningFilter
=
newListeningFilter
{
!
filter
.
invoke
(
this
,
it
)
}
*/
fun
not
():
ListeningFilter
=
newListeningFilter
{
!
filter
.
invoke
(
this
,
it
)
}
/**
/** 启动事件监听. */
* 启动事件监听.
*/
// do not inline due to kotlin (1.3.61) bug: java.lang.IllegalAccessError
// do not inline due to kotlin (1.3.61) bug: java.lang.IllegalAccessError
operator
fun
invoke
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
{
operator
fun
invoke
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
{
return
content
(
filter
,
onEvent
)
return
content
(
filter
,
onEvent
)
}
}
}
}
/** 启动这个监听器, 在满足条件时回复原消息 */
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
reply
(
toReply
:
String
):
Ret
{
open
infix
fun
ListeningFilter
.
reply
(
toReply
:
String
):
Ret
{
return
content
(
filter
)
{
reply
(
toReply
);
this
@MessageSubscribersBuilder
.
stub
}
return
content
(
filter
)
{
reply
(
toReply
);
this
@MessageSubscribersBuilder
.
stub
}
}
}
/** 启动这个监听器, 在满足条件时回复原消息 */
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
reply
(
message
:
Message
):
Ret
{
open
infix
fun
ListeningFilter
.
reply
(
message
:
Message
):
Ret
{
return
content
(
filter
)
{
reply
(
message
);
this
@MessageSubscribersBuilder
.
stub
}
return
content
(
filter
)
{
reply
(
message
);
this
@MessageSubscribersBuilder
.
stub
}
}
}
/** 启动这个监听器, 在满足条件时回复原消息 */
@JvmName
(
"reply3"
)
@JvmName
(
"reply3"
)
@Suppress
(
"INAPPLICABLE_JVM_NAME"
,
"INVALID_CHARACTERS"
,
"NAME_CONTAINS_ILLEGAL_CHARS"
,
"FunctionName"
)
@SinceMirai
(
"0.33.0"
)
@SinceMirai
(
"0.33.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
`->`(
toReply
:
String
):
Ret
{
open
infix
fun
ListeningFilter
.
`->`(
toReply
:
String
):
Ret
{
return
this
.
reply
(
toReply
)
return
this
.
reply
(
toReply
)
}
}
/** 启动这个监听器, 在满足条件时回复原消息 */
@JvmName
(
"reply3"
)
@JvmName
(
"reply3"
)
@Suppress
(
"INAPPLICABLE_JVM_NAME"
,
"INVALID_CHARACTERS"
,
"NAME_CONTAINS_ILLEGAL_CHARS"
,
"FunctionName"
)
@SinceMirai
(
"0.33.0"
)
@SinceMirai
(
"0.33.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
`->`(
message
:
Message
):
Ret
{
open
infix
fun
ListeningFilter
.
`->`(
message
:
Message
):
Ret
{
return
this
.
reply
(
message
)
return
this
.
reply
(
message
)
}
}
/** 启动这个监听器, 在满足条件时回复原消息 */
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
reply
(
replier
:
(
@MessageDsl
suspend
M
.(
String
)
->
Any
?)):
Ret
{
open
infix
fun
ListeningFilter
.
reply
(
replier
:
(
@MessageDsl
suspend
M
.(
String
)
->
Any
?)):
Ret
{
return
content
(
filter
)
{
return
content
(
filter
)
{
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
,
replier
)
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
,
replier
)
}
}
}
}
/** 启动这个监听器, 在满足条件时引用回复原消息 */
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
quoteReply
(
toReply
:
String
):
Ret
{
open
infix
fun
ListeningFilter
.
quoteReply
(
toReply
:
String
):
Ret
{
return
content
(
filter
)
{
quoteReply
(
toReply
);
this
@MessageSubscribersBuilder
.
stub
}
return
content
(
filter
)
{
quoteReply
(
toReply
);
this
@MessageSubscribersBuilder
.
stub
}
}
}
/** 启动这个监听器, 在满足条件时引用回复原消息 */
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
open
infix
fun
ListeningFilter
.
quoteReply
(
message
:
Message
):
Ret
{
@MessageDsl
return
content
(
filter
)
{
quoteReply
(
message
);
this
@MessageSubscribersBuilder
.
stub
}
open
infix
fun
ListeningFilter
.
quoteReply
(
toReply
:
Message
):
Ret
{
return
content
(
filter
)
{
quoteReply
(
toReply
);
this
@MessageSubscribersBuilder
.
stub
}
}
}
/** 启动这个监听器, 在满足条件时执行 [replier] 并引用回复原消息 */
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
@MessageDsl
open
infix
fun
ListeningFilter
.
quoteReply
(
replier
:
(
@MessageDsl
suspend
M
.(
String
)
->
Any
?)):
Ret
{
open
infix
fun
ListeningFilter
.
quoteReply
(
replier
:
(
@MessageDsl
suspend
M
.(
String
)
->
Any
?)):
Ret
{
return
content
(
filter
)
{
return
content
(
filter
)
{
@Suppress
(
"DSL_SCOPE_VIOLATION_WARNING"
)
@Suppress
(
"DSL_SCOPE_VIOLATION_WARNING"
)
...
@@ -371,15 +375,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -371,15 +375,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
}
}
/**
/** 无任何触发条件, 每次收到消息都执行 [onEvent] */
* 无任何触发条件.
*/
@MessageDsl
@MessageDsl
open
fun
always
(
onEvent
:
MessageListener
<
M
,
RR
>):
Ret
=
subscriber
({
true
},
onEvent
)
open
fun
always
(
onEvent
:
MessageListener
<
M
,
RR
>):
Ret
=
subscriber
({
true
},
onEvent
)
/**
/** 如果消息内容 `==` [equals] */
* 如果消息内容 `==` [equals]
*/
@MessageDsl
@MessageDsl
fun
case
(
fun
case
(
equals
:
String
,
equals
:
String
,
...
@@ -394,14 +394,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -394,14 +394,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
}
}
}
}
/**
/** 如果消息内容 `==` [equals] */
* 如果消息内容 `==` [equals]
*/
@MessageDsl
@MessageDsl
@JvmName
(
"case1"
)
@JvmName
(
"case1"
)
@JsName
(
"case1"
)
@JsName
(
"case1"
)
@SinceMirai
(
"0.29.0"
)
@SinceMirai
(
"0.29.0"
)
@Suppress
(
"INVALID_CHARACTERS"
,
"NAME_CONTAINS_ILLEGAL_CHARS"
,
"FunctionName"
)
infix
fun
String
.
`->`(
block
:
MessageListener
<
M
,
R
>):
Ret
{
infix
fun
String
.
`->`(
block
:
MessageListener
<
M
,
R
>):
Ret
{
return
case
(
this
,
onEvent
=
block
)
return
case
(
this
,
onEvent
=
block
)
}
}
...
@@ -424,12 +421,9 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -424,12 +421,9 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
})
})
}
}
/**
/** 如果消息内容包含 [sub] */
* 如果消息内容包含 [sub]
*/
@MessageDsl
@MessageDsl
fun
contains
(
sub
:
String
):
ListeningFilter
=
fun
contains
(
sub
:
String
):
ListeningFilter
=
content
{
sub
in
it
}
content
{
sub
in
it
}
/**
/**
* 如果消息内容包含 [sub] 中的任意一个元素
* 如果消息内容包含 [sub] 中的任意一个元素
...
@@ -453,16 +447,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -453,16 +447,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
}
}
}
}
/**
/** 如果消息内容包含 [sub] */
* 如果消息内容包含 [sub]
*/
@MessageDsl
@MessageDsl
fun
containsAny
(
vararg
sub
:
String
):
ListeningFilter
=
fun
containsAny
(
vararg
sub
:
String
):
ListeningFilter
=
content
{
sub
.
any
{
item
->
item
in
it
}
}
content
{
sub
.
any
{
item
->
item
in
it
}
}
/**
/** 如果消息内容包含 [sub] 中的任意一个元素 */
* 如果消息内容包含 [sub] 中的任意一个元素
*/
@MessageDsl
@MessageDsl
fun
containsAny
(
fun
containsAny
(
vararg
sub
:
String
,
vararg
sub
:
String
,
...
@@ -486,12 +475,9 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -486,12 +475,9 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
}
}
}
}
/**
/** 如果消息内容包含 [sub] */
* 如果消息内容包含 [sub]
*/
@MessageDsl
@MessageDsl
fun
containsAll
(
vararg
sub
:
String
):
ListeningFilter
=
fun
containsAll
(
vararg
sub
:
String
):
ListeningFilter
=
content
{
sub
.
all
{
item
->
item
in
it
}
}
content
{
sub
.
all
{
item
->
item
in
it
}
}
/**
/**
* 如果消息内容包含 [sub] 中的任意一个元素
* 如果消息内容包含 [sub] 中的任意一个元素
...
@@ -505,35 +491,22 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -505,35 +491,22 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
):
Ret
{
):
Ret
{
return
if
(
trim
)
{
return
if
(
trim
)
{
val
list
=
sub
.
map
{
it
.
trim
()
}
val
list
=
sub
.
map
{
it
.
trim
()
}
content
({
content
({
list
.
all
{
toCheck
->
it
.
contains
(
toCheck
,
ignoreCase
=
ignoreCase
)
}
},
list
.
all
{
toCheck
->
it
.
contains
(
toCheck
,
ignoreCase
=
ignoreCase
)
}
{
onEvent
(
this
,
this
.
message
.
contentToString
().
trim
())
})
},
{
onEvent
(
this
,
this
.
message
.
contentToString
().
trim
())
})
}
else
{
}
else
{
content
({
content
({
sub
.
all
{
toCheck
->
it
.
contains
(
toCheck
,
ignoreCase
=
ignoreCase
)
}
},
sub
.
all
{
toCheck
->
it
.
contains
(
toCheck
,
ignoreCase
=
ignoreCase
)
}
{
onEvent
(
this
,
this
.
message
.
contentToString
())
})
},
{
onEvent
(
this
,
this
.
message
.
contentToString
())
})
}
}
}
}
/**
/** 如果消息的前缀是 [prefix] */
* 如果消息的前缀是 [prefix]
*/
@MessageDsl
@MessageDsl
fun
startsWith
(
fun
startsWith
(
prefix
:
String
,
trim
:
Boolean
=
true
):
ListeningFilter
{
prefix
:
String
,
trim
:
Boolean
=
true
):
ListeningFilter
{
val
toCheck
=
if
(
trim
)
prefix
.
trim
()
else
prefix
val
toCheck
=
if
(
trim
)
prefix
.
trim
()
else
prefix
return
content
{
(
if
(
trim
)
it
.
trim
()
else
it
).
startsWith
(
toCheck
)
}
return
content
{
(
if
(
trim
)
it
.
trim
()
else
it
).
startsWith
(
toCheck
)
}
}
}
/**
/** 如果消息的前缀是 [prefix] */
* 如果消息的前缀是 [prefix]
*/
@MessageDsl
@MessageDsl
fun
startsWith
(
fun
startsWith
(
prefix
:
String
,
prefix
:
String
,
...
@@ -555,16 +528,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -555,16 +528,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
}
}
}
}
/**
/** 如果消息的结尾是 [suffix] */
* 如果消息的结尾是 [suffix]
*/
@MessageDsl
@MessageDsl
fun
endsWith
(
suffix
:
String
):
ListeningFilter
=
fun
endsWith
(
suffix
:
String
):
ListeningFilter
=
content
{
it
.
endsWith
(
suffix
)
}
content
{
it
.
endsWith
(
suffix
)
}
/**
/** 如果消息的结尾是 [suffix] */
* 如果消息的结尾是 [suffix]
*/
@MessageDsl
@MessageDsl
fun
endsWith
(
fun
endsWith
(
suffix
:
String
,
suffix
:
String
,
...
@@ -586,200 +554,123 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -586,200 +554,123 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
}
}
}
}
/**
/** 如果是这个人发的消息. 消息目前只会是群消息 */
* 如果是这个人发的消息. 消息目前只会是群消息
*/
@MessageDsl
@MessageDsl
fun
sentBy
(
name
:
String
):
ListeningFilter
=
fun
sentBy
(
name
:
String
):
ListeningFilter
=
content
{
this
is
GroupMessage
&&
this
.
senderName
==
name
}
content
{
this
is
GroupMessage
&&
this
.
senderName
==
name
}
/**
/** 如果是这个人发的消息. 消息目前只会是群消息 */
* 如果是这个人发的消息. 消息目前只会是群消息
*/
@MessageDsl
@MessageDsl
fun
sentBy
(
name
:
String
,
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
fun
sentBy
(
name
:
String
,
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
content
({
this
is
GroupMessage
&&
this
.
senderName
==
name
},
onEvent
)
content
({
(
this
as
?
GroupMessage
)
?
.
senderName
==
name
},
onEvent
)
/**
/** 如果是这个人发的消息. 消息可以是好友消息也可以是群消息 */
* 如果是这个人发的消息. 消息可以是好友消息也可以是群消息
*/
@MessageDsl
@MessageDsl
fun
sentBy
(
qq
:
Long
):
ListeningFilter
=
fun
sentBy
(
qq
:
Long
):
ListeningFilter
=
content
{
sender
.
id
==
qq
}
content
{
sender
.
id
==
qq
}
/**
/** 如果是这个人发的消息. 消息可以是好友消息也可以是群消息 */
* 如果是这个人发的消息. 消息可以是好友消息也可以是群消息
*/
@MessageDsl
@MessageDsl
fun
sentBy
(
qq
:
Long
,
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
fun
sentBy
(
qq
:
Long
,
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
content
({
this
.
sender
.
id
==
qq
},
onEvent
)
content
({
this
.
sender
.
id
==
qq
},
onEvent
)
/**
/** 如果是好友发来的消息 */
* 如果是好友发来的消息
*/
@MessageDsl
@MessageDsl
fun
sentByFriend
(
onEvent
:
MessageListener
<
FriendMessage
,
R
>):
Ret
=
fun
sentByFriend
(
onEvent
:
MessageListener
<
FriendMessage
,
R
>):
Ret
=
content
({
this
is
FriendMessage
})
{
content
({
this
is
FriendMessage
})
{
onEvent
(
this
as
FriendMessage
,
it
)
onEvent
(
this
as
FriendMessage
,
it
)
}
}
/**
/** 如果是好友发来的消息 */
* 如果是好友发来的消息
*/
@MessageDsl
@MessageDsl
fun
sentByFriend
():
ListeningFilter
=
newListeningFilter
{
this
is
FriendMessage
}
fun
sentByFriend
():
ListeningFilter
=
newListeningFilter
{
this
is
FriendMessage
}
/**
/** 如果是管理员或群主发的消息 */
* 如果是管理员或群主发的消息
*/
@MessageDsl
@MessageDsl
fun
sentByOperator
():
ListeningFilter
=
fun
sentByOperator
():
ListeningFilter
=
content
{
this
is
GroupMessage
&&
sender
.
permission
.
isOperator
()
}
content
{
this
is
GroupMessage
&&
sender
.
permission
.
isOperator
()
}
/**
/** 如果是管理员或群主发的消息 */
* 如果是管理员或群主发的消息
*/
@MessageDsl
@MessageDsl
fun
sentByOperator
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
fun
sentByOperator
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
content
({
this
is
GroupMessage
&&
this
.
sender
.
isOperator
()
},
onEvent
)
content
({
this
is
GroupMessage
&&
this
.
sender
.
isOperator
()
},
onEvent
)
/**
/** 如果是管理员发的消息 */
* 如果是管理员发的消息
*/
@MessageDsl
@MessageDsl
fun
sentByAdministrator
():
ListeningFilter
=
fun
sentByAdministrator
():
ListeningFilter
=
content
{
this
is
GroupMessage
&&
sender
.
permission
.
isAdministrator
()
}
content
{
this
is
GroupMessage
&&
sender
.
permission
.
isAdministrator
()
}
/**
/** 如果是管理员发的消息 */
* 如果是管理员发的消息
*/
@MessageDsl
@MessageDsl
fun
sentByAdministrator
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
fun
sentByAdministrator
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
content
({
this
is
GroupMessage
&&
this
.
sender
.
isAdministrator
()
},
onEvent
)
content
({
this
is
GroupMessage
&&
this
.
sender
.
isAdministrator
()
},
onEvent
)
/**
/** 如果是群主发的消息 */
* 如果是群主发的消息
*/
@MessageDsl
@MessageDsl
fun
sentByOwner
():
ListeningFilter
=
fun
sentByOwner
():
ListeningFilter
=
content
{
this
is
GroupMessage
&&
sender
.
isOwner
()
}
content
{
this
is
GroupMessage
&&
sender
.
isOwner
()
}
/**
/** 如果是群主发的消息 */
* 如果是群主发的消息
*/
@MessageDsl
@MessageDsl
fun
sentByOwner
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
fun
sentByOwner
(
onEvent
:
MessageListener
<
M
,
R
>):
Ret
=
content
({
this
is
GroupMessage
&&
this
.
sender
.
isOwner
()
},
onEvent
)
content
({
this
is
GroupMessage
&&
this
.
sender
.
isOwner
()
},
onEvent
)
/**
/** 如果是来自这个群的消息 */
* 如果是来自这个群的消息
*/
@MessageDsl
@MessageDsl
fun
sentFrom
(
groupId
:
Long
):
ListeningFilter
=
fun
sentFrom
(
groupId
:
Long
):
ListeningFilter
=
content
{
this
is
GroupMessage
&&
group
.
id
==
groupId
}
content
{
this
is
GroupMessage
&&
group
.
id
==
groupId
}
/**
/** 如果是来自这个群的消息, 就执行 [onEvent] */
* 如果是来自这个群的消息, 就执行 [onEvent]
*/
@MessageDsl
@MessageDsl
fun
sentFrom
(
groupId
:
Long
,
onEvent
:
MessageListener
<
GroupMessage
,
R
>):
Ret
=
fun
sentFrom
(
groupId
:
Long
,
onEvent
:
MessageListener
<
GroupMessage
,
R
>):
Ret
=
content
({
this
is
GroupMessage
&&
this
.
group
.
id
==
groupId
})
{
content
({
this
is
GroupMessage
&&
this
.
group
.
id
==
groupId
})
{
onEvent
(
this
as
GroupMessage
,
it
)
}
onEvent
(
this
as
GroupMessage
,
it
)
}
/**
/** 如果消息内容包含 [N] 类型的 [Message] */
* 如果消息内容包含 [N] 类型的 [Message]
*/
@MessageDsl
@MessageDsl
inline
fun
<
reified
N
:
Message
>
has
():
ListeningFilter
=
inline
fun
<
reified
N
:
Message
>
has
():
ListeningFilter
=
content
{
message
.
any
{
it
is
N
}
}
content
{
message
.
any
{
it
is
N
}
}
/**
/** 如果消息内容包含 [M] 类型的 [Message], 就执行 [onEvent] */
* 如果消息内容包含 [M] 类型的 [Message], 就执行 [onEvent]
*/
@MessageDsl
@MessageDsl
@SinceMirai
(
"0.30.0"
)
@SinceMirai
(
"0.30.0"
)
inline
fun
<
reified
N
:
Message
>
has
(
noinline
onEvent
:
@MessageDsl
suspend
M
.(
N
)
->
R
):
Ret
=
inline
fun
<
reified
N
:
Message
>
has
(
noinline
onEvent
:
@MessageDsl
suspend
M
.(
N
)
->
R
):
Ret
=
content
({
message
.
any
{
it
is
N
}
},
{
onEvent
.
invoke
(
this
,
message
.
firstIsInstance
())
})
content
({
message
.
any
{
it
is
N
}
},
{
onEvent
.
invoke
(
this
,
message
.
firstIsInstance
())
})
/**
/** 如果 [mapper] 返回值非空, 就执行 [onEvent] */
* 如果 [mapper] 返回值非空, 就执行 [onEvent]
*/
@MessageDsl
@MessageDsl
@SinceMirai
(
"0.30.0"
)
@SinceMirai
(
"0.30.0"
)
open
fun
<
N
:
Any
>
mapping
(
open
fun
<
N
:
Any
>
mapping
(
mapper
:
M
.(
String
)
->
N
?,
onEvent
:
@MessageDsl
suspend
M
.(
N
)
->
R
):
Ret
=
mapper
:
M
.(
String
)
->
N
?,
always
{
onEvent
.
invoke
(
this
,
mapper
(
this
,
message
.
contentToString
())
?:
return
@
always
stub
)
}
onEvent
:
@MessageDsl
suspend
M
.(
N
)
->
R
):
Ret
=
always
{
onEvent
.
invoke
(
this
,
mapper
.
invoke
(
this
,
message
.
contentToString
())
?:
return
@
always
this
@MessageSubscribersBuilder
.
stub
)
}
/**
/** 如果 [filter] 返回 `true` */
* 如果 [filter] 返回 `true`
*/
@MessageDsl
@MessageDsl
fun
content
(
filter
:
M
.(
String
)
->
Boolean
):
ListeningFilter
=
fun
content
(
filter
:
M
.(
String
)
->
Boolean
):
ListeningFilter
=
newListeningFilter
(
filter
)
newListeningFilter
(
filter
)
/**
/** 如果 [filter] 返回 `true` 就执行 `onEvent`*/
* 如果 [filter] 返回 `true` 就执行 `onEvent`
*/
@MessageDsl
@MessageDsl
fun
content
(
fun
content
(
filter
:
M
.(
String
)
->
Boolean
,
onEvent
:
MessageListener
<
M
,
RR
>):
Ret
=
filter
:
M
.(
String
)
->
Boolean
,
onEvent
:
MessageListener
<
M
,
RR
>
):
Ret
=
subscriber
(
filter
)
{
onEvent
(
this
,
it
)
}
subscriber
(
filter
)
{
onEvent
(
this
,
it
)
}
/**
/** 如果消息内容可由正则表达式匹配([Regex.matchEntire]) */
* 如果消息内容可由正则表达式匹配([Regex.matchEntire])
*/
@MessageDsl
@MessageDsl
fun
matching
(
regex
:
Regex
):
ListeningFilter
=
fun
matching
(
regex
:
Regex
):
ListeningFilter
=
content
{
regex
.
matchEntire
(
it
)
!=
null
}
content
{
regex
.
matchEntire
(
it
)
!=
null
}
/**
/** 如果消息内容可由正则表达式匹配([Regex.matchEntire]), 就执行 `onEvent` */
* 如果消息内容可由正则表达式匹配([Regex.matchEntire]), 就执行 `onEvent`
*/
@MessageDsl
@MessageDsl
fun
matching
(
regex
:
Regex
,
onEvent
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Unit
):
Ret
=
fun
matching
(
regex
:
Regex
,
onEvent
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Unit
):
Ret
=
always
{
always
{
executeAndReply
(
this
)
{
onEvent
.
invoke
(
this
,
regex
.
matchEntire
(
it
)
?:
return
@
always
stub
)
}
}
val
find
=
regex
.
matchEntire
(
it
)
?:
return
@
always
this
@MessageSubscribersBuilder
.
stub
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
onEvent
.
invoke
(
this
,
find
)
}
}
/**
/** 如果消息内容可由正则表达式查找([Regex.find]) */
* 如果消息内容可由正则表达式查找([Regex.find])
*/
@MessageDsl
@MessageDsl
fun
finding
(
regex
:
Regex
):
ListeningFilter
=
fun
finding
(
regex
:
Regex
):
ListeningFilter
=
content
{
regex
.
find
(
it
)
!=
null
}
content
{
regex
.
find
(
it
)
!=
null
}
/**
/** 如果消息内容可由正则表达式查找([Regex.find]), 就执行 `onEvent` */
* 如果消息内容可由正则表达式查找([Regex.find]), 就执行 `onEvent`
*/
@MessageDsl
@MessageDsl
fun
finding
(
regex
:
Regex
,
onEvent
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Unit
):
Ret
=
fun
finding
(
regex
:
Regex
,
onEvent
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Unit
):
Ret
=
always
{
always
{
executeAndReply
(
this
)
{
onEvent
.
invoke
(
this
,
regex
.
find
(
it
)
?:
return
@
always
stub
)
}
}
val
find
=
regex
.
find
(
it
)
?:
return
@
always
this
@MessageSubscribersBuilder
.
stub
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
onEvent
.
invoke
(
this
,
find
)
}
}
/**
/** 若消息内容包含 [this] 则回复 [reply] */
* 若消息内容包含 [this] 则回复 [reply]
*/
@MessageDsl
@MessageDsl
open
infix
fun
String
.
containsReply
(
reply
:
String
):
Ret
=
open
infix
fun
String
.
containsReply
(
reply
:
String
):
Ret
=
content
({
this
@
containsReply
in
it
},
{
reply
(
reply
);
this
@MessageSubscribersBuilder
.
stub
})
content
({
this
@
containsReply
in
it
},
{
reply
(
reply
);
stub
})
/**
/**
* 若消息内容包含 [this] 则执行 [replier] 并将其返回值回复给发信对象.
* 若消息内容包含 [this] 则执行 [replier] 并将其返回值回复给发信对象.
...
@@ -790,9 +681,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -790,9 +681,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
*/
*/
@MessageDsl
@MessageDsl
open
infix
fun
String
.
containsReply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
=
open
infix
fun
String
.
containsReply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
=
content
({
this
@
containsReply
in
it
},
{
content
({
this
@
containsReply
in
it
},
{
executeAndReply
(
this
,
replier
)
})
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
,
replier
)
})
/**
/**
* 若消息内容可由正则表达式匹配([Regex.matchEntire]), 则执行 [replier] 并将其返回值回复给发信对象.
* 若消息内容可由正则表达式匹配([Regex.matchEntire]), 则执行 [replier] 并将其返回值回复给发信对象.
...
@@ -803,12 +692,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -803,12 +692,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
*/
*/
@MessageDsl
@MessageDsl
open
infix
fun
Regex
.
matchingReply
(
replier
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Any
?):
Ret
=
open
infix
fun
Regex
.
matchingReply
(
replier
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Any
?):
Ret
=
always
{
always
{
executeAndReply
(
this
)
{
replier
.
invoke
(
this
,
matchEntire
(
it
)
?:
return
@
always
stub
)
}
}
val
find
=
this
@
matchingReply
.
matchEntire
(
it
)
?:
return
@
always
this
@MessageSubscribersBuilder
.
stub
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
replier
.
invoke
(
this
,
find
)
}
}
/**
/**
* 若消息内容可由正则表达式查找([Regex.find]), 则执行 [replier] 并将其返回值回复给发信对象.
* 若消息内容可由正则表达式查找([Regex.find]), 则执行 [replier] 并将其返回值回复给发信对象.
...
@@ -819,12 +703,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -819,12 +703,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
*/
*/
@MessageDsl
@MessageDsl
open
infix
fun
Regex
.
findingReply
(
replier
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Any
?):
Ret
=
open
infix
fun
Regex
.
findingReply
(
replier
:
@MessageDsl
suspend
M
.(
MatchResult
)
->
Any
?):
Ret
=
always
{
always
{
executeAndReply
(
this
)
{
replier
.
invoke
(
this
,
this
@
findingReply
.
find
(
it
)
?:
return
@
always
stub
)
}
}
val
find
=
this
@
findingReply
.
find
(
it
)
?:
return
@
always
this
@MessageSubscribersBuilder
.
stub
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
replier
.
invoke
(
this
,
find
)
}
}
/**
/**
* 不考虑空格, 若消息内容以 [this] 开始则执行 [replier] 并将其返回值回复给发信对象.
* 不考虑空格, 若消息内容以 [this] 开始则执行 [replier] 并将其返回值回复给发信对象.
...
@@ -843,9 +722,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -843,9 +722,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
open
infix
fun
String
.
startsWithReply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
{
open
infix
fun
String
.
startsWithReply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
{
val
toCheck
=
this
.
trimStart
()
val
toCheck
=
this
.
trimStart
()
return
content
({
it
.
trim
().
startsWith
(
toCheck
)
},
{
return
content
({
it
.
trim
().
startsWith
(
toCheck
)
},
{
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
executeAndReply
(
this
)
{
replier
(
this
,
it
.
trim
().
removePrefix
(
toCheck
))
}
replier
(
this
,
it
.
trim
().
removePrefix
(
toCheck
))
}
})
})
}
}
...
@@ -866,32 +743,31 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -866,32 +743,31 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
open
infix
fun
String
.
endsWithReply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
{
open
infix
fun
String
.
endsWithReply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
{
val
toCheck
=
this
.
trimEnd
()
val
toCheck
=
this
.
trimEnd
()
return
content
({
it
.
trim
().
endsWith
(
toCheck
)
},
{
return
content
({
it
.
trim
().
endsWith
(
toCheck
)
},
{
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
executeAndReply
(
this
)
{
replier
(
this
,
it
.
trim
().
removeSuffix
(
toCheck
))
}
replier
(
this
,
it
.
trim
().
removeSuffix
(
toCheck
))
}
})
})
}
}
/** 当发送的消息内容为 [this] 就回复 [reply] */
@MessageDsl
@MessageDsl
open
infix
fun
String
.
reply
(
reply
:
String
):
Ret
{
open
infix
fun
String
.
reply
(
reply
:
String
):
Ret
{
val
toCheck
=
this
.
trim
()
val
toCheck
=
this
.
trim
()
return
content
({
it
.
trim
()
==
toCheck
},
{
reply
(
reply
);
this
@MessageSubscribersBuilder
.
stub
})
return
content
({
it
.
trim
()
==
toCheck
},
{
reply
(
reply
);
this
@MessageSubscribersBuilder
.
stub
})
}
}
/** 当发送的消息内容为 [this] 就回复 [reply] */
@MessageDsl
@MessageDsl
open
infix
fun
String
.
reply
(
reply
:
Message
):
Ret
{
open
infix
fun
String
.
reply
(
reply
:
Message
):
Ret
{
val
toCheck
=
this
.
trim
()
val
toCheck
=
this
.
trim
()
return
content
({
it
.
trim
()
==
toCheck
},
{
reply
(
reply
);
this
@MessageSubscribersBuilder
.
stub
})
return
content
({
it
.
trim
()
==
toCheck
},
{
reply
(
reply
);
this
@MessageSubscribersBuilder
.
stub
})
}
}
/** 当发送的消息内容为 [this] 就执行并回复 [replier] 的返回值 */
@MessageDsl
@MessageDsl
open
infix
fun
String
.
reply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
{
open
infix
fun
String
.
reply
(
replier
:
@MessageDsl
suspend
M
.(
String
)
->
Any
?):
Ret
{
val
toCheck
=
this
.
trim
()
val
toCheck
=
this
.
trim
()
return
content
({
it
.
trim
()
==
toCheck
},
{
return
content
({
it
.
trim
()
==
toCheck
},
{
@Suppress
(
"DSL_SCOPE_VIOLATION_WARNING"
)
@Suppress
(
"DSL_SCOPE_VIOLATION_WARNING"
)
this
@MessageSubscribersBuilder
.
executeAndReply
(
this
)
{
executeAndReply
(
this
)
{
replier
(
this
,
it
.
trim
())
}
replier
(
this
,
it
.
trim
())
}
})
})
}
}
...
@@ -900,9 +776,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -900,9 +776,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
internal
suspend
inline
fun
executeAndReply
(
m
:
M
,
replier
:
suspend
M
.(
String
)
->
Any
?):
RR
{
internal
suspend
inline
fun
executeAndReply
(
m
:
M
,
replier
:
suspend
M
.(
String
)
->
Any
?):
RR
{
when
(
val
message
=
replier
(
m
,
m
.
message
.
contentToString
()))
{
when
(
val
message
=
replier
(
m
,
m
.
message
.
contentToString
()))
{
is
Message
->
m
.
reply
(
message
)
is
Message
->
m
.
reply
(
message
)
is
Unit
->
{
is
Unit
->
Unit
}
else
->
m
.
reply
(
message
.
toString
())
else
->
m
.
reply
(
message
.
toString
())
}
}
return
stub
return
stub
...
@@ -913,19 +787,12 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
...
@@ -913,19 +787,12 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
internal
suspend
inline
fun
executeAndQuoteReply
(
m
:
M
,
replier
:
suspend
M
.(
String
)
->
Any
?):
RR
{
internal
suspend
inline
fun
executeAndQuoteReply
(
m
:
M
,
replier
:
suspend
M
.(
String
)
->
Any
?):
RR
{
when
(
val
message
=
replier
(
m
,
m
.
message
.
contentToString
()))
{
when
(
val
message
=
replier
(
m
,
m
.
message
.
contentToString
()))
{
is
Message
->
m
.
quoteReply
(
message
)
is
Message
->
m
.
quoteReply
(
message
)
is
Unit
->
{
is
Unit
->
Unit
}
else
->
m
.
quoteReply
(
message
.
toString
())
else
->
m
.
quoteReply
(
message
.
toString
())
}
}
return
stub
return
stub
}
}
/* 易产生迷惑感
fun replyCase(equals: String, trim: Boolean = true, replier: MessageReplier<T>) = case(equals, trim) { reply(replier(this)) }
fun replyContains(value: String, replier: MessageReplier<T>) = content({ value in it }) { replier(this) }
fun replyStartsWith(value: String, replier: MessageReplier<T>) = content({ it.startsWith(value) }) { replier(this) }
fun replyEndsWith(value: String, replier: MessageReplier<T>) = content({ it.endsWith(value) }) { replier(this) }
*/
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment