Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: console event impl #2446

Merged
merged 4 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 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,32 @@
/*
* 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

/**
* 自动登陆执行后广播的指令
* @property bot 登录的BOT
* @see MiraiConsoleImplementationBridge.doStart
*/
public sealed class AutoLoginEvent : BotEvent, ConsoleEvent, AbstractEvent() {
/**
* 登录成功
*/
public class Success(override val bot: Bot): AutoLoginEvent()

/**
* 登录失败
*/
public class Failure(override val bot: Bot, public val cause: Throwable): AutoLoginEvent()
}
22 changes: 22 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,22 @@
/*
* 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.*

/**
* 在 Console 启动完成后广播的事件
* @property timestamp 启动完成的时间戳
* @see MiraiConsoleImplementationBridge.doStart
* @see PostStartupExtension
*/
public class StartupEvent(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