mamoe / mirai

高效率 QQ 机器人支持库
https://mirai.mamoe.net
GNU Affero General Public License v3.0
14.33k stars 2.53k forks source link

Bot.nick 可能在 login 结束前都还没有initialize #1023

Closed Nambers closed 3 years ago

Nambers commented 3 years ago
java.lang.IllegalStateException: 在 Bot login 时遇到了意料之中的问题. 请完整复制此日志提交给 mirai: https://github.com/mamoe/mirai/issues/new    调试信息: kotlin.UninitializedPropertyAccessException: lateinit property nick has not been initialized
        at net.mamoe.mirai.internal.message.ContextualBugReportExceptionKt.contextualBugReportException(contextualBugReportException.kt:23)
        at net.mamoe.mirai.internal.message.ContextualBugReportExceptionKt.contextualBugReportException$default(contextualBugReportException.kt:21)
        at net.mamoe.mirai.internal.AbstractBot$Login.doLogin(AbstractBot.kt:297)
        at net.mamoe.mirai.internal.AbstractBot$Login$doLogin$1.invokeSuspend(AbstractBot.kt)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.internal.ScopeCoroutine.afterResume(Scopes.kt:33)
        at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:113)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:86)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:61)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at org.example.mirai.plugin.KotlinMainKt.main(KotlinMain.kt:362)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property nick has not been initialized
        at net.mamoe.mirai.internal.QQAndroidBot.getNick(QQAndroidBot.kt:121)
        at net.mamoe.mirai.internal.AbstractBot$Login.doLogin(AbstractBot.kt:295)

复现

这里复现比较麻烦,我是使用mirai-hello-world然后用shadowJar打包成jar然后用cmd启动 以下为我完整启动日志:

正在启动
机器人qqid:692928873
机器人qq密码#######
c++部分dll存放地址#######\build\libs\mirai-demo.dll
启动成功!
github存储库:https://github.com/Nambers/MiraiCP
2021-02-15 14:20:35 I/Mirai: Mirai 正在使用桌面环境. 如遇到验证码将会弹出对话框. 可添加 JVM 属性 `mirai.no-desktop` 以关闭.
2021-02-15 14:20:35 I/Mirai: Mirai is using desktop. Captcha will be thrown by window popup. You can add `mirai.no-desktop` to JVM properties (-Dmirai.no-desktop) to disable it.
2021-02-15 14:20:36 I/Bot 692928873: Logging in...
2021-02-15 14:20:36 V/Net 692928873: Loading BdhSession from cache file
2021-02-15 14:20:36 V/Net 692928873: Loading server list from cache.
2021-02-15 14:20:36 I/Net 692928873: Connected to server 113.96.12.217:14000
2021-02-15 14:20:36 V/Net 692928873: Send: wtlogin.login
2021-02-15 14:20:36 V/Net 692928873: Recv: LoginPacketResponse.Success
2021-02-15 14:20:36 I/Net 692928873: Login successful
2021-02-15 14:20:36 V/Net 692928873: Send: StatSvc.register
2021-02-15 14:20:37 V/Net 692928873: Recv: Response(StatSvc.register)
2021-02-15 14:20:37 I/Net 692928873: Scheduled key refresh in 5h 55min 0.0s.
2021-02-15 14:20:37 V/Net 692928873: Send: StatSvc.register
2021-02-15 14:20:37 V/Net 692928873: Recv: Response(StatSvc.register)
2021-02-15 14:20:37 I/Net 692928873: Awaiting ConfigPushSvc.PushReq.
2021-02-15 14:20:37 I/Net 692928873: Syncing friend message history...
2021-02-15 14:20:37 I/Net 692928873: Start loading friend list...
2021-02-15 14:20:37 I/Net 692928873: Start loading stranger list...
2021-02-15 14:20:37 V/Net 692928873: Send: MessageSvc.PbGetMsg
2021-02-15 14:20:37 V/Net 692928873: Send: StatSvc.GetDevLoginInfo
2021-02-15 14:20:37 V/Net 692928873: Send: friendlist.getFriendGroupList
2021-02-15 14:20:37 V/Net 692928873: Send: OidbSvc.0x496
2021-02-15 14:20:37 V/Net 692928873: Send: OidbSvc.0x5d2_0
2021-02-15 14:20:37 I/Net 692928873: Syncing friend message history: Success.
2021-02-15 14:20:37 V/Net 692928873: Recv: TroopManagement.GetTroopConfig.Response(true)
2021-02-15 14:20:37 I/Net 692928873: Start loading group list...
2021-02-15 14:20:37 V/Net 692928873: Send: friendlist.GetTroopListReqV2
2021-02-15 14:20:37 V/Net 692928873: Recv: StrangerList.GetStrangerList.Response(result=0)
2021-02-15 14:20:37 V/Net 692928873: Recv: StatSvc.GetDevLoginInfo.Response(deviceList.size=1)
2021-02-15 14:20:37 I/Bot 692928873: No OtherClient online.
2021-02-15 14:20:37 I/Net 692928873: Successfully loaded stranger list: 0 in total
2021-02-15 14:20:37 V/Net 692928873: Recv: FriendList.GetFriendGroupList.Response
Exception in thread "main" 2021-02-15 14:20:40 V/Net 692928873: Recv: ConfigPushSvc.PushReq.PushReqResponse.ConfigPush
2021-02-15 14:20:40 D/Net 692928873: Received unknown commandName: ConfigPushSvc.PushDomain
2021-02-15 14:20:37 V/Net 692928873: Recv: FriendList.GetFriendGroupList.Response
java.lang.IllegalStateException: 在 Bot login 时遇到了意料之中的问题. 请完整复制此日志提交给 mirai: https://github.com/mamoe/mirai/issues/new    调试信息: kotlin.UninitializedPropertyAccessException: lateinit property nick has not been initialized
        at net.mamoe.mirai.internal.message.ContextualBugReportExceptionKt.contextualBugReportException(contextualBugReportException.kt:23)
        at net.mamoe.mirai.internal.message.ContextualBugReportExceptionKt.contextualBugReportException$default(contextualBugReportException.kt:21)
        at net.mamoe.mirai.internal.AbstractBot$Login.doLogin(AbstractBot.kt:297)
        at net.mamoe.mirai.internal.AbstractBot$Login$doLogin$1.invokeSuspend(AbstractBot.kt)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.internal.ScopeCoroutine.afterResume(Scopes.kt:33)
        at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:113)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:86)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:61)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at org.example.mirai.plugin.KotlinMainKt.main(KotlinMain.kt:362)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property nick has not been initialized
        at net.mamoe.mirai.internal.QQAndroidBot.getNick(QQAndroidBot.kt:121)
        at net.mamoe.mirai.internal.AbstractBot$Login.doLogin(AbstractBot.kt:295)
        ... 13 more
