private async void ReceivedMessageEvent(VtuberBot sender, IBotService sendingService, AcceptedMessage message)
 {
     await Task.Run(() =>
     {
         var processor = Processors.FirstOrDefault(v => v.IsMatch(message));
         if (processor == null)
         {
             return;
         }
         try
         {
             processor.SendingService = sendingService;
             processor.Database       = Database;
             processor.Observer       = this;
             processor.Process(message);
         }
         catch (Exception ex)
         {
             LogHelper.Error("处理消息时出现未知异常 包名:" + processor?.GetType(), true, ex);
         }
     });
 }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            var database = new MongoClient(Config.DefaultConfig.DatabaseUrl).GetDatabase("vtuber-bot-data");
            var observer = new VtuberBotObserver(database);

            foreach (var client in Config.DefaultConfig.Clients)
            {
                LogHelper.Info("正在初始化Bot " + client.ClientId);
                var bot = new Bot.VtuberBot {
                    BotName = client.ClientId
                };
                foreach (var config in client.Services)
                {
                    var service = ServiceDispatcher.SearchBotService(config.ServiceType);
                    if (service == null)
                    {
                        LogHelper.Error("未找到服务类型 " + config.ServiceType);
                        continue;
                    }

                    service.ListenUrl    = config.ListenUrl;
                    service.AccessToken  = config.AccessToken;
                    service.ListenPort   = config.ListenPort;
                    service.Level        = config.Level;
                    service.WebsocketUrl = config.WsUrl;
                    service.Load();
                    bot.Services.Add(service);
                }
                bot.Init();
                observer.AddBot(bot);
                LogHelper.Info($"初始化 Bot {bot.BotName} 完成..");
            }
            observer.Processors.Add(new HelpProcessor()); //HELP
            observer.Processors.Add(new LiveProcessor());
            observer.Processors.Add(new VtuberProcessor());
            observer.Processors.Add(new SubscribeProcessor());
            observer.Processors.Add(new PluginProcessor());
            services.AddSingleton(serviceProvider => database);
            services.AddSingleton(serviceProvider => observer);
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(
                JwtBearerDefaults.AuthenticationScheme,
                options =>
            {
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = true,
                    ValidIssuer    = "http://api.bot.vtb.wiki",

                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = BotJwt.IssuerSigningKey,

                    ValidateAudience = false,
                    ValidateLifetime = true,
                };
                options.SaveToken = true;
            });
            services.AddCors(option =>
            {
                option.AddPolicy("Any", policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            });
            PluginManager.Manager.LoadPlugins(observer);
        }
 public void AddBot(VtuberBot bot)
 {
     Bots.Add(bot);
     bot.ReceivedGroupMessageEvent   += ReceivedMessageEvent;
     bot.ReceivedPrivateMessageEvent += ReceivedMessageEvent;
 }