Exemple #1
0
        internal void HandleGuildCreate(GatewayGuild data)
        {
            if (!_guilds.TryGetValue(data.Id.RawValue, out var guild))
            {
                guild = new CachedGuild {
                    Id = data.Id
                };
                guild.Update(data);
                _guilds[data.Id.RawValue] = guild;
                Created?.Invoke(guild);

                guild.Unavailable = data.Unavailable;
                if (guild.Unavailable != true)
                {
                    Available?.Invoke(guild);
                }
            }
            else
            {
                if (data.Unavailable == false && guild.Unavailable != false)
                {
                    guild.Unavailable = false;
                    Available?.Invoke(guild);
                }
                else if (data.Unavailable == true && guild.Unavailable != true)
                {
                    guild.Unavailable = true;
                    Unavailable?.Invoke(guild);
                }
                guild.Update(data);
            }
        }
Exemple #2
0
 private void OnUnavailable()
 {
     try
     {
         Unavailable?.Invoke(this, EventArgs.Empty);
     }
     catch
     {
     }
 }
Exemple #3
0
 internal void HandleSessionLost()
 {
     foreach (var guild in this)
     {
         if (guild.Unavailable != true)
         {
             guild.Unavailable = true;
             Unavailable?.Invoke(guild);
         }
     }
     _guilds.Clear();
 }
Exemple #4
0
 private void tryLoadConfig()
 {
     try
     {
         Configuration = JsonConvert.DeserializeObject <T>(File.ReadAllText(_configPath));
         Changed?.Invoke();
         _lastUpdateSuccessful = true;
         _recentlyUpdated      = true;
     }
     catch (Exception ex)
     {
         Unavailable?.Invoke();
     }
 }
Exemple #5
0
        public LiveConfig(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (!Path.IsPathRooted(filePath))
            {
                filePath = Path.Combine(Environment.CurrentDirectory, filePath);
            }

            _configPath = filePath;

            if (!File.Exists(_configPath))
            {
                throw new FileNotFoundException(_configPath);
            }

            _timer          = new Timer();
            _timer.Elapsed += (s, e) =>
            {
                _recentlyUpdated = false;
                _timer.Stop();
            };
            _timer.Interval = 500;

            var dir = Path.GetDirectoryName(_configPath);

            _watcher = new FileSystemWatcher(dir);
            _watcher.NotifyFilter = _watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.FileName;
            _watcher.Filter       = Path.GetFileName(_configPath);

            _watcher.Deleted += (s, e) => Unavailable?.Invoke();
            _watcher.Created += (s, e) => tryLoadConfig();
            _watcher.Changed += (s, e) => tryLoadConfig();
            _watcher.Renamed += (s, e) => tryLoadConfig();
        }
Exemple #6
0
        internal void HandleGuildDelete(UnavailableGuild data)
        {
            if (!_guilds.TryGetValue(data.Id.RawValue, out var guild))
            {
                _logger.Warning($"Failed to process GuildDelete, unknown guild {data.Id}");
                return;
            }

            if (data.Unavailable == true)
            {
                data.Unavailable = true;
                Unavailable?.Invoke(guild);
            }
            else
            {
                if (guild.Unavailable != true)
                {
                    guild.Unavailable = true;
                    Unavailable?.Invoke(guild);
                }
                _guilds.TryRemove(data.Id.RawValue, out _);
                Deleted?.Invoke(guild);
            }
        }