예제 #1
0
 public SocketGuild(DiscordSocketClient discord, ExtendedModel model, DataStore dataStore) : base(discord, model)
 {
     _audioLock         = new SemaphoreSlim(1, 1);
     _syncPromise       = new TaskCompletionSource <bool>();
     _downloaderPromise = new TaskCompletionSource <bool>();
     Update(model, UpdateSource.Creation, dataStore);
 }
예제 #2
0
        internal void Update(ClientState state, ExtendedModel model)
        {
            IsAvailable = !(model.Unavailable ?? false);
            if (!IsAvailable)
            {
                if (_channels == null)
                {
                    _channels = new ConcurrentHashSet <ulong>();
                }
                if (_members == null)
                {
                    _members = new ConcurrentDictionary <ulong, SocketGuildUser>();
                }
                if (_roles == null)
                {
                    _roles = new ConcurrentDictionary <ulong, SocketRole>();
                }

                /*if (Emojis == null)
                 *  _emojis = ImmutableArray.Create<Emoji>();
                 * if (Features == null)
                 *  _features = ImmutableArray.Create<string>();*/
                _syncPromise       = new TaskCompletionSource <bool>();
                _downloaderPromise = new TaskCompletionSource <bool>();
                return;
            }

            Update(state, model as Model);

            var channels = new ConcurrentHashSet <ulong>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Channels.Length * 1.05));

            {
                for (int i = 0; i < model.Channels.Length; i++)
                {
                    var channel = SocketGuildChannel.Create(this, state, model.Channels[i]);
                    state.AddChannel(channel);
                    channels.TryAdd(channel.Id);
                }
            }
            _channels = channels;

            var members = new ConcurrentDictionary <ulong, SocketGuildUser>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Members.Length * 1.05));

            {
                for (int i = 0; i < model.Members.Length; i++)
                {
                    var member = SocketGuildUser.Create(this, state, model.Members[i]);
                    members.TryAdd(member.Id, member);
                }
                DownloadedMemberCount = members.Count;

                for (int i = 0; i < model.Presences.Length; i++)
                {
                    if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
                    {
                        member.Update(state, model.Presences[i], true);
                    }
                }
            }
            _members    = members;
            MemberCount = model.MemberCount;

            var voiceStates = new ConcurrentDictionary <ulong, SocketVoiceState>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.VoiceStates.Length * 1.05));

            {
                for (int i = 0; i < model.VoiceStates.Length; i++)
                {
                    SocketVoiceChannel channel = null;
                    if (model.VoiceStates[i].ChannelId.HasValue)
                    {
                        channel = state.GetChannel(model.VoiceStates[i].ChannelId.Value) as SocketVoiceChannel;
                    }
                    var voiceState = SocketVoiceState.Create(channel, model.VoiceStates[i]);
                    voiceStates.TryAdd(model.VoiceStates[i].UserId, voiceState);
                }
            }
            _voiceStates = voiceStates;

            _syncPromise       = new TaskCompletionSource <bool>();
            _downloaderPromise = new TaskCompletionSource <bool>();
            var _ = _syncPromise.TrySetResultAsync(true);

            /*if (!model.Large)
             *  _ = _downloaderPromise.TrySetResultAsync(true);*/
        }
예제 #3
0
        internal static SocketGuild Create(DiscordSocketClient discord, ClientState state, ExtendedModel model)
        {
            var entity = new SocketGuild(discord, model.Id);

            entity.Update(state, model);
            return(entity);
        }
예제 #4
0
        public void Update(ExtendedModel model, UpdateSource source, DataStore dataStore)
        {
            if (source == UpdateSource.Rest && IsAttached)
            {
                return;
            }

            _available = !(model.Unavailable ?? false);
            if (!_available)
            {
                if (_channels == null)
                {
                    _channels = new ConcurrentHashSet <ulong>();
                }
                if (_members == null)
                {
                    _members = new ConcurrentDictionary <ulong, SocketGuildUser>();
                }
                if (_roles == null)
                {
                    _roles = new ConcurrentDictionary <ulong, Role>();
                }
                if (Emojis == null)
                {
                    Emojis = ImmutableArray.Create <Emoji>();
                }
                if (Features == null)
                {
                    Features = ImmutableArray.Create <string>();
                }
                return;
            }

            base.Update(model as Model, source);

            var channels = new ConcurrentHashSet <ulong>(1, (int)(model.Channels.Length * 1.05));

            {
                for (int i = 0; i < model.Channels.Length; i++)
                {
                    AddChannel(model.Channels[i], dataStore, channels);
                }
            }
            _channels = channels;

            var members = new ConcurrentDictionary <ulong, SocketGuildUser>(1, (int)(model.Presences.Length * 1.05));

            {
                DownloadedMemberCount = 0;
                for (int i = 0; i < model.Members.Length; i++)
                {
                    AddOrUpdateUser(model.Members[i], dataStore, members);
                }
                if (Discord.ApiClient.AuthTokenType != TokenType.User)
                {
                    var _ = _syncPromise.TrySetResultAsync(true);
                    if (!model.Large)
                    {
                        _ = _downloaderPromise.TrySetResultAsync(true);
                    }
                }

                for (int i = 0; i < model.Presences.Length; i++)
                {
                    AddOrUpdateUser(model.Presences[i], dataStore, members);
                }
            }
            _members    = members;
            MemberCount = model.MemberCount;

            var voiceStates = new ConcurrentDictionary <ulong, VoiceState>(1, (int)(model.VoiceStates.Length * 1.05));

            {
                for (int i = 0; i < model.VoiceStates.Length; i++)
                {
                    AddOrUpdateVoiceState(model.VoiceStates[i], dataStore, voiceStates);
                }
            }
            _voiceStates = voiceStates;
        }