2021-02-15 14:20:42 V/Net 692928873: Saving bdh session to cache
2021-02-15 14:20:42 V/Net 692928873: Send: ConfigPushSvc.PushResp
2021-02-15 14:20:42 I/Net 692928873: ConfigPushSvc.PushReq: Config updated.

因为我完整项目全部代码加起来可能超过1k行,在这我就截取登陆部分代码,如果有需要再发

//这个是入口点
fun main(args: Array<String>){
    // qqid, passworld, dllpath
    if(args.size != 3){
        println("参数不足或多余,请提供[botqqid, password, dllpath]")
        return
    }
    println("正在启动\n机器人qqid:${args[0]}\n机器人qq密码${args[1]}\nc++部分dll存放地址${args[2]}")
    runBlocking {
        try {
            KotlinMain.main(valueOf(args[0]), args[1], args[2])
        }catch (e:NumberFormatException){
            println("${args[0]}不是一个有效的qq号数字")
            return@runBlocking
        }
    }
}

...
//这个是启动部分
suspend fun main(id:Long, pass:String, path:String){
        dll_name = path
        logger.info("启动成功!")
        logger.info("github存储库:https://github.com/Nambers/MiraiCP")
        if (!File(dll_name).exists()) {
            logger.error("文件$dll_name 不存在")
            return
        }
        val bot = BotFactory.newBot(id, pass) {
            fileBasedDeviceInfo()
        }.alsoLogin()
        cpp = CPP_lib()
        val gson = Gson()
        val globalEventChannel = bot.eventChannel
        logger.info(cpp.ver)//输出2333 正常
        globalEventChannel.subscribeAlways<BotOnlineEvent> {
...

版本

补充信息

Nambers commented 3 years ago

此外,日志部分乱码好像是因为启动的日志的颜色,那是在哪关掉吗hhhh还是有其他的解决方法 然后这里面的logger是我重写了,代码如下

object logger{
    fun info(t:String){
        println(t)
    }

    fun warning(t:String){
        println(t)
    }

    fun error(t:String){
        println(t)
    }
}
Nambers commented 3 years ago

此外,这个异常我本身无法复现,我重开了1次就正常启动了

Nambers commented 3 years ago

欸没有nick不是在2.4.0解决了? https://github.com/mamoe/mirai/issues/1019

AdoptOSS commented 3 years ago

关闭Logger打印Ansi颜色代码:

MiraiLogger.setDefaultLoggerCreator { identity ->
    PlatformLogger(identity, ::println, false)
}

更建议配合jansi自动选择合适的方式处理Ansi代码(Windows下转换成Console API调用,不支持的情况下自动转换为纯文字的输出)

dependencies {
    implementation("org.fusesource.jansi:jansi:1.18")
}
import org.fusesource.jansi.AnsiConsole
MiraiLogger.setDefaultLoggerCreator { identity ->
    PlatformLogger(identity, AnsiConsole.out::println, true)
}
Karlatemp commented 3 years ago

~看上去像是收到 FriendList.GetFriendGroupList.Response 前login步骤就已经完成了,应该得等到至少收到一个 response 再结束login~ 应该是服务器没有下发 selfInfo

Nambers commented 3 years ago

此外,还遇到了个问题

2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetFriendGroupList.Response
2021-02-15 14:56:32 V/Net 692928873: Send: friendlist.GetTroopMemberListReq
2021-02-15 14:56:32 V/Net 692928873: Send: friendlist.GetTroopMemberListReq
2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetTroopMemberList.Response
2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetTroopMemberList.Response
2021-02-15 14:56:32 I/Net 692928873: Successfully loaded group list: 2 in total.
2021-02-15 14:56:32 I/Bot 692928873: Login successful
2021-02-15 14:56:32 I/Bot 692928873: 2333
2021-02-15 14:56:34 D/Net 692928873: Received unknown commandName: ConfigPushSvc.PushDomain
2021-02-15 14:56:34 V/Net 692928873: Recv: ConfigPushSvc.PushReq.PushReqResponse.ConfigPush
2021-02-15 14:56:34 V/Net 692928873: Saving bdh session to cache
2021-02-15 14:56:34 V/Net 692928873: Send: ConfigPushSvc.PushResp
2021-02-15 14:56:34 I/Net 692928873: ConfigPushSvc.PushReq: Config updated.

里面有个unknown command不知道是不是小bug @Karlatemp

Karlatemp commented 3 years ago

或者是服务器没有下发 selfInfo? 总之很奇怪

Nambers commented 3 years ago

也可能是我代码写的有个逻辑错误,就我把bot online event的监听写在alsologin后了,然后在里面赋值我本地bot变量,如果没人遇到就是我的问题吧hhh

Karlatemp commented 3 years ago

应该是偶然性的,如果出现次数多会提高处理优先级,此问题属于协议问题与你自身的调用代码没啥关联

Nambers commented 3 years ago

okok

@AdoptOSS 谢谢啦可以用

sandtechnology commented 3 years ago

此外,还遇到了个问题

2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetFriendGroupList.Response
2021-02-15 14:56:32 V/Net 692928873: Send: friendlist.GetTroopMemberListReq
2021-02-15 14:56:32 V/Net 692928873: Send: friendlist.GetTroopMemberListReq
2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetTroopMemberList.Response
2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetTroopMemberList.Response
2021-02-15 14:56:32 I/Net 692928873: Successfully loaded group list: 2 in total.
2021-02-15 14:56:32 I/Bot 692928873: Login successful
2021-02-15 14:56:32 I/Bot 692928873: 2333
2021-02-15 14:56:34 D/Net 692928873: Received unknown commandName: ConfigPushSvc.PushDomain
2021-02-15 14:56:34 V/Net 692928873: Recv: ConfigPushSvc.PushReq.PushReqResponse.ConfigPush
2021-02-15 14:56:34 V/Net 692928873: Saving bdh session to cache
2021-02-15 14:56:34 V/Net 692928873: Send: ConfigPushSvc.PushResp
2021-02-15 14:56:34 I/Net 692928873: ConfigPushSvc.PushReq: Config updated.

里面有个unknown command不知道是不是小bug @Karlatemp

unknown command是正常现象 当前最好的解决办法应该是缓存自身信息

Nambers commented 3 years ago

此外,还遇到了个问题

2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetFriendGroupList.Response
2021-02-15 14:56:32 V/Net 692928873: Send: friendlist.GetTroopMemberListReq
2021-02-15 14:56:32 V/Net 692928873: Send: friendlist.GetTroopMemberListReq
2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetTroopMemberList.Response
2021-02-15 14:56:32 V/Net 692928873: Recv: FriendList.GetTroopMemberList.Response
2021-02-15 14:56:32 I/Net 692928873: Successfully loaded group list: 2 in total.
2021-02-15 14:56:32 I/Bot 692928873: Login successful
2021-02-15 14:56:32 I/Bot 692928873: 2333
2021-02-15 14:56:34 D/Net 692928873: Received unknown commandName: ConfigPushSvc.PushDomain
2021-02-15 14:56:34 V/Net 692928873: Recv: ConfigPushSvc.PushReq.PushReqResponse.ConfigPush
2021-02-15 14:56:34 V/Net 692928873: Saving bdh session to cache
2021-02-15 14:56:34 V/Net 692928873: Send: ConfigPushSvc.PushResp
2021-02-15 14:56:34 I/Net 692928873: ConfigPushSvc.PushReq: Config updated.

里面有个unknown command不知道是不是小bug @Karlatemp

unknown command是正常现象 当前最好的解决办法应该是缓存自身信息

okok 是我这边自己缓存? 缓存bot?

sandtechnology commented 3 years ago

不 应该是我们的工作 不需要你自己缓存(

Nambers commented 3 years ago

不 应该是我们的工作 不需要你自己缓存(

okok