Skip to content

Commit

Permalink
print warning when bot not found
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Feb 27, 2024
1 parent 3ecc323 commit 5c377e2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {

init {
_instance = this
addGroupListeners()
addFriendListeners()
addGuildListeners()

// 暂定禁止 mirai-console 的终端用户须知,它可能已不适用于 Overflow
try {
Class.forName("net.mamoe.mirai.console.enduserreadme.EndUserReadme")
Expand All @@ -132,11 +136,6 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
injectMiraiConsole()
} catch (ignored: ClassNotFoundException) {
}
EventBus.apply {
addGroupListeners()
addFriendListeners()
addGuildListeners()
}
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
Expand Down Expand Up @@ -189,21 +188,16 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
if (reversed) {
val ws = service.createWebsocketServerAndWaitConnect(this)
if (ws == null) {
if (printInfo) {
logger.error("未连接到 Onebot")
if (miraiConsole && !isNotExit) exitProcess(1)
}
if (printInfo) logger.error("未连接到 Onebot")
if (miraiConsole && !isNotExit) exitProcess(1)
return null
}
botImpl = ws.second
} else {
val ws = service.createWebsocketClient(this)
if (ws == null) {
if (printInfo) {
logger.error("未连接到 Onebot")
if (miraiConsole && !isNotExit) exitProcess(1)

}
if (printInfo) logger.error("未连接到 Onebot")
if (miraiConsole && !isNotExit) exitProcess(1)
return null
}
botImpl = ws.createBot().also { BotFactoryImpl.internalBot = it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import top.mrxiaom.overflow.internal.utils.queryProfile
import top.mrxiaom.overflow.internal.utils.wrapAsFriend
import top.mrxiaom.overflow.internal.utils.wrapAsStranger

internal fun EventBus.addFriendListeners() {
internal fun addFriendListeners() {
listOf(
FriendMessageListener(),
FriendAddRequestListener(),
FriendMessageRecallListener(),

).forEach(::addListener)
).forEach(EventBus::addListener)
}

internal class FriendMessageListener : EventListener<PrivateMessageEvent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import top.mrxiaom.overflow.internal.utils.group
import top.mrxiaom.overflow.internal.utils.queryProfile
import top.mrxiaom.overflow.internal.utils.wrapAsMember

internal fun EventBus.addGroupListeners() {
internal fun addGroupListeners() {
listOf(
GroupMessageListener(),
GroupNotifyListener(),
Expand All @@ -29,7 +29,7 @@ internal fun EventBus.addGroupListeners() {
GroupTitleChangeNoticeListener(),
GroupBanNoticeListener(),

).forEach(::addListener)
).forEach(EventBus::addListener)
}

internal class GroupMessageListener : EventListener<GroupMessageEvent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import top.mrxiaom.overflow.internal.contact.BotWrapper
import top.mrxiaom.overflow.internal.message.OnebotMessages
import top.mrxiaom.overflow.internal.utils.bot

internal fun EventBus.addGuildListeners() {
internal fun addGuildListeners() {
listOf(
GuildMessageListener(),

).forEach(::addListener)
).forEach(EventBus::addListener)
}

internal class GuildMessageListener : EventListener<GuildMessageEvent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import top.mrxiaom.overflow.internal.contact.BotWrapper
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import kotlin.reflect.jvm.jvmName


internal val defaultJson: Json = Json {
Expand Down Expand Up @@ -101,4 +102,10 @@ internal suspend inline fun <reified T : Any> BotWrapper.queryProfile(
}

internal val Event.bot: BotWrapper?
get() = Bot.getInstanceOrNull(selfId)?.asOnebot
get() {
val botWrapper = Bot.getInstanceOrNull(selfId)?.asOnebot
if (botWrapper == null) {
Overflow.logger.warning("接收到的 ${this::class.jvmName} 事件中,selfId = $selfId,无法从已登录的机器人列表中找到该机器人。")
}
return botWrapper
}

0 comments on commit 5c377e2

Please sign in to comment.