Skip to content

Commit 97e72ce

Browse files
authored
Merge pull request #870 from simple-robot/more-extension-api
提供更多Application和Bot的辅助扩展API
2 parents de95a9c + 7e1cd27 commit 97e72ce

File tree

6 files changed

+89
-31
lines changed

6 files changed

+89
-31
lines changed

README.md

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,41 @@ simbot的**平台功能**由组件驱动,安装不同的组件库来获得不
6767

6868
```Kotlin
6969
suspend fun main() {
70-
val application = launchSimpleApplication {
71-
// 安装KOOK和QQ频道组件库
72-
useKook()
73-
useQQGuild()
74-
}
75-
76-
application.kookBots {
77-
// ... 注册kook bot,并在此之后可处理到kook的相关事件
70+
launchSimpleApplication { config() }
71+
.joinWith { module() }
72+
}
73+
74+
fun ApplicationFactoryConfigurer<*, *, *>.config() {
75+
// 安装KOOK和QQ频道组件库
76+
useKook()
77+
useQQGuild()
78+
}
79+
80+
/**
81+
* 对已经构建完成的 `Application` 进行配置于应用
82+
*/
83+
suspend fun Application.module() {
84+
registerBots()
85+
registerListeners()
86+
}
87+
88+
/**
89+
* 注册所需的bot
90+
*/
91+
suspend fun Application.registerBots() {
92+
// ... 注册kook bot,并在此之后可处理到kook的相关事件
93+
kookBots {
7894
register(...) { ... }.start()
7995
}
80-
application.qqGuildBots {
81-
// ... 注册QQ频道bot,并在此之后可处理到QQ频道的相关事件
96+
97+
// ... 注册QQ频道bot,并在此之后可处理到QQ频道的相关事件
98+
qqGuildBots {
8299
register(...) { ... }.start()
83100
}
84-
85-
// 注册各种事件处理器
86-
application.listeners {
101+
}
102+
103+
fun Application.registerListeners() {
104+
listeners {
87105
// 注册一个事件处理器
88106
// ChatChannelMessageEvent 是由simbot API定义的泛用类型,代表所有子频道消息事件
89107
// 其中就包括QQ频道的公域消息事件, 或者KOOK的频道消息事件
@@ -121,9 +139,6 @@ suspend fun main() {
121139
- [应用手册][doc-homepage]
122140
- [文档引导站&API文档](https://docs.simbot.forte.love)
123141

124-
125-
> 切换分支到 [v3-dev](https://github.com/simple-robot/simpler-robot/tree/v3-dev) 可查看 simbot v3 的历史分支。
126-
127142
## 协助我们
128143
为我们点亮一个 **✨star🌟** 便是能够给予我们继续走下去的最大动力与支持!
129144

simbot-api/api/simbot-api.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public abstract interface class love/forte/simbot/application/ApplicationLaunche
267267

268268
public final class love/forte/simbot/application/Applications {
269269
public static final fun asCompletableFuture (Llove/forte/simbot/application/Application;)Ljava/util/concurrent/CompletableFuture;
270+
public static final fun joinWith (Llove/forte/simbot/application/Application;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
270271
public static final synthetic fun launchApplication (Llove/forte/simbot/application/ApplicationFactory;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
271272
public static synthetic fun launchApplication$default (Llove/forte/simbot/application/ApplicationFactory;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
272273
public static final fun launchApplicationAsync (Lkotlinx/coroutines/CoroutineScope;Llove/forte/simbot/application/ApplicationFactory;)Llove/forte/simbot/common/async/Async;
@@ -417,6 +418,11 @@ public abstract interface class love/forte/simbot/bot/BotRelations {
417418
public abstract fun getGuildRelation ()Llove/forte/simbot/bot/GuildRelation;
418419
}
419420

421+
public final class love/forte/simbot/bot/Bots {
422+
public static final fun startAndJoin (Llove/forte/simbot/bot/Bot;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
423+
public static final fun startIn (Llove/forte/simbot/bot/Bot;Lkotlinx/coroutines/CoroutineScope;)Lkotlinx/coroutines/Job;
424+
}
425+
420426
public class love/forte/simbot/bot/ConflictBotException : love/forte/simbot/bot/BotException {
421427
public fun <init> ()V
422428
public fun <init> (Ljava/lang/String;)V

simbot-api/src/commonMain/kotlin/love/forte/simbot/application/Application.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -129,3 +129,18 @@ public inline fun Application.listeners(block: EventListenerRegistrar.() -> Unit
129129
eventDispatcher.block()
130130
}
131131

132+
/**
133+
* 执行完 [block] 后挂起当前 [Application]。
134+
*
135+
* ```kotlin
136+
* app.joinWith { // this: Application
137+
* // ...
138+
* }
139+
* ```
140+
*
141+
* @see Application.join
142+
*/
143+
public suspend inline fun <T : Application> T.joinWith(block: T.() -> Unit) {
144+
block()
145+
join()
146+
}

simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/Bot.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
*
2222
*/
2323

24+
@file:JvmName("Bots")
25+
2426
package love.forte.simbot.bot
2527

2628
import kotlinx.coroutines.CoroutineScope
29+
import kotlinx.coroutines.Job
2730
import kotlinx.coroutines.cancel
31+
import kotlinx.coroutines.launch
2832
import love.forte.simbot.ability.CompletionAware
2933
import love.forte.simbot.ability.LifecycleAware
3034
import love.forte.simbot.common.collectable.Collectable
@@ -37,6 +41,7 @@ import love.forte.simbot.definition.Contact
3741
import love.forte.simbot.definition.Guild
3842
import love.forte.simbot.suspendrunner.ST
3943
import love.forte.simbot.suspendrunner.STP
44+
import kotlin.jvm.JvmName
4045

4146
/**
4247
* 一个 `Bot`。
@@ -275,3 +280,22 @@ public interface ContactRelation {
275280
@STP
276281
public suspend fun contactCount(): Int
277282
}
283+
284+
/**
285+
* 启动当前 [Bot] 后挂起。
286+
*
287+
* @see Bot.start
288+
* @see Bot.join
289+
*/
290+
public suspend fun Bot.startAndJoin() {
291+
start()
292+
join()
293+
}
294+
295+
/**
296+
* 通过 [scope] 在异步中启动 [Bot]。
297+
*
298+
* @see Bot.start
299+
*/
300+
public fun Bot.startIn(scope: CoroutineScope): Job =
301+
scope.launch { start() }

simbot-api/src/commonMain/kotlin/love/forte/simbot/message/Messages.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ public fun StringFormat.encodeMessagesToString(messages: Messages): String =
427427
public fun StringFormat.decodeMessagesFromString(string: String): Messages =
428428
decodeFromString(Messages.serializer, string)
429429

430-
431-
// TODO delete on stable version
430+
/**
431+
* @suppress 仅针对v4.0.0-dev16及以下的版本的JVM二进制兼容
432+
*/
432433
@Suppress("FunctionName")
433434
@Deprecated("仅供临时针对v4.0.0-dev16及以下的版本的JVM二进制兼容", level = DeprecationLevel.HIDDEN)
434435
public object MessagesKt {

simbot-commons/simbot-common-atomic/src/commonTest/kotlin/AtomicTests.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
*
2222
*/
2323

24-
import kotlinx.coroutines.Dispatchers
25-
import kotlinx.coroutines.coroutineScope
26-
import kotlinx.coroutines.launch
24+
import kotlinx.coroutines.*
2725
import kotlinx.coroutines.test.runTest
28-
import kotlinx.coroutines.withContext
2926
import love.forte.simbot.common.atomic.*
3027
import kotlin.test.*
3128

@@ -139,14 +136,12 @@ class AtomicTests {
139136
@Test
140137
fun compareAsyncTest() = runTest {
141138
val times = 1000
142-
withContext(Dispatchers.Default) {
143-
coroutineScope {
144-
launch(Dispatchers.Default) { checkAtomicInt(times) }
145-
launch(Dispatchers.Default) { checkAtomicLong(times) }
146-
launch(Dispatchers.Default) { checkAtomicUInt(times) }
147-
launch(Dispatchers.Default) { checkAtomicULong(times) }
148-
launch(Dispatchers.Default) { checkAtomicRef(times) }
149-
}
139+
coroutineScope {
140+
checkAtomicInt(times)
141+
checkAtomicLong(times)
142+
checkAtomicUInt(times)
143+
checkAtomicULong(times)
144+
checkAtomicRef(times)
150145
}
151146
}
152147

@@ -159,13 +154,15 @@ class AtomicTests {
159154
launch {
160155
atomic += 1
161156
}
157+
yield()
162158
}
163159
}
164160
launch {
165161
repeat(times) {
166162
launch {
167163
atomic.update { it + 1 }
168164
}
165+
yield()
169166
}
170167
}
171168
}

0 commit comments

Comments
 (0)