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
72207a3e
Commit
72207a3e
authored
Jan 19, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More actions
parent
33905806
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
11 deletions
+78
-11
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt
...onMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt
+78
-11
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt
View file @
72207a3e
...
@@ -171,6 +171,23 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
...
@@ -171,6 +171,23 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
@MessageDsl
@MessageDsl
fun
always
(
onEvent
:
MessageListener
<
T
>):
Listener
<
T
>
=
subscriber
(
onEvent
)
fun
always
(
onEvent
:
MessageListener
<
T
>):
Listener
<
T
>
=
subscriber
(
onEvent
)
/**
* 如果消息内容 `==` [equals]
*/
@MessageDsl
fun
case
(
equals
:
String
,
ignoreCase
:
Boolean
=
false
,
trim
:
Boolean
=
true
):
ListeningFilter
{
return
if
(
trim
)
{
val
toCheck
=
equals
.
trim
()
content
{
it
.
trim
().
equals
(
toCheck
,
ignoreCase
=
ignoreCase
)
}
}
else
{
content
{
it
.
equals
(
equals
,
ignoreCase
=
ignoreCase
)
}
}
}
/**
/**
* 如果消息内容 `==` [equals]
* 如果消息内容 `==` [equals]
* @param trim `true` 则删除首尾空格后比较
* @param trim `true` 则删除首尾空格后比较
...
@@ -199,16 +216,35 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
...
@@ -199,16 +216,35 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
/**
/**
* 如果消息内容包含 [sub]
* 如果消息内容包含 [sub]
*/
*/
@MessageDsl
inline
fun
contains
(
inline
fun
contains
(
sub
:
String
,
sub
:
String
,
ignoreCase
:
Boolean
=
false
,
ignoreCase
:
Boolean
=
false
,
trim
:
Boolean
=
true
,
trim
:
Boolean
=
true
,
crossinline
onEvent
:
MessageListener
<
T
>
crossinline
onEvent
:
MessageListener
<
T
>
):
Listener
<
T
>
{
):
Listener
<
T
>
{
val
toCheck
=
if
(
trim
)
sub
.
trim
()
else
sub
return
if
(
trim
)
{
return
content
({
(
if
(
trim
)
it
.
trim
()
else
it
).
contains
(
toCheck
,
ignoreCase
=
ignoreCase
)
},
{
val
toCheck
=
sub
.
trim
()
onEvent
(
this
,
this
.
message
.
toString
())
content
({
it
.
trimStart
().
contains
(
toCheck
,
ignoreCase
=
ignoreCase
)
},
{
})
onEvent
(
this
,
this
.
message
.
toString
().
trim
())
})
}
else
{
content
({
it
.
contains
(
sub
,
ignoreCase
=
ignoreCase
)
},
{
onEvent
(
this
,
this
.
message
.
toString
())
})
}
}
/**
* 如果消息的前缀是 [prefix]
*/
@MessageDsl
fun
startsWith
(
prefix
:
String
,
trim
:
Boolean
=
true
):
ListeningFilter
{
val
toCheck
=
if
(
trim
)
prefix
.
trim
()
else
prefix
return
content
{
(
if
(
trim
)
it
.
trim
()
else
it
).
startsWith
(
toCheck
)
}
}
}
/**
/**
...
@@ -221,11 +257,18 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
...
@@ -221,11 +257,18 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
trim
:
Boolean
=
true
,
trim
:
Boolean
=
true
,
crossinline
onEvent
:
@MessageDsl
suspend
T
.(
String
)
->
Unit
crossinline
onEvent
:
@MessageDsl
suspend
T
.(
String
)
->
Unit
):
Listener
<
T
>
{
):
Listener
<
T
>
{
val
toCheck
=
if
(
trim
)
prefix
.
trim
()
else
prefix
return
if
(
trim
)
{
return
content
({
(
if
(
trim
)
it
.
trim
()
else
it
).
startsWith
(
toCheck
)
},
{
val
toCheck
=
prefix
.
trim
()
if
(
removePrefix
)
this
.
onEvent
(
this
.
message
.
toString
().
substringAfter
(
toCheck
))
content
({
it
.
trimStart
().
startsWith
(
toCheck
)
},
{
else
onEvent
(
this
,
this
.
message
.
toString
())
if
(
removePrefix
)
this
.
onEvent
(
this
.
message
.
toString
().
substringAfter
(
toCheck
).
trim
())
})
else
onEvent
(
this
,
this
.
message
.
toString
().
trim
())
})
}
else
{
content
({
it
.
startsWith
(
prefix
)
},
{
if
(
removePrefix
)
this
.
onEvent
(
this
.
message
.
toString
().
removePrefix
(
prefix
))
else
onEvent
(
this
,
this
.
message
.
toString
())
})
}
}
}
/**
/**
...
@@ -235,6 +278,30 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
...
@@ -235,6 +278,30 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
fun
endsWith
(
suffix
:
String
):
ListeningFilter
=
fun
endsWith
(
suffix
:
String
):
ListeningFilter
=
content
{
it
.
endsWith
(
suffix
)
}
content
{
it
.
endsWith
(
suffix
)
}
/**
* 如果消息的结尾是 [suffix]
*/
@MessageDsl
inline
fun
endsWith
(
suffix
:
String
,
removeSuffix
:
Boolean
=
true
,
trim
:
Boolean
=
true
,
crossinline
onEvent
:
@MessageDsl
suspend
T
.(
String
)
->
Unit
):
Listener
<
T
>
{
return
if
(
trim
)
{
val
toCheck
=
suffix
.
trim
()
content
({
it
.
trimStart
().
startsWith
(
toCheck
)
},
{
if
(
removeSuffix
)
this
.
onEvent
(
this
.
message
.
toString
().
substringBeforeLast
(
toCheck
).
trim
())
else
onEvent
(
this
,
this
.
message
.
toString
().
trim
())
})
}
else
{
content
({
it
.
startsWith
(
suffix
)
},
{
if
(
removeSuffix
)
this
.
onEvent
(
this
.
message
.
toString
().
removeSuffix
(
suffix
))
else
onEvent
(
this
,
this
.
message
.
toString
())
})
}
}
/**
/**
* 如果是这个人发的消息. 消息可以是好友消息也可以是群消息
* 如果是这个人发的消息. 消息可以是好友消息也可以是群消息
*/
*/
...
@@ -323,8 +390,8 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
...
@@ -323,8 +390,8 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* 如果 [filter] 返回 `true`
* 如果 [filter] 返回 `true`
*/
*/
@MessageDsl
@MessageDsl
inline
fun
content
(
crossinline
filter
:
T
.(
String
)
->
Boolean
):
ListeningFilter
=
fun
content
(
filter
:
T
.(
String
)
->
Boolean
):
ListeningFilter
=
ListeningFilter
{
filter
(
this
,
it
)
}
ListeningFilter
(
filter
)
/**
/**
* 如果 [filter] 返回 `true` 就执行 `onEvent`
* 如果 [filter] 返回 `true` 就执行 `onEvent`
...
...
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