Esempio 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);
            }
            else if (type == DiscordChannelType.GuildNews)
            {
                channel = new DiscordGuildNewsChannel(http, data, guildId);
            }
            else if (type == DiscordChannelType.GuildStore)
            {
                channel = new DiscordGuildStoreChannel(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!");
            }
        }
Esempio n. 2
0
        void HandleChannelDeleteEvent(DiscordApiData data)
        {
            Snowflake          id   = data.GetSnowflake("id").Value;
            DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value;

            if (type == DiscordChannelType.DirectMessage)
            {
                // DM channel
                DiscordDMChannel dm;
                if (cache.DMChannels.TryRemove(id, out MutableDMChannel mutableDM))
                {
                    mutableDM.ClearReferences();

                    dm = mutableDM.ImmutableEntity;
                }
                else
                {
                    dm = new DiscordDMChannel(http, data);
                }

                OnDMChannelRemoved?.Invoke(this, new DMChannelEventArgs(shard, dm));
            }
            else if (type == DiscordChannelType.GuildText ||
                     type == DiscordChannelType.GuildVoice ||
                     type == DiscordChannelType.GuildCategory ||
                     type == DiscordChannelType.GuildNews ||
                     type == DiscordChannelType.GuildStore)
            {
                // Guild channel
                Snowflake guildId = data.GetSnowflake("guild_id").Value;

                DiscordGuildChannel channel;

                if (type == DiscordChannelType.GuildText)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildTextChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildVoice)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildVoiceChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildCategory)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildCategoryChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildNews)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildNewsChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildStore)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildStoreChannel(http, data, guildId);
                    }
                }
                else
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                OnGuildChannelRemoved?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
Esempio n. 3
0
        void HandleChannelCreateEvent(DiscordApiData data)
        {
            Snowflake          id   = data.GetSnowflake("id").Value;
            DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value;

            if (type == DiscordChannelType.DirectMessage)
            {
                // DM channel
                DiscordApiData recipientData = data.GetArray("recipients").First();
                Snowflake      recipientId   = recipientData.GetSnowflake("id").Value;

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

                recipient.Update(recipientData);

                MutableDMChannel mutableDMChannel;
                if (!cache.DMChannels.TryGetValue(id, out mutableDMChannel))
                {
                    mutableDMChannel     = new MutableDMChannel(id, recipient, http);
                    cache.DMChannels[id] = mutableDMChannel;
                }

                OnDMChannelCreated?.Invoke(this, new DMChannelEventArgs(shard, mutableDMChannel.ImmutableEntity));
            }
            else if (type == DiscordChannelType.GuildText ||
                     type == DiscordChannelType.GuildVoice ||
                     type == DiscordChannelType.GuildCategory ||
                     type == DiscordChannelType.GuildNews ||
                     type == DiscordChannelType.GuildStore)
            {
                // Guild channel
                Snowflake guildId = data.GetSnowflake("guild_id").Value;

                DiscordGuildChannel channel;

                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);
                }
                else if (type == DiscordChannelType.GuildNews)
                {
                    channel = new DiscordGuildNewsChannel(http, data, guildId);
                }
                else if (type == DiscordChannelType.GuildStore)
                {
                    channel = new DiscordGuildStoreChannel(http, data, guildId);
                }
                else
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                cache.GuildChannels[id] = channel;

                OnGuildChannelCreated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
Esempio n. 4
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);
                }
                else if (channelType == DiscordChannelType.GuildNews)
                {
                    channel = new DiscordGuildNewsChannel(http, channelData, guildId);
                }
                else if (channelType == DiscordChannelType.GuildStore)
                {
                    channel = new DiscordGuildStoreChannel(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));
            }
        }