public static async Task <DiscordClientFactory> CreateAsync(IDbContext dbContext)
        {
            var fac = new DiscordClientFactory(dbContext);
            await fac.Client.ConnectAsync();

            return(fac);
        }
예제 #2
0
        public async Task RunAsync(CancellationToken cancellationToken = default)
        {
            var cancellationTokenSource = new CancellationTokenSource();

            try
            {
                InitBackupSnapshotTask();
                Task.Run(ActionThread);
                var queueHandlerThread = new Thread(QueueExecuteHandler);
                queueHandlerThread.Start();
                var activityObject = new DiscordActivity();
                var embedMessage   = new DiscordEmbedBuilder();

                discordClientFactory = await DiscordClientFactory.CreateAsync(this._dbContext);

                var runner = new BroadCaptureRunner();
                runner.OnBroadCaptured += async(broadMessage) =>
                {
                    try
                    {
                        var type = BroadCaptureML.Model.ConsumeModel.Predict(broadMessage);
                        if (type == BroadCaptureML.Model.Enum.MessageType.Other && Config.Instance.DisabledOtherMessage)
                        {
                            return;
                        }
                        var author = StringHelpers.ExtractCreateBy(broadMessage);
                        //await this._dbContext.Messages.ManualInsertAsync(broadMessage, (int)type, author);
                        await this._dbContext.Messages.InsertAsync(new Message
                        {
                            content  = broadMessage,
                            type     = (int)type,
                            createby = author
                        });

                        embedMessage.Author = new DiscordEmbedBuilder.EmbedAuthor()
                        {
                            Name = $"{author} - {type.ToString()}"
                        };
                        embedMessage.Title     = broadMessage;
                        embedMessage.Timestamp = DateTime.Now;
                        embedMessage.Color     = DiscordColorHelpers.GetColorForMessage(type);
                        foreach (var channel in discordClientFactory.GetDiscordChannels())
                        {
                            Queue.Enqueue(CheckReservation(broadMessage, channel, type));
                            discordClientFactory.Client.SendMessageAsync(channel, embed: embedMessage);
                        }
                        shortLiveUidBuffer.Clear();
                        activityObject.Name = $"Reading {this._dbContext.Messages.Count():n0} messages now.";
                        await discordClientFactory.Client.UpdateStatusAsync(activityObject);
                    }
                    catch (Exception ex)
                    {
                        this._dbContext.ErrorLogs.Insert(new ErrorLog(ex.ToString()));
                    }
                };
                runner.OnMaintenanceModeActivated += async() =>
                {
                    try
                    {
                        activityObject.Name = "Maintenance Mode Activated";
                        await discordClientFactory.Client.UpdateStatusAsync(activityObject);
                    }
                    catch (Exception ex)
                    {
                        this._dbContext.ErrorLogs.Insert(new ErrorLog(ex.ToString()));
                    }
                };
                await runner.RunAsync(cancellationTokenSource.Token);

                await Task.Delay(-1);
            }
            catch (Exception ex)
            {
                cancellationTokenSource.Cancel();
                this._dbContext.ErrorLogs.Insert(new ErrorLog(ex.ToString()));
                //Process.Start("BroadCapture.exe");
            }
        }