Skip to content

Commit

Permalink
[console] Add StartupEvent and AutoLoginEvent (#2446)
Browse files Browse the repository at this point in the history
* add: console event impl

* add: internal annotation

* add: since 2.15
  • Loading branch information
cssxsh authored Feb 19, 2023
1 parent 26a3ddd commit 38e03fd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions mirai-console/backend/mirai-console/src/events/AutoLoginEvent.kt
Original file line number Diff line number Diff line change
@@ -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()
}
26 changes: 26 additions & 0 deletions mirai-console/backend/mirai-console/src/events/StartupEvent.kt
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() }
}

Expand Down

0 comments on commit 38e03fd

Please sign in to comment.