예제 #1
0
        /// <summary>
        /// Sync channels and guild when discord is ready
        /// </summary>
        private static async void OnDiscordReady(object?sender, EventArgs?e)
        {
            Logger.Info("Discord Ready, waiting for lock");
            await _contextSemaphore.WaitAsync().ConfigureAwait(false);

            Logger.Info("Lock acquired");

            try
            {
                Logger.Info("Loading guilds and channels");
                using (DatabaseContext context = new ContextBuilder().CreateDbContext())
                {
                    await context
                    .Channels
                    .Include(c => c.Guild)
                    .Include(c => c.Trackers)
                    .LoadAsync()
                    .ConfigureAwait(false);

                    context.ChangeTracker.DetectChanges();
                    ChannelAdapter.SyncAll(context, _discordService);
                    context.SaveChanges();
                }

                _srlService.IsUpdateTriggerEnabled = true;
            }
            catch (Exception ex)
            {
                Logger.Error("Exception thrown", ex);
                Environment.Exit(-1);
            }
            finally
            {
                _contextSemaphore.Release();
            }
        }