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
f9daee03
Commit
f9daee03
authored
Oct 15, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update docs
parent
fdd9db65
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
7 deletions
+17
-7
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt
...rc/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt
+11
-3
mirai-demos/mirai-demo-1/src/main/java/demo1/Main.kt
mirai-demos/mirai-demo-1/src/main/java/demo1/Main.kt
+6
-4
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt
View file @
f9daee03
...
@@ -11,7 +11,8 @@ enum class ListeningStatus {
...
@@ -11,7 +11,8 @@ enum class ListeningStatus {
STOPPED
STOPPED
}
}
/* 顶层方法 */
// region 顶层方法
inline
fun
<
reified
E
:
Event
>
subscribe
(
noinline
handler
:
suspend
(
E
)
->
ListeningStatus
)
=
E
::
class
.
subscribe
(
handler
)
inline
fun
<
reified
E
:
Event
>
subscribe
(
noinline
handler
:
suspend
(
E
)
->
ListeningStatus
)
=
E
::
class
.
subscribe
(
handler
)
...
@@ -30,7 +31,10 @@ inline fun <reified E : Event> subscribeWhileFalse(noinline listener: suspend (E
...
@@ -30,7 +31,10 @@ inline fun <reified E : Event> subscribeWhileFalse(noinline listener: suspend (E
inline
fun
<
reified
E
:
Event
>
subscribeWhileTrue
(
noinline
listener
:
suspend
(
E
)
->
Boolean
)
=
E
::
class
.
subscribeWhileTrue
(
listener
)
inline
fun
<
reified
E
:
Event
>
subscribeWhileTrue
(
noinline
listener
:
suspend
(
E
)
->
Boolean
)
=
E
::
class
.
subscribeWhileTrue
(
listener
)
inline
fun
<
reified
E
:
Event
>
subscribeWhileNull
(
noinline
listener
:
suspend
(
E
)
->
Any
?)
=
E
::
class
.
subscribeWhileNull
(
listener
)
inline
fun
<
reified
E
:
Event
>
subscribeWhileNull
(
noinline
listener
:
suspend
(
E
)
->
Any
?)
=
E
::
class
.
subscribeWhileNull
(
listener
)
/* KClass 的扩展方法, 不推荐 */
// endregion
// region KClass 的扩展方法 (不推荐)
fun
<
E
:
Event
>
KClass
<
E
>.
subscribe
(
handler
:
suspend
(
E
)
->
ListeningStatus
)
=
this
.
subscribeInternal
(
Handler
(
handler
))
fun
<
E
:
Event
>
KClass
<
E
>.
subscribe
(
handler
:
suspend
(
E
)
->
ListeningStatus
)
=
this
.
subscribeInternal
(
Handler
(
handler
))
...
@@ -49,7 +53,9 @@ fun <E : Event> KClass<E>.subscribeWhileFalse(listener: suspend (E) -> Boolean)
...
@@ -49,7 +53,9 @@ fun <E : Event> KClass<E>.subscribeWhileFalse(listener: suspend (E) -> Boolean)
fun
<
E
:
Event
>
KClass
<
E
>.
subscribeWhileTrue
(
listener
:
suspend
(
E
)
->
Boolean
)
=
subscribeWhile
(
true
,
listener
)
fun
<
E
:
Event
>
KClass
<
E
>.
subscribeWhileTrue
(
listener
:
suspend
(
E
)
->
Boolean
)
=
subscribeWhile
(
true
,
listener
)
fun
<
E
:
Event
>
KClass
<
E
>.
subscribeWhileNull
(
listener
:
suspend
(
E
)
->
Any
?)
=
subscribeWhile
(
null
,
listener
)
fun
<
E
:
Event
>
KClass
<
E
>.
subscribeWhileNull
(
listener
:
suspend
(
E
)
->
Any
?)
=
subscribeWhile
(
null
,
listener
)
/* ListenerBuilder DSL */
// endregion
// region ListenerBuilder DSL
/**
/**
* 监听一个事件. 可同时进行多种方式的监听
* 监听一个事件. 可同时进行多种方式的监听
...
@@ -105,3 +111,5 @@ inline class ListenerBuilder<out E : Event>(
...
@@ -105,3 +111,5 @@ inline class ListenerBuilder<out E : Event>(
fun
once
(
block
:
suspend
(
E
)
->
Unit
)
=
handler
{
block
(
it
);
ListeningStatus
.
STOPPED
}
fun
once
(
block
:
suspend
(
E
)
->
Unit
)
=
handler
{
block
(
it
);
ListeningStatus
.
STOPPED
}
}
}
// endregion
\ No newline at end of file
mirai-demos/mirai-demo-1/src/main/java/demo1/Main.kt
View file @
f9daee03
...
@@ -20,8 +20,8 @@ import kotlin.system.exitProcess
...
@@ -20,8 +20,8 @@ import kotlin.system.exitProcess
suspend
fun
main
()
{
suspend
fun
main
()
{
val
bot
=
Bot
(
BotAccount
(
//填写你的账号
val
bot
=
Bot
(
BotAccount
(
//填写你的账号
qqNumber
=
1994701
0
21
,
qqNumber
=
1994701
1
21
,
password
=
"a
sdhim188666
"
password
=
"a
bcdefg
"
),
Console
())
),
Console
())
bot
.
login
{
bot
.
login
{
...
@@ -65,7 +65,7 @@ suspend fun main() {
...
@@ -65,7 +65,7 @@ suspend fun main() {
}
}
}
}
//通过 KClass 扩展方式监听事件
//通过 KClass 扩展方式监听事件
(不推荐)
GroupMessageEvent
::
class
.
subscribeAlways
{
GroupMessageEvent
::
class
.
subscribeAlways
{
when
{
when
{
it
.
message
.
contains
(
"复读"
)
->
it
.
reply
(
it
.
message
)
it
.
message
.
contains
(
"复读"
)
->
it
.
reply
(
it
.
message
)
...
@@ -74,7 +74,7 @@ suspend fun main() {
...
@@ -74,7 +74,7 @@ suspend fun main() {
//DSL 监听
//DSL 监听
FriendMessageEvent
::
class
.
subscribeAll
{
subscribeAll
<
FriendMessageEvent
>
{
always
{
always
{
//获取第一个纯文本消息
//获取第一个纯文本消息
val
firstText
=
it
.
message
.
first
<
PlainText
>()
val
firstText
=
it
.
message
.
first
<
PlainText
>()
...
@@ -82,6 +82,8 @@ suspend fun main() {
...
@@ -82,6 +82,8 @@ suspend fun main() {
}
}
}
}
demo2
()
//由于使用的是协程, main函数执行完后就会结束程序.
//由于使用的是协程, main函数执行完后就会结束程序.
delay
(
Long
.
MAX_VALUE
)
//永远等待, 以测试事件
delay
(
Long
.
MAX_VALUE
)
//永远等待, 以测试事件
}
}
...
...
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