Beispiel #1
0
        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();
        }
Beispiel #2
0
 public MonitorManage(TweeterMonitorConfig config, SoraApi soraApi)
 {
     _config  = config;
     _soraApi = soraApi;
 }