Skip to content

Commit 3679233

Browse files
authored
Merge pull request #1079 from simple-robot/init-bot-api
feat(bot): introduce `ConfigurableBot` and `InitializableBot` interfaces
2 parents 7c3c57e + a3e836a commit 3679233

File tree

4 files changed

+148
-2
lines changed

4 files changed

+148
-2
lines changed

simbot-api/api/simbot-api.api

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ public final class love/forte/simbot/bot/Bots {
445445
public static final fun startIn (Llove/forte/simbot/bot/Bot;Lkotlinx/coroutines/CoroutineScope;)Lkotlinx/coroutines/Job;
446446
}
447447

448+
public abstract interface class love/forte/simbot/bot/ConfigurableBot : love/forte/simbot/bot/Bot {
449+
public abstract fun getConfiguration ()Ljava/lang/Object;
450+
}
451+
448452
public class love/forte/simbot/bot/ConflictBotException : love/forte/simbot/bot/BotException {
449453
public fun <init> ()V
450454
public fun <init> (Ljava/lang/String;)V
@@ -488,6 +492,15 @@ public abstract interface class love/forte/simbot/bot/GuildRelation {
488492
public abstract synthetic fun guildCount (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
489493
}
490494

495+
public abstract interface class love/forte/simbot/bot/InitializableBot : love/forte/simbot/bot/ConfigurableBot {
496+
public abstract synthetic fun init (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
497+
public fun initAsync ()Ljava/util/concurrent/CompletableFuture;
498+
public fun initBlocking ()Z
499+
public fun initReserve ()Llove/forte/simbot/suspendrunner/reserve/SuspendReserve;
500+
public abstract fun isInitialized ()Z
501+
public abstract fun isInitializing ()Z
502+
}
503+
491504
public abstract class love/forte/simbot/bot/JobBasedBot : love/forte/simbot/bot/Bot {
492505
public fun <init> ()V
493506
public fun cancel (Ljava/lang/Throwable;)V

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024. ForteScarlet.
2+
* Copyright (c) 2024-2025. ForteScarlet.
33
*
44
* Project https://github.com/simple-robot/simpler-robot
55
@@ -27,7 +27,6 @@ package love.forte.simbot.bot
2727

2828
import kotlinx.coroutines.CoroutineScope
2929
import kotlinx.coroutines.Job
30-
import kotlinx.coroutines.NonCancellable.join
3130
import kotlinx.coroutines.cancel
3231
import kotlinx.coroutines.launch
3332
import love.forte.simbot.ability.CompletionAware
@@ -55,6 +54,16 @@ import kotlin.jvm.JvmName
5554
* 它们可以提供针对 [Guild]、[ChatGroup]、[Contact] 等常见行为对象的操作。
5655
* [Bot] 的实现者也许会根据不同的平台扩展这些行为类型或提供更多的行为类型。
5756
*
57+
* ## 扩展类型
58+
*
59+
* 对于组件实现者来说,[Bot] 有一些具有特殊含义的额外拓展类型。
60+
* 可参考:
61+
* - [ConfigurableBot]
62+
* - [InitializableBot]
63+
*
64+
* @see ConfigurableBot
65+
* @see InitializableBot
66+
*
5867
* @author ForteScarlet
5968
*/
6069
public interface Bot : IDContainer, LifecycleAware, CompletionAware, CoroutineScope, BotRelations {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2025. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.bot
25+
26+
27+
/**
28+
* 一个拥有配置信息的 [Bot]。
29+
* 配置信息的具体类型由实现者决定。
30+
*
31+
* 配置信息类可能是一个可变类型,当使用它构建 [Bot] 后,它理应不再被修改。
32+
* 如果实现了 [InitializableBot],则直到 [init][InitializableBot.init] 调用前,配置信息 [configuration] 中的信息可以被修改。
33+
*
34+
* 配置类的内容也许在上述要求“不可修改”的情况下进行修改时不会产生异常(例如在 [init][InitializableBot.init]后继续修改),
35+
* 但是它的修改可能不会生效,或可能引发任何不可预知的错误或埋下隐患。
36+
*
37+
* ## 特殊含义
38+
* [ConfigurableBot] 具有特殊含义,可能会在部分场景被判断并有特殊作用。
39+
*
40+
* @see Bot
41+
* @see InitializableBot
42+
*
43+
* @since 4.13.0
44+
*
45+
* @author ForteScarlet
46+
*/
47+
public interface ConfigurableBot : Bot {
48+
/**
49+
* 当前 Bot 持有的配置信息。
50+
*/
51+
public val configuration: Any
52+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2025. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.bot
25+
26+
import love.forte.simbot.suspendrunner.ST
27+
28+
29+
/**
30+
* 一个可初始化的 [Bot],使其用于**延迟初始化**的能力。
31+
* 实现了 [InitializableBot] 的 Bot 类型约定,在 [init] 之前,
32+
* 可以继续修改 Bot 中的一些可修改信息,例如Bot所提供的配置类等。
33+
*
34+
* ## 特殊含义
35+
* [ConfigurableBot] 具有特殊含义,可能会在部分场景被判断并有特殊作用。
36+
*
37+
* - 在 Spring Boot 中,
38+
*
39+
* @since 4.13.0
40+
*
41+
* @author ForteScarlet
42+
*/
43+
public interface InitializableBot : ConfigurableBot {
44+
45+
/**
46+
* 初始化当前的 bot。当初始化完成后,[isInitialized] 将会得到 `true`,
47+
* 且 [configuration] 中的属性不应再被修改。
48+
*
49+
* [init] 同样会在 [start] 的过程中被自动初始化,因此不需要手动在 [start] 之前调用 [init]。
50+
* 你只需要在只需要初始化而不启动 bot 的情况下主动调用 [init]。
51+
*
52+
* [init] 可以被重复调用,但最终效果是一致的 —— bot只会被初始化一次。
53+
* 当初始化完成后,后续的其他调用会直接返回 `false`。[init] 内部需通过锁或其他手段来保证这一点。
54+
* 当 [init] 尚在初始化过程中持有锁时,[isInitializing] 将会得到 `true`。
55+
*
56+
* @return 如果本次初始化成功,则得到 `true`, 如果已经初始化过了,则得到 `false`。
57+
*/
58+
@ST
59+
public suspend fun init(): Boolean
60+
61+
/**
62+
* 当前 bot 是否已经完成初始化。
63+
*/
64+
public val isInitialized: Boolean
65+
66+
/**
67+
* 是否正在初始化。
68+
* 当 [isInitialized] 为 `false` 且 [init] 内部正在进行初始化并持有锁时,
69+
* [isInitializing] 得到 `false`。
70+
*/
71+
public val isInitializing: Boolean
72+
}

0 commit comments

Comments
 (0)