Skip to content

Commit 38e03fd

Browse files
authored
[console] Add StartupEvent and AutoLoginEvent (#2446)
* add: console event impl * add: internal annotation * add: since 2.15
1 parent 26a3ddd commit 38e03fd

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

mirai-console/backend/mirai-console/compatibility-validation/jvm/api/jvm.api

+16
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,25 @@ public final class net/mamoe/mirai/console/data/java/JavaAutoSavePluginData$Comp
12831283
public final fun createKType (Ljava/lang/Class;[Lkotlin/reflect/KType;)Lkotlin/reflect/KType;
12841284
}
12851285

1286+
public abstract class net/mamoe/mirai/console/events/AutoLoginEvent : net/mamoe/mirai/event/AbstractEvent, net/mamoe/mirai/console/events/ConsoleEvent, net/mamoe/mirai/event/events/BotEvent {
1287+
}
1288+
1289+
public final class net/mamoe/mirai/console/events/AutoLoginEvent$Failure : net/mamoe/mirai/console/events/AutoLoginEvent {
1290+
public fun getBot ()Lnet/mamoe/mirai/Bot;
1291+
public final fun getCause ()Ljava/lang/Throwable;
1292+
}
1293+
1294+
public final class net/mamoe/mirai/console/events/AutoLoginEvent$Success : net/mamoe/mirai/console/events/AutoLoginEvent {
1295+
public fun getBot ()Lnet/mamoe/mirai/Bot;
1296+
}
1297+
12861298
public abstract interface class net/mamoe/mirai/console/events/ConsoleEvent : net/mamoe/mirai/event/Event {
12871299
}
12881300

1301+
public final class net/mamoe/mirai/console/events/StartupEvent : net/mamoe/mirai/event/AbstractEvent, net/mamoe/mirai/console/events/ConsoleEvent {
1302+
public final fun getTimestamp ()J
1303+
}
1304+
12891305
public abstract class net/mamoe/mirai/console/extension/AbstractExtensionPoint : net/mamoe/mirai/console/extension/ExtensionPoint {
12901306
public fun <init> (Lkotlin/reflect/KClass;)V
12911307
public fun getExtensionType ()Lkotlin/reflect/KClass;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019-2023 Mamoe Technologies and contributors.
3+
*
4+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
6+
*
7+
* https://github.com/mamoe/mirai/blob/dev/LICENSE
8+
*/
9+
10+
package net.mamoe.mirai.console.events
11+
12+
import net.mamoe.mirai.Bot
13+
import net.mamoe.mirai.event.AbstractEvent
14+
import net.mamoe.mirai.console.internal.*
15+
import net.mamoe.mirai.event.events.BotEvent
16+
import net.mamoe.mirai.utils.MiraiInternalApi
17+
18+
/**
19+
* 自动登录执行后广播的事件
20+
* @property bot 登录的BOT
21+
* @see MiraiConsoleImplementationBridge.doStart
22+
* @since 2.15
23+
*/
24+
public sealed class AutoLoginEvent : BotEvent, ConsoleEvent, AbstractEvent() {
25+
/**
26+
* 登录成功
27+
*/
28+
public class Success @MiraiInternalApi constructor(
29+
override val bot: Bot
30+
) : AutoLoginEvent()
31+
32+
/**
33+
* 登录失败
34+
*/
35+
public class Failure @MiraiInternalApi constructor(
36+
override val bot: Bot,
37+
public val cause: Throwable
38+
) : AutoLoginEvent()
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2019-2023 Mamoe Technologies and contributors.
3+
*
4+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
6+
*
7+
* https://github.com/mamoe/mirai/blob/dev/LICENSE
8+
*/
9+
10+
package net.mamoe.mirai.console.events
11+
12+
import net.mamoe.mirai.console.extensions.PostStartupExtension
13+
import net.mamoe.mirai.event.AbstractEvent
14+
import net.mamoe.mirai.console.internal.*
15+
import net.mamoe.mirai.utils.MiraiInternalApi
16+
17+
/**
18+
* 在 Console 启动完成后广播的事件
19+
* @property timestamp 启动完成的时间戳
20+
* @see MiraiConsoleImplementationBridge.doStart
21+
* @see PostStartupExtension
22+
* @since 2.15
23+
*/
24+
public class StartupEvent @MiraiInternalApi constructor(
25+
public val timestamp: Long
26+
) : ConsoleEvent, AbstractEvent()

mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package net.mamoe.mirai.console.internal
1313

1414
import kotlinx.coroutines.CoroutineExceptionHandler
1515
import kotlinx.coroutines.Job
16+
import kotlinx.coroutines.launch
1617
import kotlinx.coroutines.runBlocking
1718
import me.him188.kotlin.dynamic.delegation.dynamicDelegation
1819
import net.mamoe.mirai.Bot
@@ -26,6 +27,8 @@ import net.mamoe.mirai.console.command.ConsoleCommandSender
2627
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
2728
import net.mamoe.mirai.console.command.parse.SpaceSeparatedCommandCallParser
2829
import net.mamoe.mirai.console.command.resolve.BuiltInCommandCallResolver
30+
import net.mamoe.mirai.console.events.AutoLoginEvent
31+
import net.mamoe.mirai.console.events.StartupEvent
2932
import net.mamoe.mirai.console.extensions.CommandCallParserProvider
3033
import net.mamoe.mirai.console.extensions.CommandCallResolverProvider
3134
import net.mamoe.mirai.console.extensions.PermissionServiceProvider
@@ -58,6 +61,7 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
5861
import net.mamoe.mirai.console.util.ConsoleInput
5962
import net.mamoe.mirai.console.util.SemVersion
6063
import net.mamoe.mirai.console.util.cast
64+
import net.mamoe.mirai.event.broadcast
6165
import net.mamoe.mirai.utils.*
6266
import java.time.Instant
6367
import java.time.ZoneId
@@ -410,16 +414,29 @@ ___ ____ _ _____ _
410414
}
411415
}
412416

413-
runCatching { bot.login() }.getOrElse {
417+
runCatching {
418+
bot.login()
419+
}.onSuccess {
420+
launch {
421+
AutoLoginEvent.Success(bot = bot).broadcast()
422+
}
423+
}.onFailure {
414424
mainLogger.error(it)
415425
bot.close()
426+
launch {
427+
AutoLoginEvent.Failure(bot = bot, cause = it).broadcast()
428+
}
416429
}
417430
}
418431

419432
}
420433
}
421434

435+
val startuped = currentTimeSeconds()
422436
phase("finally post") {
437+
launch {
438+
StartupEvent(timestamp = startuped).broadcast()
439+
}
423440
globalComponentStorage.useEachExtensions(PostStartupExtension) { it.invoke() }
424441
}
425442

0 commit comments

Comments
 (0)