static async Task Main() { //修改控制台标题 Console.Title = @"AntiRain"; ConsoleLog.Info("AntiRain初始化", "AntiRain初始化..."); //初始化配置文件 ConsoleLog.Info("AntiRain初始化", "初始化服务器全局配置..."); //全局文件初始化不需要uid,填0仅占位,不使用构造函数重载 Config config = new Config(0); config.GlobalConfigFileInit(); config.LoadGlobalConfig(out GlobalConfig globalConfig, false); ConsoleLog.SetLogLevel(globalConfig.LogLevel); //显示Log等级 ConsoleLog.Debug("Log Level", globalConfig.LogLevel); //初始化字符编码 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //指令匹配初始化 Command.KeywordResourseInit(); Command.RegexResourseInit(); Command.PCRGuildBattlecmdResourseInit(); ConsoleLog.Info("AntiRain初始化", "启动反向WS服务器..."); //初始化服务器 SoraWSServer server = new SoraWSServer(new ServerConfig { Location = globalConfig.Location, Port = globalConfig.Port, AccessToken = globalConfig.AccessToken, UniversalPath = globalConfig.UniversalPath, ApiPath = globalConfig.ApiPath, EventPath = globalConfig.EventPath, HeartBeatTimeOut = globalConfig.HeartBeatTimeOut, ApiTimeOut = globalConfig.ApiTimeOut }); //服务器回调 //初始化 server.Event.OnClientConnect += InitalizationEvent.Initalization; //群聊事件 server.Event.OnGroupMessage += GroupMessageEvent.GroupMessageParse; //私聊事件 server.Event.OnPrivateMessage += PrivateMessageEvent.PrivateMessageParse; //群聊戳一戳 server.Event.OnGroupPoke += GroupPokeEvent.GroupPokeEventParse; //关闭连接事件处理 server.ConnManager.OnCloseConnectionAsync += TimerEventParse.StopTimer; server.ConnManager.OnHeartBeatTimeOut += TimerEventParse.StopTimer; await server.StartServer(); }
static async Task Main(string[] args) { ConsoleLog.SetLogLevel(LogLevel.Debug); ConsoleLog.Debug("dotnet version", Environment.Version); SoraWSServer server = new SoraWSServer(new ServerConfig { Port = 8080, ApiTimeOut = 5000 }); //服务器连接事件 server.ConnManager.OnOpenConnectionAsync += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"connectionId = {connectionInfo.Id} type = {eventArgs.Role}"); return(ValueTask.CompletedTask); }; //服务器连接关闭事件 server.ConnManager.OnCloseConnectionAsync += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"connectionId = {connectionInfo.Id} type = {eventArgs.Role}"); return(ValueTask.CompletedTask); }; //服务器心跳包超时事件 server.ConnManager.OnHeartBeatTimeOut += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"Get heart beat time out from[{connectionInfo.Id}] uid[{eventArgs.SelfId}]"); return(ValueTask.CompletedTask); }; //群聊消息事件 server.Event.OnGroupMessage += async(sender, eventArgs) => { if (eventArgs.SourceGroup != 732774614) { return; } var msg = await eventArgs.SoraApi .GetUserInfo(eventArgs.Sender); ConsoleLog.Info("qid", msg.qid); }; //私聊消息事件 server.Event.OnOfflineFileEvent += async(sender, eventArgs) => { await eventArgs.Sender.SendPrivateMessage("好耶"); }; await server.StartServer().RunCatch(); }
static async Task Main(string[] args) { ConsoleLog.SetLogLevel(LogLevel.Debug); ConsoleLog.Debug("dotnet version", Environment.Version); SoraWSServer server = new SoraWSServer(new ServerConfig { Port = 9200 }); //服务器连接事件 server.ConnManager.OnOpenConnectionAsync += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"connectionId = {connectionInfo.Id} type = {eventArgs.Role}"); return(ValueTask.CompletedTask); }; //服务器连接关闭事件 server.ConnManager.OnCloseConnectionAsync += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"connectionId = {connectionInfo.Id} type = {eventArgs.Role}"); return(ValueTask.CompletedTask); }; //服务器心跳包超时事件 server.ConnManager.OnHeartBeatTimeOut += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"Get heart beat time out from[{connectionInfo.Id}] uid[{eventArgs.SelfId}]"); return(ValueTask.CompletedTask); }; //群聊消息事件 server.Event.OnGroupMessage += async(sender, eventArgs) => { await eventArgs.SourceGroup.SendGroupMessage("好耶"); }; //私聊消息事件 server.Event.OnOfflineFileEvent += async(sender, eventArgs) => { await eventArgs.Sender.SendPrivateMessage("好耶"); }; try { await server.StartServer(); } catch (Exception e) //侦测所有未处理的错误 { ConsoleLog.Fatal("unknown error", ConsoleLog.ErrorLogBuilder(e)); } }
static async Task Main(string[] args) { ConsoleLog.SetLogLevel(LogLevel.Debug); ConsoleLog.Debug("dotnet version", Environment.Version); SoraWSServer server = new SoraWSServer(new ServerConfig { Port = 8080 }); //服务器连接事件 server.ConnManager.OnOpenConnectionAsync += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"connectionId = {connectionInfo.Id} type = {eventArgs.Role}"); return(ValueTask.CompletedTask); }; //服务器连接关闭事件 server.ConnManager.OnCloseConnectionAsync += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"connectionId = {connectionInfo.Id} type = {eventArgs.Role}"); return(ValueTask.CompletedTask); }; //服务器心跳包超时事件 server.ConnManager.OnHeartBeatTimeOut += (connectionInfo, eventArgs) => { ConsoleLog.Debug("Sora_Test", $"Get heart beat time out from[{connectionInfo.Id}] uid[{eventArgs.SelfId}]"); return(ValueTask.CompletedTask); }; //群聊消息事件 server.Event.OnGroupMessage += async(sender, eventArgs) => { await eventArgs.SourceGroup.SendGroupMessage("好耶"); }; //私聊消息事件 server.Event.OnPrivateMessage += async(sender, eventArgs) => { await eventArgs.Sender.SendPrivateMessage(CQCode.CQImage("E:\\P\\83128088_p0.png")); }; await server.StartServer().RunCatch(); }
private static async Task Main(string[] args) { if (!Directory.Exists("cache")) { Directory.CreateDirectory("cache"); } JsonSerializerOptions options = new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true, WriteIndented = true }; options.Converters.Add(new Common.DatetimeJsonConverter()); InvokeConfig invokeConfig = InvokeConfig.Default; if (!File.Exists("InvokeConfig.json")) { await File.WriteAllTextAsync("InvokeConfig.json", JsonSerializer.Serialize(invokeConfig, options)); ConsoleLog.Info("NoACG", "没有找到InvokeConfig.json,已经生成默认文件"); } else { invokeConfig = JsonSerializer.Deserialize <InvokeConfig>(await File.ReadAllTextAsync("InvokeConfig.json"), options); if (invokeConfig == null) { ConsoleLog.Warning("NoACG", "读取配置文件InvokeConfig.json发生了错误,建议删除文件重新生成"); return; } ConsoleLog.Info("NoACG", "已经读取配置文件InvokeConfig.json"); } Config.HandleInvokeParams(invokeConfig); AppConfig appConfig = AppConfig.Default; if (!File.Exists("AppConfig.json")) { await File.WriteAllTextAsync("AppConfig.json", JsonSerializer.Serialize(appConfig, options)); Log.Info(typeof(Program).FullName, "没有找到AppConfig.json,已经生成默认文件"); } else { appConfig = JsonSerializer.Deserialize <AppConfig>(await File.ReadAllTextAsync("AppConfig.json"), options); if (appConfig == null) { ConsoleLog.Warning("NoACG", "读取配置文件AppConfig.json发生了错误,建议删除文件重新生成"); return; } ConsoleLog.Info("NoACG", "已经读取配置文件AppConfig.json"); } ConsoleLog.SetLogLevel(LogLevel.Debug); Yande yande = new Yande(appConfig.YandeConfig); var webClient = new WebClient { Proxy = appConfig.YandeConfig.Proxy }; Twitter twitter = new Twitter(ref webClient); //初始化服务器实例 SoraWSServer server = new SoraWSServer(appConfig.ServerConfig); //setup our DI var serviceProvider = new ServiceCollection() .AddSingleton(yande) .AddSingleton(server) .AddSingleton(invokeConfig) .AddSingleton(options) .AddSingleton(twitter) .BuildServiceProvider(); TweeterMonitorConfig tweeterMonitorConfig = new TweeterMonitorConfig { Proxy = appConfig.YandeConfig.Proxy, Items = new List <MonitorItem> { new MonitorItem() { Mark = "wuyu_8512", Private = new List <long> { 3117836505 }, Group = new List <long> { 764444946, 551856311, 648300801 }, } } }; bool isConnected = false; server.Event.OnClientConnect += async(sender, eventArgs) => { if (!isConnected) { isConnected = true; MonitorManage monitorManage = new MonitorManage(tweeterMonitorConfig, eventArgs.SoraApi); monitorManage.OpenTwitterMonitor(); } var(apiStatus, nick) = await eventArgs.SoraApi.GetLoginUserName(); }; //群消息接收回调 server.Event.OnGroupMessage += async(sender, eventArgs) => { if (invokeConfig.BlackLists?.Contains(eventArgs.SourceGroup.Id) ?? false) { return; } if (invokeConfig.BlackLists?.Contains(eventArgs.SenderInfo.UserId) ?? false) { return; } if (isConnected) { await HandleMessage(serviceProvider, invokeConfig.UniversalConfigs.Concat(invokeConfig.GroupConfigs), eventArgs, eventArgs.SourceGroup.Id); } }; server.Event.OnPrivateMessage += async(sender, eventArgs) => { if (invokeConfig.BlackLists?.Contains(eventArgs.SenderInfo.UserId) ?? false) { return; } if (isConnected) { await HandleMessage(serviceProvider, invokeConfig.UniversalConfigs.Concat(invokeConfig.PrivateConfigs), eventArgs, eventArgs.SenderInfo.UserId); } }; //启动服务器 await server.StartServer(); }