Ejemplo n.º 1
0
        void HandleChannelUpdateEvent(DiscordApiData data)
        {
            Snowflake          id      = data.GetSnowflake("id").Value;
            DiscordChannelType type    = (DiscordChannelType)data.GetInteger("type");
            Snowflake          guildId = data.GetSnowflake("guild_id").Value;

            DiscordGuildChannel channel = null;

            if (type == DiscordChannelType.GuildText)
            {
                channel = new DiscordGuildTextChannel(http, data, guildId);
            }
            else if (type == DiscordChannelType.GuildVoice)
            {
                channel = new DiscordGuildVoiceChannel(http, data, guildId);
            }
            else if (type == DiscordChannelType.GuildCategory)
            {
                channel = new DiscordGuildCategoryChannel(http, data, guildId);
            }

            if (channel != null)
            {
                cache.GuildChannels[id] = channel;

                OnGuildChannelUpdated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
            else
            {
                log.LogWarning($"Failed to update channel {id} because the type ({type}) doesn't have an implementation!");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a list of all channels in a guild.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordGuildChannel> > GetGuildChannels(Snowflake guildId)
        {
            DiscordApiData data = await rest.Get($"guilds/{guildId}/channels",
                                                 $"guilds/{guildId}/channels").ConfigureAwait(false);

            DiscordGuildChannel[] channels = new DiscordGuildChannel[data.Values.Count];
            for (int i = 0; i < channels.Length; i++)
            {
                channels[i] = (DiscordGuildChannel)DeserializeChannelData(data.Values[i]);
            }

            return(channels);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the specified channel or null if it is not currently cached.
        /// </summary>
        public DiscordChannel GetChannel(Snowflake channelId)
        {
            DiscordGuildChannel guildChannel = GuildChannels[channelId];

            if (guildChannel != null)
            {
                return(guildChannel);
            }
            else
            {
                return(DMChannels[channelId]?.ImmutableEntity);
            }
        }
Ejemplo n.º 4
0
        internal void AddGuildChannel(DiscordGuildChannel guildChannel)
        {
            GuildChannels[guildChannel.Id] = guildChannel;

            ConcurrentHashSet <Snowflake> guildChannelsIdSet;

            if (!GuildChannelIds.TryGetValue(guildChannel.GuildId, out guildChannelsIdSet))
            {
                guildChannelsIdSet = new ConcurrentHashSet <Snowflake>();
                GuildChannelIds[guildChannel.GuildId] = guildChannelsIdSet;
            }

            guildChannelsIdSet.Add(guildChannel.Id);
        }
Ejemplo n.º 5
0
        void HandleGuildCreateEvent(DiscordApiData data)
        {
            Snowflake guildId = data.GetSnowflake("id").Value;

            bool wasUnavailable = !cache.IsGuildAvailable(guildId);

            // Update guild
            MutableGuild mutableGuild;

            if (!cache.Guilds.TryGetValue(guildId, out mutableGuild))
            {
                mutableGuild          = new MutableGuild(guildId, http);
                cache.Guilds[guildId] = mutableGuild;
            }

            mutableGuild.Update(data);

            // Ensure the cache guildId list contains this guild (it uses a hashset so don't worry about duplicates).
            cache.AddGuildId(guildId);

            // GUILD_CREATE specifics
            // Update metadata
            cache.GuildMetadata[guildId] = new DiscordGuildMetadata(data);

            // Deserialize members
            cache.GuildMembers.Clear(guildId);
            IList <DiscordApiData> membersArray = data.GetArray("members");

            for (int i = 0; i < membersArray.Count; i++)
            {
                DiscordApiData memberData = membersArray[i];

                DiscordApiData userData = memberData.Get("user");
                Snowflake      userId   = userData.GetSnowflake("id").Value;

                MutableUser user;
                if (!cache.Users.TryGetValue(userId, out user))
                {
                    user = new MutableUser(userId, false, http);
                    cache.Users[userId] = user;
                }

                user.Update(userData);

                MutableGuildMember member;
                if (!cache.GuildMembers.TryGetValue(guildId, userId, out member))
                {
                    member = new MutableGuildMember(user, guildId, http);
                    cache.GuildMembers[guildId, userId] = member;
                }

                member.Update(memberData);
            }

            // Deserialize channels
            cache.ClearGuildChannels(guildId);
            IList <DiscordApiData> channelsArray = data.GetArray("channels");

            for (int i = 0; i < channelsArray.Count; i++)
            {
                DiscordApiData     channelData = channelsArray[i];
                DiscordChannelType channelType = (DiscordChannelType)channelData.GetInteger("type");

                DiscordGuildChannel channel = null;
                if (channelType == DiscordChannelType.GuildText)
                {
                    channel = new DiscordGuildTextChannel(http, channelData, guildId);
                }
                else if (channelType == DiscordChannelType.GuildVoice)
                {
                    channel = new DiscordGuildVoiceChannel(http, channelData, guildId);
                }
                else if (channelType == DiscordChannelType.GuildCategory)
                {
                    channel = new DiscordGuildCategoryChannel(http, channelData, guildId);
                }

                if (channel != null)
                {
                    cache.AddGuildChannel(channel);
                }
            }

            // Deserialize voice states
            cache.GuildVoiceStates.Clear(guildId);
            IList <DiscordApiData> voiceStatesArray = data.GetArray("voice_states");

            for (int i = 0; i < voiceStatesArray.Count; i++)
            {
                DiscordVoiceState state = new DiscordVoiceState(guildId, voiceStatesArray[i]);
                UpdateMemberVoiceState(state);
            }

            // Deserialize presences
            cache.GuildPresences.Clear(guildId);
            IList <DiscordApiData> presencesArray = data.GetArray("presences");

            for (int i = 0; i < presencesArray.Count; i++)
            {
                // Presence's in GUILD_CREATE do not contain full user objects,
                // so don't attempt to update them here.

                DiscordApiData presenceData = presencesArray[i];
                Snowflake      userId       = presenceData.LocateSnowflake("user.id").Value;

                cache.GuildPresences[guildId, userId] = new DiscordUserPresence(userId, presenceData);
            }

            // Mark the guild as available
            cache.SetGuildAvailability(guildId, true);

            // Fire event
            if (wasUnavailable)
            {
                OnGuildAvailable?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity));
            }
            else
            {
                OnGuildCreated?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity));
            }
        }
Ejemplo n.º 6
0
 internal GuildChannelEventArgs(Shard shard, Snowflake guildId, DiscordGuildChannel channel)
     : base(shard)
 {
     GuildId = guildId;
     Channel = channel;
 }