public static void run() { int counter = 0; int lasterr = 0; while (true) { try { counter++; if (counter % 600 == 0) {//5分钟一次 GC.Collect(5, GCCollectionMode.Optimized, true, true); if (!MainHolder.bililogin.IsOnline()) { MainHolder.broadcaster.BroadcastToAdminGroup("BiliWebToken似乎已经失效,将触发重新登录。\n"); MainHolder.doBiliLogin = true; } } if (counter % 3600 == 0) {//一小时执行一次 MainHolder.broadcaster.BroadcastToAdminGroup("[在线]\n" + "最近一条消息:" + lastrecv.ToString() + "\n" + "时段总接收消息:" + MainHolder.MsgCount + "\n" + "上次检查:" + laststat.ToString()); laststat = DateTime.Now; MainHolder.MsgCount = 0; } if (counter % 30 == 0) {//每半分钟一次 if (MainHolder.bilidmkproc?.lid <= 0) { MainHolder.bilidmkproc?.PickupRunningLive(); } } if (counter % (60 * 60 * 12) == 0) {//12小时一次 MainHolder.checkCrewGroup(); } if ((counter + (60 * 60 * 6)) % (60 * 60 * 12) == 0) {//12小时一次,错位+6小时 帮小伙伴续费黑名单>_< //trigger_BanRefresh(); } if (counter >= (60 * 60 * 24)) { counter = 0;//以一天为循环体 } WatchDog.FeedDog("sckt"); MainHolder.bilidmkproc?.UpdateLiveDataToDB();//每秒都更新数据库 } catch (Exception err) { if ((counter - lasterr) < (60 * 60)) { continue; } MainHolder.broadcaster.BroadcastToAdminGroup("[计划任务失败]\n计划任务未能顺利完成(每小时仅报错一次防止持续错误刷屏)\n" + err.Message + "\nStack:" + err.StackTrace); } Thread.Sleep(1000); } }
public static void run() { run_head: try { man.fetchUnfollowed(); foreach (PrivMessageSession session in man.unfollowed_sessions) { try { session.fetch(); session.pick_latest_messages();//先取一遍消息防止抓到老消息 } catch { } } int lateststamp = -1; if (File.Exists("saves/timestamp.int")) { bool succ = int.TryParse(File.ReadAllText("saves/timestamp.int"), out int tstamp); if (succ) { lateststamp = tstamp; } } while (true) { try { man.updateSessions(); Thread.Sleep(1000); man.refresh(); foreach (PrivMessageSession session in man.unfollowed_sessions) { try { if (DropMessages) { MainHolder.Logger.Info("PRIVMSG", "DropMsg=True, drop session[#" + session.talker_id + "]."); session.Close(); continue; } while (BlockReceiver) { Thread.Sleep(0); } session.fetch();//Sessions会被自动更新,不再需要手动更新 EDIT:划掉 List <PrivMessage> messages = session.pick_latest_messages(); if (lateststamp >= session.lastmessage.timestamp) { continue; //会话最后一条消息在上一次处理之前就已经发送,很可能处理过了 } File.WriteAllText("saves/timestamp.int", lateststamp.ToString()); foreach (PrivMessage pm in messages) { if (DropMessages) { MainHolder.Logger.Info("PRIVMSG", "DropMsg=True, drop message[#" + pm.msg_seqno + "]."); continue; } while (BlockReceiver) { Thread.Sleep(0); } //var pm = session.lastmessage; if (pm.content == null || pm.content.Length < 1) { continue;//假私信 } if (pm.talker.uid == man.MyUID) { continue; //自己的消息不处理 } if (pm.timestamp <= lateststamp) { continue; //老消息 } MainHolder.Logger.Info("B站私信", pm.content); MainHolder.broadcaster.BroadcastToAdminGroup("私信.[" + pm.talker.name + "#" + pm.talker.uid + "]:" + pm.content); Task.Delay(1000).Wait(); if (pm.content[0] == 'F') { if (long.TryParse(pm.content.Substring(1), out long fq) && fq > 1000) { BoundQQ(fq, session, pm, true); } } else { if (long.TryParse(pm.content, out long qq) && qq > 1000) { BoundQQ(qq, session, pm, false); } else { MainHolder.broadcaster.SendToAnEgg("私信.[" + pm.talker.name + "#" + pm.talker.uid + "]:" + pm.content); } } } session.Close(); } catch (Exception err) { if (session.lastjson != null && session.lastjson.Length > 1) { MainHolder.broadcaster.BroadcastToAdminGroup("[Exception]\n下面的信息用来帮助鸡蛋定位错误,管理不必在意。\n[B站私信部分_内循环]" + err.Message + "\n\n堆栈跟踪:\n" + err.StackTrace + "\n出错的消息:"); MainHolder.broadcaster.BroadcastToAdminGroup(session.lastjson); } } Thread.Sleep(1 * 1000); } lateststamp = BiliApi.TimestampHandler.GetTimeStamp(DateTime.Now); WatchDog.FeedDog("pmsgchk"); } catch (Exception err) { MainHolder.broadcaster.BroadcastToAdminGroup("[Exception]\n下面的信息用来帮助鸡蛋定位错误,管理不必在意。\n[B站私信部分_外循环]" + err.Message + "\n\n堆栈跟踪:\n" + err.StackTrace + "\n-= Func Failure =-"); } Thread.Sleep(60 * 1000); } } catch (Exception err) { MainHolder.broadcaster.BroadcastToAdminGroup("[PART_FALIURE]\n一个模块发生了不可恢复的错误,已经停止运行,将尝试重启。\n[B站私信部分]" + err.Message + "\n\n堆栈跟踪:\n" + err.StackTrace + "\n出错的消息:"); MainHolder.broadcaster.BroadcastToAdminGroup(man.lastjson); Thread.Sleep(1000); MainHolder.broadcaster.SendToAnEgg("B站私信部分崩溃。\n" + err.Message + "\n\n" + err.StackTrace); goto run_head; } }