public static IReadOnlyCollection <SocketMessage> GetCachedMessages(SocketChannel channel, DiscordSocketClient discord, MessageCache messages,
                                                                     ulong?fromMessageId, Direction dir, int limit)
 {
     if (messages != null) //Cache enabled
     {
         return(messages.GetMany(fromMessageId, dir, limit));
     }
     else
     {
         return(ImmutableArray.Create <SocketMessage>());
     }
 }
Example #2
0
        internal void AddChannel(SocketChannel channel)
        {
            _channels[channel.Id] = channel;

            switch (channel)
            {
            case SocketDMChannel dmChannel:
                _dmChannels[dmChannel.Recipient.Id] = dmChannel;
                break;

            case SocketGroupChannel groupChannel:
                _groupChannels.TryAdd(groupChannel.Id);
                break;
            }
        }
Example #3
0
        internal void AddChannel(SocketChannel channel)
        {
            _channels[channel.Id] = channel;

            if (channel is SocketDMChannel dmChannel)
            {
                _dmChannels[dmChannel.Recipient.Id] = dmChannel;
            }
            else
            {
                if (channel is SocketGroupChannel groupChannel)
                {
                    _groupChannels.TryAdd(groupChannel.Id);
                }
            }
        }
Example #4
0
        internal void AddChannel(SocketChannel channel)
        {
            _channels[channel.Id] = channel;

            var dmChannel = channel as SocketDMChannel;

            if (dmChannel != null)
            {
                _dmChannels[dmChannel.Recipient.Id] = dmChannel;
            }
            else
            {
                var groupChannel = channel as SocketGroupChannel;
                if (groupChannel != null)
                {
                    _groupChannels.TryAdd(groupChannel.Id);
                }
            }
        }
        internal SocketResolvableData(DiscordSocketClient discord, ulong?guildId, T model)
        {
            var guild = guildId.HasValue ? discord.GetGuild(guildId.Value) : null;

            var resolved = model.Resolved.Value;

            if (resolved.Users.IsSpecified)
            {
                foreach (var user in resolved.Users.Value)
                {
                    var socketUser = discord.GetOrCreateUser(discord.State, user.Value);

                    Users.Add(ulong.Parse(user.Key), socketUser);
                }
            }

            if (resolved.Channels.IsSpecified)
            {
                foreach (var channel in resolved.Channels.Value)
                {
                    SocketChannel socketChannel = guild != null
                        ? guild.GetChannel(channel.Value.Id)
                        : discord.GetChannel(channel.Value.Id);

                    if (socketChannel == null)
                    {
                        var channelModel = guild != null
                            ? discord.Rest.ApiClient.GetChannelAsync(guild.Id, channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult()
                            : discord.Rest.ApiClient.GetChannelAsync(channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult();

                        socketChannel = guild != null
                            ? SocketGuildChannel.Create(guild, discord.State, channelModel)
                            : (SocketChannel)SocketChannel.CreatePrivate(discord, discord.State, channelModel);
                    }

                    discord.State.AddChannel(socketChannel);
                    Channels.Add(ulong.Parse(channel.Key), socketChannel);
                }
            }

            if (resolved.Members.IsSpecified && guild != null)
            {
                foreach (var member in resolved.Members.Value)
                {
                    member.Value.User = resolved.Users.Value[member.Key];
                    var user = guild.AddOrUpdateUser(member.Value);
                    GuildMembers.Add(ulong.Parse(member.Key), user);
                }
            }

            if (resolved.Roles.IsSpecified)
            {
                foreach (var role in resolved.Roles.Value)
                {
                    var socketRole = guild.AddOrUpdateRole(role.Value);
                    Roles.Add(ulong.Parse(role.Key), socketRole);
                }
            }

            if (resolved.Messages.IsSpecified)
            {
                foreach (var msg in resolved.Messages.Value)
                {
                    var channel = discord.GetChannel(msg.Value.ChannelId) as ISocketMessageChannel;

                    SocketUser author;
                    if (guild != null)
                    {
                        if (msg.Value.WebhookId.IsSpecified)
                        {
                            author = SocketWebhookUser.Create(guild, discord.State, msg.Value.Author.Value, msg.Value.WebhookId.Value);
                        }
                        else
                        {
                            author = guild.GetUser(msg.Value.Author.Value.Id);
                        }
                    }
                    else
                    {
                        author = (channel as SocketChannel).GetUser(msg.Value.Author.Value.Id);
                    }

                    if (channel == null)
                    {
                        if (!msg.Value.GuildId.IsSpecified)  // assume it is a DM
                        {
                            channel = discord.CreateDMChannel(msg.Value.ChannelId, msg.Value.Author.Value, discord.State);
                        }
                    }

                    var message = SocketMessage.Create(discord, discord.State, author, channel, msg.Value);
                    Messages.Add(message.Id, message);
                }
            }

            if (resolved.Attachments.IsSpecified)
            {
                foreach (var attachment in resolved.Attachments.Value)
                {
                    var discordAttachment = Attachment.Create(attachment.Value);

                    Attachments.Add(ulong.Parse(attachment.Key), discordAttachment);
                }
            }
        }
Example #6
0
 /// <summary>
 /// Converts an existing <see cref="SocketChannel"/> to an abstracted <see cref="ISocketChannel"/> value.
 /// </summary>
 /// <param name="socketChannel">The existing <see cref="SocketChannel"/> to be abstracted.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="socketChannel"/>.</exception>
 /// <returns>An <see cref="ISocketChannel"/> that abstracts <paramref name="socketChannel"/>.</returns>
 public static ISocketChannel Abstract(this SocketChannel socketChannel)
 => socketChannel switch
 {
     null
     => throw new ArgumentNullException(nameof(socketChannel)),
Example #7
0
 /// <inheritdoc />
 public ISocketUser GetUser(ulong id)
 => SocketChannel.GetUser(id)
 ?.Abstract();
Example #8
0
 /// <summary>
 /// Constructs a new <see cref="SocketChannelAbstraction"/> around an existing <see cref="WebSocket.SocketChannel"/>.
 /// </summary>
 /// <param name="socketChannel">The value to use for <see cref="WebSocket.SocketChannel"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="socketChannel"/>.</exception>
 public SocketChannelAbstraction(SocketChannel socketChannel)
 {
     SocketChannel = socketChannel ?? throw new ArgumentNullException(nameof(socketChannel));
 }