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
        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)
            {
                // 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
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                OnGuildChannelRemoved?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
Ejemplo 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)
            {
                // 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
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                cache.GuildChannels[id] = channel;

                OnGuildChannelCreated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Using the cache, this will attempt to verify that the current bot can
        /// join the selected voice channel. If the cache does not have the required details,
        /// this will NOT throw an exception (the handshake timeout will handle invalid permission anyway).
        /// </summary>
        /// <exception cref="DiscordPermissionException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        void AssertUserCanJoin(Snowflake guildId, Snowflake voiceChannelId)
        {
            DiscordGuildMember       member       = cache.GetGuildMember(guildId, Shard.UserId.Value);
            DiscordGuild             guild        = cache.GetGuild(guildId);
            DiscordGuildVoiceChannel voiceChannel = cache.GetGuildVoiceChannel(voiceChannelId);

            if (member != null && guild != null && voiceChannel != null)
            {
                // Check if the user has permission to connect.
                DiscordPermissionHelper.AssertPermission(DiscordPermission.Connect, member, guild, voiceChannel);

                // Check if the voice channel has room
                bool channelHasRoom = false;
                if (voiceChannel.UserLimit == 0)
                {
                    channelHasRoom = true;
                }
                else if (DiscordPermissionHelper.HasPermission(DiscordPermission.Administrator, member, guild, voiceChannel))
                {
                    channelHasRoom = true;
                }
                else
                {
                    IReadOnlyList <Snowflake> usersInChannel = Shard.Voice.GetUsersInVoiceChannel(voiceChannelId);
                    if (usersInChannel.Count < voiceChannel.UserLimit)
                    {
                        channelHasRoom = true;
                    }
                }

                if (!channelHasRoom)
                {
                    throw new InvalidOperationException("The voice channel is full, and the current bot " +
                                                        "does not have the administrator permission.");
                }
            }
        }
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));
            }
        }