mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-26 20:20:14 +08:00
[console] Add StartupEvent and AutoLoginEvent (#2446)
* add: console event impl * add: internal annotation * add: since 2.15
This commit is contained in:
parent
26a3ddd505
commit
38e03fdc00
@ -1283,9 +1283,25 @@ public final class net/mamoe/mirai/console/data/java/JavaAutoSavePluginData$Comp
|
||||
public final fun createKType (Ljava/lang/Class;[Lkotlin/reflect/KType;)Lkotlin/reflect/KType;
|
||||
}
|
||||
|
||||
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 {
|
||||
}
|
||||
|
||||
public final class net/mamoe/mirai/console/events/AutoLoginEvent$Failure : net/mamoe/mirai/console/events/AutoLoginEvent {
|
||||
public fun getBot ()Lnet/mamoe/mirai/Bot;
|
||||
public final fun getCause ()Ljava/lang/Throwable;
|
||||
}
|
||||
|
||||
public final class net/mamoe/mirai/console/events/AutoLoginEvent$Success : net/mamoe/mirai/console/events/AutoLoginEvent {
|
||||
public fun getBot ()Lnet/mamoe/mirai/Bot;
|
||||
}
|
||||
|
||||
public abstract interface class net/mamoe/mirai/console/events/ConsoleEvent : net/mamoe/mirai/event/Event {
|
||||
}
|
||||
|
||||
public final class net/mamoe/mirai/console/events/StartupEvent : net/mamoe/mirai/event/AbstractEvent, net/mamoe/mirai/console/events/ConsoleEvent {
|
||||
public final fun getTimestamp ()J
|
||||
}
|
||||
|
||||
public abstract class net/mamoe/mirai/console/extension/AbstractExtensionPoint : net/mamoe/mirai/console/extension/ExtensionPoint {
|
||||
public fun <init> (Lkotlin/reflect/KClass;)V
|
||||
public fun getExtensionType ()Lkotlin/reflect/KClass;
|
||||
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2019-2023 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.console.events
|
||||
|
||||
import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.event.AbstractEvent
|
||||
import net.mamoe.mirai.console.internal.*
|
||||
import net.mamoe.mirai.event.events.BotEvent
|
||||
import net.mamoe.mirai.utils.MiraiInternalApi
|
||||
|
||||
/**
|
||||
* 自动登录执行后广播的事件
|
||||
* @property bot 登录的BOT
|
||||
* @see MiraiConsoleImplementationBridge.doStart
|
||||
* @since 2.15
|
||||
*/
|
||||
public sealed class AutoLoginEvent : BotEvent, ConsoleEvent, AbstractEvent() {
|
||||
/**
|
||||
* 登录成功
|
||||
*/
|
||||
public class Success @MiraiInternalApi constructor(
|
||||
override val bot: Bot
|
||||
) : AutoLoginEvent()
|
||||
|
||||
/**
|
||||
* 登录失败
|
||||
*/
|
||||
public class Failure @MiraiInternalApi constructor(
|
||||
override val bot: Bot,
|
||||
public val cause: Throwable
|
||||
) : AutoLoginEvent()
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2019-2023 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.console.events
|
||||
|
||||
import net.mamoe.mirai.console.extensions.PostStartupExtension
|
||||
import net.mamoe.mirai.event.AbstractEvent
|
||||
import net.mamoe.mirai.console.internal.*
|
||||
import net.mamoe.mirai.utils.MiraiInternalApi
|
||||
|
||||
/**
|
||||
* 在 Console 启动完成后广播的事件
|
||||
* @property timestamp 启动完成的时间戳
|
||||
* @see MiraiConsoleImplementationBridge.doStart
|
||||
* @see PostStartupExtension
|
||||
* @since 2.15
|
||||
*/
|
||||
public class StartupEvent @MiraiInternalApi constructor(
|
||||
public val timestamp: Long
|
||||
) : ConsoleEvent, AbstractEvent()
|
@ -13,6 +13,7 @@ package net.mamoe.mirai.console.internal
|
||||
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import me.him188.kotlin.dynamic.delegation.dynamicDelegation
|
||||
import net.mamoe.mirai.Bot
|
||||
@ -26,6 +27,8 @@ import net.mamoe.mirai.console.command.ConsoleCommandSender
|
||||
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
||||
import net.mamoe.mirai.console.command.parse.SpaceSeparatedCommandCallParser
|
||||
import net.mamoe.mirai.console.command.resolve.BuiltInCommandCallResolver
|
||||
import net.mamoe.mirai.console.events.AutoLoginEvent
|
||||
import net.mamoe.mirai.console.events.StartupEvent
|
||||
import net.mamoe.mirai.console.extensions.CommandCallParserProvider
|
||||
import net.mamoe.mirai.console.extensions.CommandCallResolverProvider
|
||||
import net.mamoe.mirai.console.extensions.PermissionServiceProvider
|
||||
@ -58,6 +61,7 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
||||
import net.mamoe.mirai.console.util.ConsoleInput
|
||||
import net.mamoe.mirai.console.util.SemVersion
|
||||
import net.mamoe.mirai.console.util.cast
|
||||
import net.mamoe.mirai.event.broadcast
|
||||
import net.mamoe.mirai.utils.*
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
@ -410,16 +414,29 @@ ___ ____ _ _____ _
|
||||
}
|
||||
}
|
||||
|
||||
runCatching { bot.login() }.getOrElse {
|
||||
runCatching {
|
||||
bot.login()
|
||||
}.onSuccess {
|
||||
launch {
|
||||
AutoLoginEvent.Success(bot = bot).broadcast()
|
||||
}
|
||||
}.onFailure {
|
||||
mainLogger.error(it)
|
||||
bot.close()
|
||||
launch {
|
||||
AutoLoginEvent.Failure(bot = bot, cause = it).broadcast()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
val startuped = currentTimeSeconds()
|
||||
phase("finally post") {
|
||||
launch {
|
||||
StartupEvent(timestamp = startuped).broadcast()
|
||||
}
|
||||
globalComponentStorage.useEachExtensions(PostStartupExtension) { it.invoke() }
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user